Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[JsonStreamer] Addinclude_null_properties option#60730
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Uh oh!
There was an error while loading.Please reload this page.
soyuka commentedJun 6, 2025
I'm wondering if this shouldn't actually be the default, I would even not add this as an option as this would simplify the code if a value is null we just skip it. The JsonStreamer should be used for maximum performances, |
mtarld commentedJun 7, 2025
I agree with@soyuka; in my opinion, the less noise we have, the better. I updated the PR in that way. |
null valuenull valuenicolas-grekas commentedJun 27, 2025
Let's target 7.3 then |
mtarld commentedJun 27, 2025
This PR needs#60544, which has been merged in 7.4. |
nicolas-grekas left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Go for 7.4 then
stof commentedJun 27, 2025
Omitting a property from the JSON or setting it to
Always stripping null properties from the output is a no-go to me, as it makes the component incompatible with some kinds of output (and this would be a BC break for projects that rely on those cases) |
alexandre-daubois commentedJun 27, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I agree with@stof: consuming APIs with fields that can sometimes appear or disappear can really be disturbing. That's also true if a documentation is provided: the response may be different from what's written in the doc because some fields are missing. To me, it is semantically really different to have a null value or no value at all and it may not be treated the same way in underlying code. The option you proposed first, |
| $this->yieldBuffer =''; | ||
| return$this->yield("'$yieldBuffer'",$context); | ||
| return$this->yield('"'.$yieldBuffer.'"',$context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
be careful. Using double quotes unlocks many escape sequences in PHP, andyieldString does not perform any escaping (except for$ now) so it expects the caller to pass a string being the content of a string literal, instead of being the content of the string it wants to yield.
Are we sure that there is nothing impacted by that ? We might have injection of escape sequences rather than injection of interpolation.
Btw,yieldString might need to be renamed if it is not about yielding the given string (taking care internally of turning it into a PHP string literal with proper escaping) but about yielding some string literal instead (where the caller has to deal with escaping), in order to reduce the risk of confusion for future contributors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Are we sure that there is nothing impacted by that ? We might have injection of escape sequences rather than injection of interpolation.
I'm deliberately using an interpolated string because it allows yielding larger, already-assembled chunks instead of many smaller fragments, which improves performance by reducing the number of yield operations.
Btw,
yieldStringmight need to be renamed [...]
The function is now calledyieldInterpolatedString to make it explicit that the string is not a literal (and will be escaped)
yieldStringdoes not perform any escaping (except for $ now) [...]
I now escape the string like the following:
$string =addcslashes($string,"\\\"\n\r\t\v\e\f");if ($escapeDollar) {$string =addcslashes($string,'$');}
(pulled for the table fromphp documentation
soyuka commentedJun 27, 2025
I see your point, I'm convinced the null type may be required in JSON. I still think that the default for this component should be that it's disabled by default. From an architecture point of view, when it comes to resource oriented APIs or JSON-LD, we don't recommend the use of null values, it adds unnecessary noise and adds complexity to front ends. |
null valueinclude_nullable_properties optionmtarld commentedJul 1, 2025
You're right@stof, omitting a property from the JSON or setting it to |
20821d6 to48b755dComparealexandre-daubois commentedJul 1, 2025
Ah yes! I wanted to comment back to suggest an opt-in to include null values but forgot about it. It seems to be the good compromise! |
soyuka commentedJul 1, 2025
What about |
mtarld commentedJul 1, 2025
This might be misleading, IMO. |
alexandre-daubois commentedJul 1, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
|
mtarld commentedJul 1, 2025
I see your point, therefore what about |
alexandre-daubois commentedJul 1, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Both removes the ambiguity, definitely better! Maybe the first one is more accurate (because you include the property and its value, not only the value) |
include_nullable_properties optioninclude_null_properties optionmtarld commentedJul 2, 2025
I went for |
nicolas-grekas commentedJul 24, 2025
rebase needed |
Uh oh!
There was an error while loading.Please reload this page.
nicolas-grekas commentedJul 24, 2025
Thank you@mtarld. |
d3a0df0 intosymfony:7.4Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
A really common use case in APIs is to be able to skip properties with
nullvalue (for instance, this use case is required to make the JsonStreamer component working with API Platform).This PR adds the optionskip_null_propertiesthat allow to skipnullproperties during stream writing.This PR skips properties withnullnvalues during stream writing.This PR adds the option
include_null_propertiesthat allow to encodenullproperties during stream writing.This requires to define dynamic object prefixes (such as
''and','). That's why the PHP generated code has been updated to merge as many prefixes as possible in raw strings and therefore yield a bit bigger chunks.