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] 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

Merged
nicolas-grekas merged 1 commit intosymfony:5.4fromJeroeny:fixnullunion
Nov 24, 2023

Conversation

@Jeroeny
Copy link
Contributor

@JeroenyJeroeny commentedOct 19, 2023
edited
Loading

QA
Branch?5.4
Bug fix?yes
New feature?no
Deprecations?no
LicenseMIT

In XML and CSV all basic datatypes are represented as strings

TheAbstractObjectNormalizer handles this for bool, int and float types. But if a parameter is typedObject|null, a serialization and then deserialization will fail.

This will throw:

finalclass Xml{publicfunction__construct(publicUuid|null$element =null)    {    }}$test =newXml(null);$serialized =$this->serializer->serialize($test, XmlEncoder::FORMAT);$deserialized =$this->serializer->deserialize($serialized, Xml::class, XmlEncoder::FORMAT);
  [Symfony\Component\Serializer\Exception\NotNormalizableValueException]         The data is not a valid "Symfony\Component\Uid\Uuid" string representation.

Reproducer:https://github.com/Jeroeny/reproduce/blob/xmlnull/src/Test.php

@carsonbotcarsonbot added this to the6.4 milestoneOct 19, 2023
@OskarStarkOskarStark changed the title[Serializer] Fix denormalizing empty string into object|null parameter[Serializer] Fix denormalizing empty string intoobject|null parameterOct 20, 2023
@OskarStark
Copy link
Contributor

Looks like a bugfix to me, in this case 🎯6.3, but let's wait for advice of@nicolas-grekas

@nicolas-grekas
Copy link
Member

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

I've added tests. I also discovered that$builtinType isn't reset in the for-loop in some cases. Which means aObject|bool type wouldn't hit this switch-case:https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php#L471 (The added tests also cover this).

@Jeroeny
Copy link
ContributorAuthor

Jeroeny commentedOct 23, 2023
edited
Loading

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 throwsMissingConstructorArgumentsException, which goes before the return onDISABLE_TYPE_ENFORCEMENT.
The second throwsNotNormalizableValueException, but is caught and will return0 because ofDISABLE_TYPE_ENFORCEMENT.

I'm not sure what the reasoning was here or if this is actually not intended.

It could be fixed by adding$hasTypeMismatch = $builtinType !== Type::BUILTIN_TYPE_OBJECT here:
https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php#L565. To establish that it did not throw for at least one of the$types.
And the later using that to return beforeMissingConstructorArgumentsException. (Or always return before that exception when DISABLE_TYPE_ENFORCEMENT is true?)

@nicolas-grekas
Copy link
Member

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

Applied feedback and targeting6.3

@nicolas-grekasnicolas-grekas modified the milestones:7.1,6.3Nov 21, 2023
@nicolas-grekas
Copy link
Member

Isn't 5.4 affected also?

@Jeroeny
Copy link
ContributorAuthor

Eh yes, I was looking into the wrong part when checking that branch. Now based on 5.4.

@nicolas-grekas
Copy link
Member

Parse error on PHP 7.2 ;)

@JeroenyJeroenyforce-pushed thefixnullunion branch 3 times, most recently from1a725d3 to1df027aCompareNovember 23, 2023 10:27
@Jeroeny
Copy link
ContributorAuthor

Serializer tests are ok now

@nicolas-grekas
Copy link
Member

(rebase needed ;) )

Jeroeny reacted with thumbs up emoji

@nicolas-grekas
Copy link
Member

Thank you@Jeroeny.

Jeroeny reacted with laugh emoji

@nicolas-grekasnicolas-grekas merged commit1bc8d26 intosymfony:5.4Nov 24, 2023
nicolas-grekas added a commit that referenced this pull requestDec 4, 2023
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
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@dunglasdunglasAwaiting requested review from dunglasdunglas is a code owner

@jderussejderusseAwaiting requested review from jderusse

@mtarldmtarldAwaiting requested review from mtarld

@nicolas-grekasnicolas-grekasAwaiting requested review from nicolas-grekas

Assignees

No one assigned

Projects

None yet

Milestone

5.4

Development

Successfully merging this pull request may close these issues.

6 participants

@Jeroeny@OskarStark@nicolas-grekas@mtarld@yceruto@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp