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

Tracking Settings jsonSerialize returns empty array #314

Closed
schneiders11 opened this issue Oct 19, 2016 · 8 comments
Closed

Tracking Settings jsonSerialize returns empty array #314

schneiders11 opened this issue Oct 19, 2016 · 8 comments
Labels
status: help wanted requesting help from the community type: bug bug in the library

Comments

@schneiders11
Copy link

When I attempt to disable Click, Open, or Google Analytics tracking via the API it seems like the jsonSerialize/json_encode functions do not return the expected output. The php objects appear correct, but when encoding them, the values are lost or not returned properly.

PHP Versions: 5.6.23 & 7.0.12
SendGrid Version: 5.1.1

CODE SETUP:
$mail = new SendGrid\Mail();
...
$tracking_settings = new SendGrid\TrackingSettings();
$click_tracking = new SendGrid\ClickTracking();
$click_tracking->setEnable(false);
$click_tracking->setEnableText(false);
$tracking_settings->setClickTracking($click_tracking);
$mail->setTrackingSettings($tracking_settings);

RESPONSE:
{"errors":[{"message":"Invalid type. Expected: object, given: array.","field":"tracking_settings.click_tracking","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.tracking_settings.click_tracking"}]}

SUBMITTED BODY (partial): "tracking_settings":{"click_tracking":[]}

CODE:
var_dump($click_tracking);
echo 'click:'.json_encode($click_tracking);

OUTPUT:
object(SendGrid\ClickTracking)#12 (2) {
["enable":"SendGrid\ClickTracking":private]=>
bool(false)
["enable_text":"SendGrid\ClickTracking":private]=>
bool(false)
}
click:[]

@thinkingserious thinkingserious added type: bug bug in the library status: help wanted requesting help from the community hacktoberfest labels Oct 19, 2016
@thinkingserious
Copy link
Contributor

Hi @schneiders11,

Thanks for letting us know. This bug is now added to our backlog for a fix.

@pizzot
Copy link

pizzot commented Oct 20, 2016

Pretty straightforward fix.

https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L73

array_filter() filters out all false values as per documentation.

@thinkingserious Was it supposed to filter out null values?

@pizzot
Copy link

pizzot commented Oct 20, 2016

Looks like you were sleeping there in the US :) I have put a PR in the meantime. It fixes this particular issue but discovers more. It is the same problem for all the settings classes.

There's a major design problem in how these settings classes are created. See, you now need to fix the same issue on all the X classes. Having the serialized function abstracted into a base class would prevent this in the first place.

In fact all the functionality could be abstracted into a base class for the settings if we eliminate getters and setters and use magic methods instead.
I've put together a quick example:
https://github.com/okneloper/sendgrid-php/blob/refactoring/refactoring.php

@pizzot
Copy link

pizzot commented Oct 20, 2016

Looks like I need to submit an agreemt before the fix can be merged though...

@pizzot
Copy link

pizzot commented Oct 20, 2016

@schneiders11 you can clone this fix from my fork and see if it works now for you: https://github.com/okneloper/sendgrid-php/tree/clickTracking

@thinkingserious
Copy link
Contributor

@okneloper,

Thanks for providing a temporary fix! Please take a moment to fill out this form so we can send you some swag :)

@schneiders11
Copy link
Author

@okneloper That did the trick! I ended up working around it by reverting back to the v2 API, but no one likes that solution, I'm just glad I wasn't going crazy. Thanks for the quick patch option!

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
@thinkingserious
Copy link
Contributor

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