symfony icon indicating copy to clipboard operation
symfony copied to clipboard

[Serializer] Add support for union collection value types in `ArrayDenormalizer`

Open Jeroeny opened this issue 9 months ago • 3 comments

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

Currently only the following array types combination is supported: array<MyObject>|array<Another>. This results in arrays with only 1 type of objects.

This PR aims to add support for arrays where values can be either of those, like union types: array<MyObject|Another>.

I've currently tested is successfully against the following types:

  • DateTimeInterface[]
  • array<array<DateTimeInterface|DateInterval|string>>
  • array<DateTimeInterface>|array<DateInterval>
  • array<DateTimeInterface|DateInterval>
  • array<DateTimeInterface|null>

See also https://github.com/Jeroeny/reproduce/blob/mixedarray/src/Test.php

Jeroeny avatar Oct 12 '23 13:10 Jeroeny

Anyone with more background on Serializer would be willing to review this PR? (note that it should target 7.1 for now, maybe 7.2 if we can't make it since we're in feature freeze now). /cc @mtarld maybe? Anyone else?

nicolas-grekas avatar Apr 12 '24 12:04 nicolas-grekas

As https://github.com/symfony/symfony/pull/53160 has been merged, the validateAndDenormalize method is now using Type of TypeInfo component, which can make this patch easier. So, this PR has to be rebased and updated accordingly.

mtarld avatar Apr 12 '24 13:04 mtarld

Rebased onto 7.1 and description targeting 7.2.

The new validateAndDenormalizeLegacy vs validateAndDenormalize` are a bit complex, so not sure yet how to apply the changes to the method using the new types.

Jeroeny avatar May 06 '24 11:05 Jeroeny