-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] fix: deserialization union type of enum #47803
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
Conversation
if ($missingConstructorArgumentException) { | ||
throw $missingConstructorArgumentException; | ||
if ($exception) { | ||
throw $exception; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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;
}
@stof can you review please ? |
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@nicolas-grekas can you request available reviewers to review this ? |
Should be fixed by #51475 |
Fix deserialization of union type of BackedEnum