Skip to content

[LiveComponent] Fix collections hydration with serializer #1583

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/LiveComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.17.0

- Add `modifier` option in `LiveProp` so options can be modified at runtime.
- Fix collections hydration with serializer in LiveComponents

## 2.16.0

Expand Down
15 changes: 13 additions & 2 deletions src/LiveComponent/src/LiveComponentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,22 @@ public function hydrateValue(mixed $value, LivePropMetadata $propMetadata, objec
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but the given serializer does not implement DenormalizerInterface.', $propMetadata->getName(), $parentObject::class));
}

if (null === $propMetadata->getType()) {
if ($propMetadata->collectionValueType()) {
$builtInType = $propMetadata->collectionValueType()->getBuiltinType();
if (Type::BUILTIN_TYPE_OBJECT === $builtInType) {
$type = $propMetadata->collectionValueType()->getClassName().'[]';
} else {
$type = $builtInType.'[]';
}
} else {
$type = $propMetadata->getType();
}

if (null === $type) {
throw new \LogicException(sprintf('The "%s::%s" object should be hydrated with the Serializer, but no type could be guessed.', $parentObject::class, $propMetadata->getName()));
}

return $this->serializer->denormalize($value, $propMetadata->getType(), 'json', $propMetadata->serializationContext());
return $this->serializer->denormalize($value, $type, 'json', $propMetadata->serializationContext());
}

if ($propMetadata->collectionValueType() && Type::BUILTIN_TYPE_OBJECT === $propMetadata->collectionValueType()->getBuiltinType()) {
Expand Down
36 changes: 36 additions & 0 deletions src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,42 @@ public function mount()
;
}];

yield 'Collection: using serializer (de)hydrates correctly' => [function () {
return HydrationTest::create(new class() {
/** @var \Symfony\UX\LiveComponent\Tests\Fixtures\Dto\Temperature[] */
#[LiveProp(useSerializerForHydration: true)]
public array $temperatures = [];

/**
* @var string[]
*/
#[LiveProp(useSerializerForHydration: true)]
public array $tags = [];
})
->mountWith([
'temperatures' => [
new Temperature(10, 'C'),
new Temperature(20, 'C'),
],
'tags' => ['foo', 'bar'],
])
->assertDehydratesTo([
'temperatures' => [
['degrees' => 10, 'uom' => 'C'],
['degrees' => 20, 'uom' => 'C'],
],
'tags' => ['foo', 'bar'],
])
->assertObjectAfterHydration(function (object $object) {
self::assertSame(10, $object->temperatures[0]->degrees);
self::assertSame('C', $object->temperatures[0]->uom);
self::assertSame(20, $object->temperatures[1]->degrees);
self::assertSame('C', $object->temperatures[1]->uom);
self::assertSame(['foo', 'bar'], $object->tags);
})
;
}];

yield 'Updating non-writable path is rejected' => [function () {
$product = new ProductFixtureEntity();
$product->name = 'original name';
Expand Down