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

Add new serializer empty_data feature doc#8914

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
javiereguiluz merged 3 commits intosymfony:4.1fromNek-:add-feature-25493
Jun 27, 2018

Conversation

@Nek-
Copy link
Contributor

This is the documentation related to the following feature:symfony/symfony#25493

@javiereguiluzjaviereguiluz added the Waiting Code MergeDocs for features pending to be merged labelDec 23, 2017
@xabbuhxabbuh added this to the4.1 milestoneJan 2, 2018
@Nek-Nek-force-pushed theadd-feature-25493 branch 2 times, most recently fromc4d61f9 to2ba3fddCompareJanuary 15, 2018 06:52
----------------------

Value Objets are difficult to handle because they often require parameters in the constructor. If the input omit one
of theses parameters the serializer will throw an exeception because it can't create the object.
Copy link
Member

Choose a reason for hiding this comment

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

typoexeception

symfony-splitter pushed a commit to symfony/serializer that referenced this pull requestJan 19, 2018
…ption for denormalization (Nek-)This PR was squashed before being merged into the 4.1-dev branch (closes #25493).Discussion----------[Serializer] `default_constructor_arguments` context option for denormalization| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | (there is no RFC for this)| License       | MIT| Doc PR        |symfony/symfony-docs#8914## ProblemsIn the case you want to deserialize value-objects, if all the data required by its constructor are **not** given as input, the serializer will throw a simple `RuntimeException` exception. This makes impossible to catch it. (as current fix on my projects I use exception message to be sure to catch the good one X.x")The second problem is a missing feature to fill the required object with an empty one. This needs to be defined by the user because the serializer can't guess how to build it.Here is a project that exposes the problem of the current behavior.https://github.com/Nek-/api-platform-value-object## Solutions suggestedI suggest a solution in 2 parts because the second part is more touchy.1. Replace the current exception by a new specific one2. Add a new `empty_data` option to the context of deserialization so you can specify data for objects impossible to instantiate, this is great because the serializer no more throw exception and the validator can then work as expected and send violations to the user. This solution is inspired by forms solutions to fix the issue with value objectsHere is what you can do with this feature:```phpclass DummyValueObject{    public function __construct($foo, $bar) { $this->foo = $foo; $this->bar = $bar; }}$empty = new DummyValueObject('', '');$result = $normalizer->denormalize(['foo' => 'Hello'], DummyValueObject::class, 'json', [    'empty_data' => [        DummyValueObject::class => $empty,    ],]);// It's impossible to construct a DummyValueObject with only "foo" value. So the serializer// will replace it with the given empty data```There are 2 commits so I can quickly provide you only the first point if you want. Hope you'll like this.## Solution after discussion1. New exception `MissingConstructorArgumentsException`2. New context option `default_constructor_arguments````phpclass DummyValueObject{    public function __construct($foo, $bar) { $this->foo = $foo; $this->bar = $bar; }}$result = $normalizer->denormalize(['foo' => 'Hello'], DummyValueObject::class, 'json', [    'default_constructor_arguments' => [        DummyValueObject::class => ['foo' => '', 'bar' => ''],    ],]);// DummyValueObject is contructed with the given `foo` and empty `bar````Commits-------1523a85542 [Serializer]  context option for denormalization
fabpot added a commit to symfony/symfony that referenced this pull requestJan 19, 2018
…ption for denormalization (Nek-)This PR was squashed before being merged into the 4.1-dev branch (closes#25493).Discussion----------[Serializer] `default_constructor_arguments` context option for denormalization| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | (there is no RFC for this)| License       | MIT| Doc PR        |symfony/symfony-docs#8914## ProblemsIn the case you want to deserialize value-objects, if all the data required by its constructor are **not** given as input, the serializer will throw a simple `RuntimeException` exception. This makes impossible to catch it. (as current fix on my projects I use exception message to be sure to catch the good one X.x")The second problem is a missing feature to fill the required object with an empty one. This needs to be defined by the user because the serializer can't guess how to build it.Here is a project that exposes the problem of the current behavior.https://github.com/Nek-/api-platform-value-object## Solutions suggestedI suggest a solution in 2 parts because the second part is more touchy.1. Replace the current exception by a new specific one2. Add a new `empty_data` option to the context of deserialization so you can specify data for objects impossible to instantiate, this is great because the serializer no more throw exception and the validator can then work as expected and send violations to the user. This solution is inspired by forms solutions to fix the issue with value objectsHere is what you can do with this feature:```phpclass DummyValueObject{    public function __construct($foo, $bar) { $this->foo = $foo; $this->bar = $bar; }}$empty = new DummyValueObject('', '');$result = $normalizer->denormalize(['foo' => 'Hello'], DummyValueObject::class, 'json', [    'empty_data' => [        DummyValueObject::class => $empty,    ],]);// It's impossible to construct a DummyValueObject with only "foo" value. So the serializer// will replace it with the given empty data```There are 2 commits so I can quickly provide you only the first point if you want. Hope you'll like this.## Solution after discussion1. New exception `MissingConstructorArgumentsException`2. New context option `default_constructor_arguments````phpclass DummyValueObject{    public function __construct($foo, $bar) { $this->foo = $foo; $this->bar = $bar; }}$result = $normalizer->denormalize(['foo' => 'Hello'], DummyValueObject::class, 'json', [    'default_constructor_arguments' => [        DummyValueObject::class => ['foo' => '', 'bar' => ''],    ],]);// DummyValueObject is contructed with the given `foo` and empty `bar````Commits-------1523a85 [Serializer]  context option for denormalization
@xabbuhxabbuh added Serializer and removed Waiting Code MergeDocs for features pending to be merged labelsJan 22, 2018
@Nek-
Copy link
ContributorAuthor

Nek- commentedFeb 1, 2018

@fabpot fixed

Copy link
Member

@javiereguiluzjaviereguiluz left a comment

Choose a reason for hiding this comment

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

@Nek- thanks for contributing this doc. I liked it a lot and everything is well explained. I just made a minor reword to focus more on the general use case ("Handling Constructor Arguments") instead of the "value object" detail. Thanks!

@xabbuhxabbuh changed the base branch frommaster to4.1May 8, 2018 08:43
Handling Constructor Arguments
------------------------------

If the constructor of a class defines arguments, as usually happens with
Copy link
Member

@dunglasdunglasMay 16, 2018
edited
Loading

Choose a reason for hiding this comment

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

It's not exact: the serializer deals well with constructor arguments, if they are present in the data to deserialize.

Thedefault_constructor_arguments is useful only when the value has not been provided in the data to deserialize.

Nek- reacted with thumbs up emoji
@Nek-
Copy link
ContributorAuthor

Nek- commentedJun 4, 2018

Fixed and rebased. 👍

@javiereguiluz
Copy link
Member

@Nek- I'm sorry it took us so long to merge. Thanks!

HeahDude reacted with thumbs up emoji

@javiereguiluzjaviereguiluz merged commit9f31bbb intosymfony:4.1Jun 27, 2018
javiereguiluz added a commit that referenced this pull requestJun 27, 2018
…uiluz)This PR was merged into the 4.1 branch.Discussion----------Add new serializer empty_data feature docThis is the documentation related to the following feature:symfony/symfony#25493Commits-------9f31bbb Fix not precise enough sentenceb0d3fe8 Minor reword5a48201 Add new serializer empty_data feature doc
@Nek-Nek- deleted the add-feature-25493 branchJune 27, 2018 14:19
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot requested changes

@javiereguiluzjaviereguiluzjaviereguiluz approved these changes

+1 more reviewer

@dunglasdunglasdunglas left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.1

Development

Successfully merging this pull request may close these issues.

6 participants

@Nek-@javiereguiluz@fabpot@dunglas@xabbuh@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp