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

JsonResponse makes it impossible to send literal null #11679

Closed
zachmullen opened this issue Aug 15, 2014 · 6 comments
Closed

JsonResponse makes it impossible to send literal null #11679

zachmullen opened this issue Aug 15, 2014 · 6 comments

Comments

@zachmullen
Copy link

The default value of the $data parameter for JsonResponse is null, but when it is set to null, the actual value sent in the response is in fact {}. This is problematic because it makes it impossible to send a literal value of null, which is valid JSON. The distinction is important, because in javascript {} evaluates truthy and null evaluates falsy.

@xabbuh
Copy link
Member

xabbuh commented Aug 15, 2014

This seems to be the desired value since there actually is a test ensuring this behaviour: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php#L18-22

You can use the setData() method to send a null response:

$response = new JsonResponse();
$response->setData(null);

@zachmullen
Copy link
Author

Thanks, that works as a workaround.

@stof
Copy link
Member

stof commented Aug 18, 2014

the goal is that calling new JsonResponse() without data returns a {} response. Given that we cannot have new arrayObject() as default value, we had to make a special handling for null in the constructor.
The fix is indeed to call setData explicitly.

Note that this cannot be changed for BC (I don't know many APIs returning null as JSON content though btw). I'm closing this issue

@stof stof closed this as completed Aug 18, 2014
@lol768
Copy link
Contributor

lol768 commented May 13, 2024

Can we please document this behaviour somewhere?

It's bizarre behaviour to me, and suggests a misunderstanding of the JSON specification from whoever originally implemented it, but at least if it was written down somewhere in the PHPdoc I wouldn't have wasted time wondering what was going on

@derrabus
Copy link
Member

Can we please document this behaviour somewhere?

Go ahead and propose the piece of documentation that would've helped you to https://github.com/symfony/symfony-docs/

@OskarStark
Copy link
Contributor

What's about adding also a testcase for this behavior and using setData ?

xabbuh added a commit that referenced this issue May 15, 2024
… content (xabbuh)

This PR was merged into the 5.4 branch.

Discussion
----------

[HttpFoundation] add test for JSON response with null as content

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | #11679 (comment)
| License       | MIT

Commits
-------

5c37277 add test for JSON response with null as content
OskarStark added a commit to symfony/symfony-docs that referenced this issue May 16, 2024
…768)

This PR was merged into the 5.4 branch.

Discussion
----------

Document strange JsonResponse constructor behaviour

Discussed on symfony/symfony#11679

Commits
-------

90fa8ca Respond to "patches welcome" comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants