Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Serializer] Fix denormalizing empty string intoobject|null parameter#52172
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
object|null parameterOskarStark commentedOct 20, 2023
Looks like a bugfix to me, in this case 🎯 |
nicolas-grekas commentedOct 20, 2023
This would need tests for sure. But I'm no expert in the Serializer component so I'd appreciate if someone more familiar with it could review. |
Jeroeny commentedOct 23, 2023
I've added tests. I also discovered that |
Jeroeny commentedOct 23, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Another interesting failure scenario: // this fails$this->serializer->denormalize(['element' =>'0'], Example::class, context: [AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT =>true]);class Example {publicfunction__construct(publicArgs|int$element) {}}class Args {publicfunction__construct(publicUuid$nested,privatestring$test)}// [Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException]// Cannot create an instance of "App\Args" from serialized data because its constructor requires the following parameters to be present : "$nested", "$test".// whereas this returns an instance succesfully$this->serializer->denormalize(['element' =>'0'], Test::class, context: [AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT =>true]);class Test {publicfunction__construct(publicUuid|int$element)} Because the first denormalization throws I'm not sure what the reasoning was here or if this is actually not intended. It could be fixed by adding |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
nicolas-grekas commentedNov 20, 2023
Let's resume this one? Is this a bugfix as it's documented in the PR description? If yes can you please target the lowest affected branch? |
Jeroeny commentedNov 21, 2023
Applied feedback and targeting |
nicolas-grekas commentedNov 21, 2023
Isn't 5.4 affected also? |
Jeroeny commentedNov 21, 2023
Eh yes, I was looking into the wrong part when checking that branch. Now based on 5.4. |
nicolas-grekas commentedNov 21, 2023
Parse error on PHP 7.2 ;) |
1a725d3 to1df027aCompareJeroeny commentedNov 23, 2023
Serializer tests are ok now |
src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
nicolas-grekas commentedNov 24, 2023
(rebase needed ;) ) |
nicolas-grekas commentedNov 24, 2023
Thank you@Jeroeny. |
This PR was merged into the 5.4 branch.Discussion----------[Serializer] fix nullable int cannot be serialized| Q | A| ------------- | ---| Branch? | 5.4| Bug fix? | yes| New feature? | no| Deprecations? | no| License | MITHello,previous to [this PR](#52172) such XML could be deserialized correctly, setting null in the value:```xml<?xml version="1.0" encoding="UTF-8"?><DummyNullableInt><value/></DummyNullableInt>``````phpclass DummyNullableInt{ public function __construct( public int|null $value = null ) {}}```but now it creates the following error:```Uninitialized string offset 0/home/nikophil/works/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:495/home/nikophil/works/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:630/home/nikophil/works/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php:377/home/nikophil/works/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:246/home/nikophil/works/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:346```I looked for any issue or PR mentioning this problem, but couldn't find it. So here is a fixping `@Jeroeny`Commits-------5d62dea [Serializer] fix regression where nullable int cannot be serialized
Uh oh!
There was an error while loading.Please reload this page.
The
AbstractObjectNormalizerhandles this for bool, int and float types. But if a parameter is typedObject|null, a serialization and then deserialization will fail.This will throw:
Reproducer:https://github.com/Jeroeny/reproduce/blob/xmlnull/src/Test.php