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 using DateIntervalNormalizer with union types #51992

Merged
merged 1 commit into from Oct 25, 2023

Conversation

Jeroeny
Copy link
Contributor

@Jeroeny Jeroeny commented Oct 11, 2023

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
License MIT

The union logic of AbstractObjectNormalizer tries to denormalize each union type and catches NotNormalizableValueException (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 a DateInterval|DateTimeInterface type, it will fail because the DateIntervalNormalizer throws UnexpectedValueException instead of NotNormalizableValueException.
Denormalizing a DateInterval into DateTimeInterface|DateInterval does work, because DateTimeNormalizer throws NotNormalizableValueException. I also checked some other Object-specific normalizers like Uid, Problem, DateTimeZone, DataUri, BackedEnum, they are using NotNormalizableValueException.

See reproducer: https://github.com/Jeroeny/reproduce/tree/union/src

@Jeroeny Jeroeny requested a review from dunglas as a code owner October 11, 2023 13:06
@carsonbot carsonbot added this to the 5.4 milestone Oct 11, 2023
@OskarStark OskarStark changed the title [Serializer] Fix using DateIntervalNormalizer with union types [Serializer] Fix using DateIntervalNormalizer with union types Oct 11, 2023
@nicolas-grekas
Copy link
Member

Could it be possible to add a test case that covers the situation you describe, ie that uses a union type?

@Jeroeny
Copy link
Contributor Author

Jeroeny commented Oct 12, 2023

@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 DateIntervalNormalizerTest now does assert the exception is thrown for this normalizer.

@nicolas-grekas
Copy link
Member

/cc @HypeMC maybe for a review? 🙏

@HypeMC
Copy link
Contributor

HypeMC commented Oct 19, 2023

@nicolas-grekas Sure, I'll take a look sometime today.

Copy link
Contributor

@HypeMC HypeMC left a 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.

@fabpot
Copy link
Member

fabpot commented Oct 25, 2023

Thank you @Jeroeny.

@fabpot fabpot merged commit 3f92492 into symfony:5.4 Oct 25, 2023
9 of 11 checks passed
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

7 participants