Skip to content
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

[Serializer] fix: deserialization union type of enum #47803

Closed
wants to merge 1 commit into from

Conversation

Gwemox
Copy link
Contributor

@Gwemox Gwemox commented Oct 6, 2022

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets #47797

Fix deserialization of union type of BackedEnum

if ($missingConstructorArgumentException) {
throw $missingConstructorArgumentException;
if ($exception) {
throw $exception;
Copy link
Member

@stof stof Oct 6, 2022

Choose a reason for hiding this comment

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

This will now re-throw a NotNormalizableValueException before the DISABLE_TYPE_ENFORCEMENT check

Copy link
Contributor Author

@Gwemox Gwemox Oct 6, 2022

Choose a reason for hiding this comment

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

@stof I did not understand quite well.
Is it better not to store the NotNormalizableValueException?

Like this :

if (!$exception && !$e instanceof NotNormalizableValueException) {
    $exception = $e;
}

@carsonbot carsonbot changed the title fix: deserialization union type of enum [Serializer] fix: deserialization union type of enum Oct 8, 2022
@Gwemox Gwemox requested review from stof and removed request for dunglas October 10, 2022 07:59
@Gwemox
Copy link
Contributor Author

Gwemox commented Oct 24, 2022

@stof can you review please ?

Copy link
Member

@stof stof left a comment

Choose a reason for hiding this comment

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

Not sure the right fix is to swallow other kind of exceptions in the component. See my comment about using NotNormalizableValueException

@@ -63,7 +63,7 @@ public function denormalize($data, string $type, string $format = null, array $c

try {
return $type::from($data);
} catch (\ValueError $e) {
} catch (\ValueError|\TypeError $e) {
Copy link
Member

Choose a reason for hiding this comment

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

To me, the TypeError should result in a NotNormalizableValueException, as it is a more precise case of the check above (as an integer-backed enum won't support strings but the normalizer does not distinguish the backing type)

Copy link
Contributor Author

@Gwemox Gwemox Oct 26, 2022

Choose a reason for hiding this comment

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

@stof Now, TypeError result in a NotNormalizableValueException.

I'm not sure how to throw the exception, your knowledge is welcome.

@@ -63,7 +63,7 @@ public function denormalize($data, string $type, string $format = null, array $c

try {
return $type::from($data);
} catch (\ValueError $e) {
} catch (\ValueError|\TypeError $e) {
throw new InvalidArgumentException('The data must belong to a backed enumeration of type '.$type);
Copy link
Member

Choose a reason for hiding this comment

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

This case should probably use NotNormalizableValueException even for invalid strings btw.

Copy link
Member

Choose a reason for hiding this comment

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

The exception was changed here #47128

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this commit is the source of the bug.

Copy link
Member

Choose a reason for hiding this comment

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

Yet it was also fixing a bug, please make sure that test added in #47128 keeps passing and/or that the behavior it fixed remains the expected one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All Serializer-related tests, including those in this commit, pass.

@Gwemox Gwemox requested review from chalasr and stof and removed request for chalasr November 8, 2022 16:06
@Gwemox Gwemox requested review from chalasr and removed request for stof February 9, 2023 16:44
@Gwemox
Copy link
Contributor Author

Gwemox commented Feb 24, 2023

@nicolas-grekas can you request available reviewers to review this ?

@fabpot
Copy link
Member

fabpot commented Aug 26, 2023

Should be fixed by #51475

@fabpot fabpot closed this Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants