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 #45838 [Serializer] Fix denormalizing union types (T-bond)
This PR was merged into the 4.4 branch.
Discussion
----------
[Serializer] Fix denormalizing union types
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | yes
| New feature? | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets | Fix#45818
| License | MIT
| Doc PR | -
<!--
Replace this notice by a short README for your feature/bugfix.
This will help reviewers and should be a good start for the documentation.
Additionally (see https://symfony.com/releases):
- Always add tests and ensure they pass.
- Bug fixes must be submitted against the lowest maintained branch where they apply
(lowest branches are regularly merged to upper ones so they get the fixes too.)
- Features and deprecations must be submitted against the latest branch.
- Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
- Never break backward compatibility (see https://symfony.com/bc).
-->
`@nicolas`-grekas Replaces: #45824 as I accidentally registerred a lot of people to the notifier list when changing target branch.
Note about difference between docblock vs PHP 8 union types:
With PHP 8 union types, the resolved `$types` array was always (when I checked) in the same order, no matter the type definition.
So these two things: ```public null|boolean|DateTime``` and ```public DateTime|boolean|null``` would **always** generate an array, where the **first** item was `DateTime`.
With PHP docblock the `$tpyes` array order depends of the order of the documentation.
So the `* `@var` \DateTime|bool|null` would resolve to an array with `DateTime` as **first** item, but the `* `@var` null|bool|\DateTime` would resolve to the `DateTime` being the **last** element.
Commits
-------
98acd62 [Serializer] Fix denormalizing union types
if (!$this->serializerinstanceof DenormalizerInterface) {
460
-
thrownewLogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer.', $attribute, $class));
459
+
// This try-catch should cover all NotNormalizableValueException (and all return branches after the first
460
+
// exception) so we could try denormalizing all types of an union type. If the target type is not an union
461
+
// type, we will just re-throw the catched exception.
462
+
// In the case of no denormalization succeeds with an union type, it will fall back to the default exception
463
+
// with the acceptable types list.
464
+
try {
465
+
if (Type::BUILTIN_TYPE_OBJECT === $builtinType) {
466
+
if (!$this->serializerinstanceof DenormalizerInterface) {
467
+
thrownewLogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer.', $attribute, $class));
0 commit comments