Skip to content
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
4 changes: 2 additions & 2 deletions src/LiveComponent/src/LiveComponentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
final class LiveComponentHydrator
{
private const CHECKSUM_KEY = '_checksum';
private const EXPOSED_PROP_KEY = 'id';
private const EXPOSED_PROP_KEY = '_id';
Copy link
Member Author

@kbond kbond Jan 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't mess up the frontend, does it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe so


/** @var PropertyHydratorInterface[] */
private iterable $propertyHydrators;
Expand Down Expand Up @@ -197,7 +197,7 @@ private function computeChecksum(array $data, array $readonlyProperties): string
// for read-only properties with "exposed" sub-parts,
// only use the main value
foreach ($properties as $key => $val) {
if (\in_array($key, $readonlyProperties) && \is_array($val)) {
if (\in_array($key, $readonlyProperties) && \is_array($val) && isset($val[self::EXPOSED_PROP_KEY])) {
$properties[$key] = $val[self::EXPOSED_PROP_KEY];
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\UX\LiveComponent\Tests\Integration;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\LiveComponentHydrator;
use Symfony\UX\LiveComponent\Tests\ContainerBC;
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component1;
Expand Down Expand Up @@ -239,4 +240,29 @@ public function testCorrectlyUsesCustomFrontendNameInDehydrateAndHydrate(): void
$this->assertSame('value1', $component->prop1);
$this->assertSame('value2', $component->prop2);
}

public function testCanDehydrateAndHydrateArrays(): void
{
/** @var LiveComponentHydrator $hydrator */
$hydrator = self::getContainer()->get('ux.live_component.component_hydrator');

$component = new class() {
#[LiveProp]
public array $prop;
};

$instance = clone $component;
$instance->prop = ['some', 'array'];

$dehydrated = $hydrator->dehydrate($instance);

$this->assertArrayHasKey('prop', $dehydrated);
$this->assertSame($instance->prop, $dehydrated['prop']);

$this->assertFalse(isset($component->prop));

$hydrator->hydrate($component, $dehydrated);

$this->assertSame($instance->prop, $component->prop);
}
}