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 usingDateIntervalNormalizer with union types#51992
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
DateIntervalNormalizer with union typesnicolas-grekas commentedOct 12, 2023
Could it be possible to add a test case that covers the situation you describe, ie that uses a union type? |
src/Symfony/Component/Serializer/Normalizer/DateIntervalNormalizer.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Jeroeny commentedOct 12, 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.
@nicolas-grekas I think this tests the union type with a failure first:https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/Serializer/Tests/SerializerTest.php#L740 But it won't testevery normalizer for throwing the correct exception. And secondly the |
nicolas-grekas commentedOct 17, 2023
/cc@HypeMC maybe for a review? 🙏 |
HypeMC commentedOct 19, 2023
@nicolas-grekas Sure, I'll take a look sometime today. |
HypeMC left a comment
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.
Looks good, tried it out and it works as expected.
src/Symfony/Component/Serializer/Normalizer/DateIntervalNormalizer.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
fabpot commentedOct 25, 2023
Thank you@Jeroeny. |
Uh oh!
There was an error while loading.Please reload this page.
The union logic of
AbstractObjectNormalizertries to denormalize each union type and catchesNotNormalizableValueException(among some other exceptions). If a non-catching exception is thrown, denormalization fails on that first type, while a later type might have succeeded.If I try to denormalize a
DateTimeInterfacevalue into aDateInterval|DateTimeInterfacetype, it will fail because theDateIntervalNormalizerthrowsUnexpectedValueExceptioninstead ofNotNormalizableValueException.Denormalizing a
DateIntervalintoDateTimeInterface|DateIntervaldoes work, becauseDateTimeNormalizerthrowsNotNormalizableValueException. I also checked some other Object-specific normalizers likeUid,Problem,DateTimeZone,DataUri,BackedEnum, they are usingNotNormalizableValueException.See reproducer:https://github.com/Jeroeny/reproduce/tree/union/src