Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty mail_settings returns empty JSON array instead of object #332

Closed
Dreyer opened this issue Nov 28, 2016 · 3 comments
Closed

Empty mail_settings returns empty JSON array instead of object #332

Dreyer opened this issue Nov 28, 2016 · 3 comments
Labels
status: help wanted requesting help from the community type: bug bug in the library

Comments

@Dreyer
Copy link
Contributor

Dreyer commented Nov 28, 2016

Issue Summary

When assigning empty mail settings, JSON serialization returns an empty array but the API expects an object (or null, also works).

// for purpose of illustration, user has set this to false.
$spam_check = false;

$from = new Email(null, "test@example.com");
$subject = "Hello World from the SendGrid PHP Library";
$to = new Email(null, "test@example.com");
$content = new Content("text/plain", "some text here");
$mail = new Mail($from, $subject, $to, $content);
$to = new Email(null, "test2@example.com");
$mail->personalization[0]->addTo($to);

$mail_settings = new \SendGrid\MailSettings();
// not adding any settings because spam check is false.
if ($spam_check) {
            $spam_check = new \SendGrid\SpamCheck();
            $spam_check->setEnable(true);
            $spam_check->setThreshold(1);
            $spam_check->setPostToUrl('https://spamcatcher.sendgrid.com');
            $mail_settings->setSpamCheck($spam_check);
}
$mail->setMailSettings($mail_settings);
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo $response->body();
echo $response->headers();

Seems to be resolved by altering MailSettings::jsonSerialize() on L:569

   public function jsonSerialize()
    {
        return array_filter(
            [
                'bcc' => $this->getBccSettings(),
                'bypass_list_management' => $this->getBypassListManagement(),
                'footer' => $this->getFooter(),
                'sandbox_mode' => $this->getSandboxMode(),
                'spam_check' => $this->getSpamCheck()
            ],
            function ($value) {
                return $value !== null;
            }
        ) ?: null;
    }

Steps to Reproduce

  1. Don't add any mail settings.
  2. Attempt to send request.
  3. Get error message:
{"errors":[{"message":"Invalid type. Expected: object, given: array.","field":"mail_settings","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Mail-Settings-Errors"}]}

I would imagine it possible to instantiate new SendGrid\MailSettings(); and then optionally add settings as flags are required at run time or by users options for a possible spam check.

Technical details:

  • sendgrid-php Version: master (latest commit: 46f85e6)
  • PHP Version: 5.6.2
@thinkingserious thinkingserious added status: help wanted requesting help from the community type: bug bug in the library labels Nov 28, 2016
@thinkingserious
Copy link
Contributor

Thanks for reporting this bug @Dreyer, especially in such detail :)

I have added this to our backlog for a fix.

It can gain priority with other +1s and/or a PR.

@Skrath
Copy link

Skrath commented Feb 10, 2017

Additionally, if you set Sandbox Mode to false explicitly:

$mail = new \SendGrid\Mail();
$mail_settings = new \SendGrid\MailSettings();
$sandbox_mode = new \SendGrid\SandBoxMode();
$sandbox_mode->setEnable(false);
$mail_settings->setSandboxMode($sandbox_mode);
$mail->setMailSettings($mail_settings);

You get a very similar error: "message":"Invalid type. Expected: object, given: array.","field":"mail_settings.sandbox_mode"

Setting Sandbox Mode to true works as expected.

@thinkingserious
Copy link
Contributor

Thanks for the confirmation @Skrath! I've added your vote to this issue in our backlog.

thinkingserious added a commit that referenced this issue Mar 4, 2017
5.2.3: Pull #334
Fixed serialization of empty JSON objects, fixes #332 & #314
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: bug bug in the library
Projects
None yet
Development

No branches or pull requests

3 participants