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 using DateIntervalNormalizer
with union types
#51992
Conversation
230ba6b
to
4cb1d2c
Compare
DateIntervalNormalizer
with union types
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
Outdated
Show resolved
Hide resolved
@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 test every normalizer for throwing the correct exception. And secondly the |
4cb1d2c
to
e542f9e
Compare
/cc @HypeMC maybe for a review? 🙏 |
@nicolas-grekas Sure, I'll take a look sometime today. |
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
Outdated
Show resolved
Hide resolved
0734963
to
c727a2f
Compare
Thank you @Jeroeny. |
The union logic of
AbstractObjectNormalizer
tries 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
DateTimeInterface
value into aDateInterval|DateTimeInterface
type, it will fail because theDateIntervalNormalizer
throwsUnexpectedValueException
instead ofNotNormalizableValueException
.Denormalizing a
DateInterval
intoDateTimeInterface|DateInterval
does work, becauseDateTimeNormalizer
throwsNotNormalizableValueException
. 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