Skip to content

Commit b7bc7e0

Browse files
committed
bug #241 [LiveComponent] add ability to have array props (kbond)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [LiveComponent] add ability to have array props | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | n/a | License | MIT Discovered a bug when working on #220. You currently can't have `LiveProp`'s that are "arrays". This is because the [_expose_ system](https://symfony.com/bundles/ux-live-component/current/index.html#modifying-embedded-properties-with-the-exposed-option) uses arrays and we weren't strict enough in determining when this system was being used. Commits ------- f71e718 [LiveComponent] add ability to have array props
2 parents 48e8f84 + f71e718 commit b7bc7e0

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/LiveComponent/src/LiveComponentHydrator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
final class LiveComponentHydrator
2929
{
3030
private const CHECKSUM_KEY = '_checksum';
31-
private const EXPOSED_PROP_KEY = 'id';
31+
private const EXPOSED_PROP_KEY = '_id';
3232

3333
/** @var PropertyHydratorInterface[] */
3434
private iterable $propertyHydrators;
@@ -197,7 +197,7 @@ private function computeChecksum(array $data, array $readonlyProperties): string
197197
// for read-only properties with "exposed" sub-parts,
198198
// only use the main value
199199
foreach ($properties as $key => $val) {
200-
if (\in_array($key, $readonlyProperties) && \is_array($val)) {
200+
if (\in_array($key, $readonlyProperties) && \is_array($val) && isset($val[self::EXPOSED_PROP_KEY])) {
201201
$properties[$key] = $val[self::EXPOSED_PROP_KEY];
202202
}
203203
}

src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\LiveComponent\Tests\Integration;
1313

1414
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15+
use Symfony\UX\LiveComponent\Attribute\LiveProp;
1516
use Symfony\UX\LiveComponent\LiveComponentHydrator;
1617
use Symfony\UX\LiveComponent\Tests\ContainerBC;
1718
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component1;
@@ -239,4 +240,29 @@ public function testCorrectlyUsesCustomFrontendNameInDehydrateAndHydrate(): void
239240
$this->assertSame('value1', $component->prop1);
240241
$this->assertSame('value2', $component->prop2);
241242
}
243+
244+
public function testCanDehydrateAndHydrateArrays(): void
245+
{
246+
/** @var LiveComponentHydrator $hydrator */
247+
$hydrator = self::getContainer()->get('ux.live_component.component_hydrator');
248+
249+
$component = new class() {
250+
#[LiveProp]
251+
public array $prop;
252+
};
253+
254+
$instance = clone $component;
255+
$instance->prop = ['some', 'array'];
256+
257+
$dehydrated = $hydrator->dehydrate($instance);
258+
259+
$this->assertArrayHasKey('prop', $dehydrated);
260+
$this->assertSame($instance->prop, $dehydrated['prop']);
261+
262+
$this->assertFalse(isset($component->prop));
263+
264+
$hydrator->hydrate($component, $dehydrated);
265+
266+
$this->assertSame($instance->prop, $component->prop);
267+
}
242268
}

0 commit comments

Comments
 (0)