Skip to content

Commit 05959a5

Browse files
committed
[LiveComponent] Fix collections hydration with serializer
1 parent 2ddc47b commit 05959a5

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/LiveComponent/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.17.0
4+
5+
- Fix collections hydration with serializer in LiveComponents
6+
37
## 2.16.0
48

59
- LiveComponents is now stable and no longer experimental 🥳

src/LiveComponent/src/LiveComponentHydrator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ public function hydrateValue(mixed $value, LivePropMetadata $propMetadata, objec
259259
throw new \LogicException(sprintf('The "%s::%s" object should be hydrated with the Serializer, but no type could be guessed.', $parentObject::class, $propMetadata->getName()));
260260
}
261261

262-
return $this->serializer->denormalize($value, $propMetadata->getType(), 'json', $propMetadata->serializationContext());
262+
$type = $propMetadata->collectionValueType() ? $propMetadata->collectionValueType()->getClassName().'[]' : $propMetadata->getType();
263+
264+
return $this->serializer->denormalize($value, $type, 'json', $propMetadata->serializationContext());
263265
}
264266

265267
if ($propMetadata->collectionValueType() && Type::BUILTIN_TYPE_OBJECT === $propMetadata->collectionValueType()->getBuiltinType()) {

src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,31 @@ public function mount()
10311031
;
10321032
}];
10331033

1034+
yield 'Collection: using serializer (de)hydrates correctly' => [function () {
1035+
return HydrationTest::create(new class() {
1036+
/** @var \Symfony\UX\LiveComponent\Tests\Fixtures\Dto\Temperature[] */
1037+
#[LiveProp(useSerializerForHydration: true)]
1038+
public array $temperatures = [];
1039+
})
1040+
->mountWith(['temperatures' => [
1041+
new Temperature(10, 'C'),
1042+
new Temperature(20, 'C'),
1043+
]])
1044+
->assertDehydratesTo([
1045+
'temperatures' => [
1046+
['degrees' => 10, 'uom' => 'C'],
1047+
['degrees' => 20, 'uom' => 'C'],
1048+
],
1049+
])
1050+
->assertObjectAfterHydration(function (object $object) {
1051+
self::assertSame(10, $object->temperatures[0]->degrees);
1052+
self::assertSame('C', $object->temperatures[0]->uom);
1053+
self::assertSame(20, $object->temperatures[1]->degrees);
1054+
self::assertSame('C', $object->temperatures[1]->uom);
1055+
})
1056+
;
1057+
}];
1058+
10341059
yield 'Updating non-writable path is rejected' => [function () {
10351060
$product = new ProductFixtureEntity();
10361061
$product->name = 'original name';

0 commit comments

Comments
 (0)