-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Allow AbstractNormalizer to use null for non-optional nullable constructor parameters without default value #40522
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Build failure seems to be unrelated, am I misreading it ? |
ro0NL
approved these changes
Mar 20, 2021
ro0NL
reviewed
Mar 20, 2021
src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Tests/Fixtures/NullableOptionalConstructorArgumentDummy.php
Outdated
Show resolved
Hide resolved
nicolas-grekas
approved these changes
Mar 22, 2021
@ro0NL @nicolas-grekas thank you. I hope it will land in next 5.2 release, I have to maintain a patch in the meantime. |
…constructor parameter denormalization when not present in input
I rebased on 4.4, where bugfixes should be merged. |
Oh, nice ! Thank you very much. |
maxhelias
approved these changes
Mar 26, 2021
chalasr
approved these changes
Mar 29, 2021
Thank you @pounard. |
nicolas-grekas
added a commit
that referenced
this pull request
May 5, 2023
…listed in the input (Christian Kolb) This PR was merged into the 6.3 branch. Discussion ---------- [Serializer] Add flag to require all properties to be listed in the input | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #49504 | License | MIT | Doc PR | symfony/symfony-docs#17979 The PR #40522 introduced a fallback for nullable properties to be set to `null` when they aren't provided as parameters. A drawback of that approach is that it easier for bugs to appear through typos or renamings of those properties. I think the current implementation makes perfect sense as a default. Therefore, this PR introduces a new context flag that prevents that fallback behaviour. This way nothing changes for existing systems, but for people wanting more control, it's possible to set a flag. ### Example ```php final class Product { public function __construct( public string $name, public ?int $costsInCent, ) { } } // This works and results in $costsInCent as null $product = $this->serializer->deserialize( '{"name": "foo"}', Product::class, JsonEncoder::FORMAT, ); // When using the flag, only the following JSON is valid $product = $this->serializer->deserialize( '{"name": "foo", "costsInCent": null}', Product::class, JsonEncoder::FORMAT, [ AbstractNormalizer::PREVENT_NULLABLE_FALLBACK => true, ], ); // This would result in an error due to missing parameters $product = $this->serializer->deserialize( '{"name": "foo"}', Product::class, JsonEncoder::FORMAT, [ AbstractNormalizer::PREVENT_NULLABLE_FALLBACK => true, ], ); ``` Commits ------- d62410a [Serializer] Add flag to require all properties to be listed in the input
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Serializer component AbstractNormalizer attemps to guess constructor parameters, and falls back using default values when possible. Yet, it misses one use case: nullable non-optional parameter with value not being present in incoming input, case in which null is a valid value, not the default one, yet still valid.
This PR introduce a two-line fix that forcefully set null as value for missing from input non-optional nullable constructor parameters values.