Skip to content

Commit 460cdd7

Browse files
committed
minor #19882 Document strange JsonResponse constructor behaviour (lol768)
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
2 parents c6d0feb + 90fa8ca commit 460cdd7

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

components/http_foundation.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,16 @@ The ``JsonResponse`` class sets the ``Content-Type`` header to
729729
Only methods that respond to GET requests are vulnerable to XSSI 'JSON Hijacking'.
730730
Methods responding to POST requests only remain unaffected.
731731

732+
.. danger::
733+
734+
The ``JsonResponse`` constructor exhibits non-standard JSON encoding behavior
735+
and will treat ``null`` as an empty object if passed as a constructor argument,
736+
despite null being a `valid JSON top-level value`_.
737+
738+
This behavior cannot be changed without backwards-compatibility concerns, but
739+
it's possible to call ``setData`` and pass the value there to opt-out of the
740+
behavior.
741+
732742
JSONP Callback
733743
~~~~~~~~~~~~~~
734744

@@ -832,5 +842,6 @@ Learn More
832842
.. _nginx: https://www.nginx.com/resources/wiki/start/topics/examples/xsendfile/
833843
.. _Apache: https://tn123.org/mod_xsendfile/
834844
.. _`JSON Hijacking`: https://haacked.com/archive/2009/06/25/json-hijacking.aspx/
845+
.. _`valid JSON top-level value`: https://www.json.org/json-en.html
835846
.. _OWASP guidelines: https://cheatsheetseries.owasp.org/cheatsheets/AJAX_Security_Cheat_Sheet.html#always-return-json-with-an-object-on-the-outside
836847
.. _RFC 8674: https://tools.ietf.org/html/rfc8674

0 commit comments

Comments
 (0)