Fix serialisation/hydration symmetry of list items with registered casters/serialisers for their types#94
Open
lcobucci wants to merge 2 commits into
Open
Conversation
Although we're able to properly serialise lists of objects that have custom serialisers, the same doesn't happen during hydration. This adds an integration test to illustrate the scenario and expected behaviour. Signed-off-by: Luís Cobucci <lcobucci@gmail.com>
This fixes the symmetry issue in hydrating list items that have registered casters for their types. Signed-off-by: Luís Cobucci <lcobucci@gmail.com>
lcobucci
commented
May 28, 2026
| $casters[] = [$attributeName, $attribute->getArguments()]; | ||
| $args = $attribute->getArguments(); | ||
|
|
||
| if (is_a($attributeName, PropertyCasters\CastListToType::class, true) ) { |
Contributor
Author
There was a problem hiding this comment.
@frankdejonge trade-off wise, this was the least worse I could arrive at and am 100% open to ideas on avoiding this (and BC-breaks).
lcobucci
commented
May 28, 2026
Comment on lines
+75
to
+94
| private function castViaCustomLogic(array $value, ObjectMapper $hydrator): array | ||
| { | ||
| $itemCaster = $this->itemCaster(); | ||
| assert($itemCaster instanceof PropertyCaster); | ||
|
|
||
| foreach ($value as $i => $item) { | ||
| $value[$i] = $itemCaster->cast($item, $hydrator); | ||
| } | ||
|
|
||
| return $value; | ||
| } | ||
|
|
||
| private function itemCaster(): ?PropertyCaster | ||
| { | ||
| if ($this->itemCasterConfig === null) { | ||
| return null; | ||
| } | ||
|
|
||
| return $this->itemCaster ??= new ($this->itemCasterConfig[0])(...$this->itemCasterConfig[1]); | ||
| } |
Contributor
Author
There was a problem hiding this comment.
@frankdejonge I also don't like those property/method names...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#[CastListToType]defers the serialisation of list items to$hydrator->serializeObject(), which will properly handle registered serialisers for list items based on their types. The hydration, however, breaks as it assumes the input will always be an array.That assumption is broken when dealing with things like
DateTimeImmutableorUuidInterface, as both https://github.com/EventSaucePHP/ObjectHydrator/blob/main/src/PropertyCasters/CastToDateTimeImmutable.php and https://github.com/EventSaucePHP/ObjectHydrator/blob/main/src/PropertyCasters/CastToUuid.php usestringto represent their input/output.Changing everything around
ObjectMapper#serializeObject()so that it can usemixedas input is a massive BC-break - and opens an undesired can of worms.This tries to address the symmetry issue with the minimal disruption on the public API, accepting an additional parameter on
#[CastListToType]that's always overridden to avoid unexpected injection of arguments.