You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bug #51992 [Serializer] Fix using DateIntervalNormalizer with union types (Jeroeny)
This PR was squashed before being merged into the 5.4 branch.
Discussion
----------
[Serializer] Fix using `DateIntervalNormalizer` with union types
| 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
Commits
-------
c727a2f [Serializer] Fix using `DateIntervalNormalizer` with union types
thrownewInvalidArgumentException(sprintf('Data expected to be a string, "%s" given.', get_debug_type($data)));
78
+
throwNotNormalizableValueException::createForUnexpectedDataType('Data expected to be a string.', $data, ['string'], $context['deserialization_path'] ?? null, true);
80
79
}
81
80
82
81
if (!$this->isISO8601($data)) {
83
-
thrownewUnexpectedValueException('Expected a valid ISO 8601 interval string.');
82
+
throwNotNormalizableValueException::createForUnexpectedDataType('Expected a valid ISO 8601 interval string.', $data, ['string'], $context['deserialization_path'] ?? null, true);
0 commit comments