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

[Serializer] Add an option to skip null values#28661

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

Conversation

@dunglas
Copy link
Member

@dunglasdunglas commentedOct 1, 2018
edited by nicolas-grekas
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsn/a
LicenseMIT
Doc PRsymfony/symfony-docs#10427

Adds a new option to not serializenull values:

$dummy =newclass {public$foo;public$bar ='notNull';};$normalizer =newObjectNormalizer();$result =$normalizer->normalize($dummy,'json', ['skip_null_values' =>true]);// ['bar' => 'notNull']

This feature is the only missing part to addJSON Merge Patch support inAPI Platform.
It will also help supporting this RFC in all other projects using the Symfony Serializer.

thomasbisignani, ntomka, ragboyjr, fnash, rufinus, and mxkh reacted with thumbs up emoji
Copy link
Contributor

@ostroluckyostrolucky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Correct me if I am wrong, but utilizing $context for this most likely means you can't configure Serializer to enable this option by default? If somebody decides to go with this, they will want this to be enabled for whole instance by default. At least that happened in our case. App developer told us he does not want us to send null values and we could easily do that globally, since we used JMS serializer.

@nicolas-grekasnicolas-grekas added this to thenext milestoneOct 1, 2018
@dunglas
Copy link
MemberAuthor

@ostrolucky you're right. I'll also add a constructor option.

@dunglas
Copy link
MemberAuthor

@ostrolucky done

I also widened the visibility of$maxDepthHandler to protected, for consistency.

* @var callable|null
*/
private$maxDepthHandler;
protected$maxDepthHandler;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

should be private

/**
* @var bool
*/
protected$skipNullValues =false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

shouldbe private also (and the typehint looks useless to me, isn't it?)

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

As it's an abstract class, making it private allows to reuse this property in child classes... and we need to do it in API Platform.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

that's still possible to interact with by decorating the skipNullValues method
protected properties are very hard to deprecate, that's why we really prefer not introducing new ones

Copy link
MemberAuthor

@dunglasdunglasOct 2, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

We need to access to the value too... in the hot path.
But I agree with your point, and it's why we need to improve the general design of this component.

As a compromise, can we keep it protected but mark it@internal? We'll use it "as is" in API Platform, and if it breaks at some point... we'll upgrade!

@dunglas
Copy link
MemberAuthor

Last commit has been reverted, after discussing with@nicolas-grekas we had a better idea: we'll introduce soon an option to be able to easily set a default context.

@nicolas-grekas
Copy link
Member

We just talked with@dunglas on Slack: he's going to make another PR that will provide a$contextDefaults argument to the constructor, instead of any added setter or extra argument.

@nicolas-grekasnicolas-grekasforce-pushed theserializer-skip-null-values branch from0e158f7 tod3c5055CompareOctober 2, 2018 15:09
@nicolas-grekas
Copy link
Member

Thank you@dunglas.

@nicolas-grekasnicolas-grekas merged commitd3c5055 intosymfony:masterOct 2, 2018
nicolas-grekas added a commit that referenced this pull requestOct 2, 2018
This PR was squashed before being merged into the 4.2-dev branch (closes#28661).Discussion----------[Serializer] Add an option to skip null values| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      |no| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->| BC breaks?    | no     <!-- seehttps://symfony.com/bc -->| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->| Fixed tickets | n/a   <!-- #-prefixed issue number(s), if any -->| License       | MIT| Doc PR        | todoAdds a new option to not serialize `null` values:```php$dummy = new class {    public $foo;    public $bar = 'notNull';};$normalizer = new ObjectNormalizer();$result = $normalizer->normalize($dummy, 'json', ['skip_null_values' => true]);// ['bar' => 'notNull']```This feature is the only missing part to add [JSON Merge Patch](https://tools.ietf.org/html/rfc7386) support in [API Platform](https://api-platform.com).It will also help supporting this RFC in all other projects using the Symfony Serializer.Commits-------d3c5055 [Serializer] Add an option to skip null values
@dunglasdunglas deleted the serializer-skip-null-values branchOctober 2, 2018 15:12
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull requestOct 3, 2018
This PR was merged into the master branch.Discussion----------[Serializer] Add an option to skip null valuessymfony/symfony#28661Commits-------6349bd3 [Serializer] Add an option to skip null values
@nicolas-grekasnicolas-grekas modified the milestones:next,4.2Nov 1, 2018
This was referencedNov 3, 2018
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

+1 more reviewer

@ostroluckyostroluckyostrolucky approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.2

Development

Successfully merging this pull request may close these issues.

4 participants

@dunglas@nicolas-grekas@ostrolucky@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp