-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[JsonStreamer] Skip properties with null
value
#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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.4
Are you sure you want to change the base?
Conversation
src/Symfony/Component/JsonStreamer/Tests/Fixtures/stream_writer/null.php
Show resolved
Hide resolved
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, |
2b0f89a
to
7982333
Compare
I agree with @soyuka; in my opinion, the less noise we have, the better. I updated the PR in that way. |
null
valuenull
value
Let's target 7.3 then |
This PR needs #60544, which has been merged in 7.4. |
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
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) |
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, |
@@ -304,7 +345,7 @@ private function flushYieldBuffer(array $context): string | |||
$yieldBuffer = $this->yieldBuffer; | |||
$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, and yieldString
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
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. |
A really common use case in APIs is to be able to skip properties with
null
value (for instance, this use case is required to make the JsonStreamer component working with API Platform).This PR adds the optionskip_null_properties
that allow to skipnull
properties during stream writing.This PR skips properties with
nulln
values 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.