Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Merged
nicolas-grekas merged 1 commit intosymfony:7.4frommtarld:feat/skip-nullable
Jul 24, 2025

Conversation

@mtarld
Copy link
Contributor

@mtarldmtarld commentedJun 6, 2025
edited
Loading

QA
Branch?7.4
Bug fix?no
New feature?yes
Deprecations?no
Issues
LicenseMIT

A really common use case in APIs is to be able to skip properties withnull 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 withnulln values during stream writing.
This PR adds the optioninclude_null_properties that allow to encodenull properties 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.

@soyuka
Copy link
Contributor

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,null values just add noise and have rather few impact when used in Javascript as they'll just get evaluated asundefined.

@mtarld
Copy link
ContributorAuthor

I agree with@soyuka; in my opinion, the less noise we have, the better. I updated the PR in that way.

@mtarldmtarld changed the title [JsonStreamer] Allow to skip properties withnull value [JsonStreamer] Skip properties withnull valueJun 7, 2025
@nicolas-grekas
Copy link
Member

Let's target 7.3 then

@mtarld
Copy link
ContributorAuthor

This PR needs#60544, which has been merged in 7.4.
Maybe we can backport it - as it is only internal changes?

Copy link
Member

@nicolas-grekasnicolas-grekas left a 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
Copy link
Member

Omitting a property from the JSON or setting it tonull is not equivalent, and might behave differently:

  • it is observable in Javascript, which hasundefined andnull as different types (and if you doa.myProp === null, it won't be true forundefined)
  • it is different in term of OpenAPI or json-schema specs (it is possible to have a nullable required property in such spec)
  • it might have an impact for parsers in other languages that might be strict about such properties

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 and Kocal reacted with thumbs up emoji

@alexandre-daubois
Copy link
Member

alexandre-daubois commentedJun 27, 2025
edited
Loading

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,skip_null_properties, looks better to me.

$this->yieldBuffer ='';

return$this->yield("'$yieldBuffer'",$context);
return$this->yield('"'.$yieldBuffer.'"',$context);
Copy link
Member

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

Copy link
ContributorAuthor

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,yieldString might need to be renamed [...]

The function is now calledyieldInterpolatedString to make it explicit that the string is not a literal (and will be escaped)

yieldString does 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
Copy link
Contributor

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.
Also as we're all about performances and band-width consumption when it comes to streaming I think that we should go with the less the better.

@mtarldmtarld changed the title [JsonStreamer] Skip properties withnull value [JsonStreamer] Addinclude_nullable_properties optionJul 1, 2025
@mtarld
Copy link
ContributorAuthor

You're right@stof, omitting a property from the JSON or setting it tonull is not the same.
Therefore, I reintroduced the option, but inverted it so that the default will be to omit null properties (the option becameinclude_nullable_properties).

alexandre-daubois reacted with thumbs up emoji

@mtarldmtarldforce-pushed thefeat/skip-nullable branch 2 times, most recently from20821d6 to48b755dCompareJuly 1, 2025 08:34
@alexandre-daubois
Copy link
Member

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

What aboutinclude_null_values instead ofinclude_nullable_properties?

alexandre-daubois and chalasr reacted with thumbs up emoji

@mtarld
Copy link
ContributorAuthor

This might be misleading, IMO.
For example, if we want to stream[null, null, null], withoutinclude_null_values, what can we expect?[null, null, null]?[]? "property" here is important to me.

@alexandre-daubois
Copy link
Member

alexandre-daubois commentedJul 1, 2025
edited
Loading

include_nullable_properties gives the impression that if set to false, properties thatcould be null will not be included in the output. That's why I'm a bit reluctant for this naming

@mtarld
Copy link
ContributorAuthor

I see your point, therefore what aboutinclude_null_properties, or maybeinclude_null_property_values?

@alexandre-daubois
Copy link
Member

alexandre-daubois commentedJul 1, 2025
edited
Loading

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)

@mtarldmtarld changed the title [JsonStreamer] Addinclude_nullable_properties option [JsonStreamer] Addinclude_null_properties optionJul 2, 2025
@mtarld
Copy link
ContributorAuthor

I went forinclude_null_properties.

alexandre-daubois and OskarStark reacted with rocket emoji

@nicolas-grekas
Copy link
Member

rebase needed
still 👍 on my side

@nicolas-grekas
Copy link
Member

Thank you@mtarld.

@nicolas-grekasnicolas-grekas merged commitd3a0df0 intosymfony:7.4Jul 24, 2025
11 checks passed
@mtarldmtarld deleted the feat/skip-nullable branchJuly 24, 2025 09:10
This was referencedOct 27, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@alexandre-dauboisalexandre-dauboisalexandre-daubois approved these changes

@stofstofAwaiting requested review from stof

+1 more reviewer

@soyukasoyukasoyuka approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

7.4

Development

Successfully merging this pull request may close these issues.

6 participants

@mtarld@soyuka@nicolas-grekas@stof@alexandre-daubois@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp