Skip to content

[LiveComponent] Throw exception for typed LiveProps as interfaces #1593

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
merged 1 commit into from
Mar 20, 2024
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
8 changes: 8 additions & 0 deletions src/LiveComponent/src/LiveComponentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ private function dehydrateObjectValue(object $value, string $classType, ?string
}
}

if (interface_exists($classType)) {
throw new \LogicException(sprintf('Cannot dehydrate value typed as interface "%s" on component "%s". Change this to a concrete type that can be dehydrated. Or set the hydrateWith/dehydrateWith options in LiveProp or set "useSerializerForHydration: true" on the LiveProp to use the serializer.', get_debug_type($value), $parentObject::class));
}

$dehydratedObjectValues = [];
foreach ((new PropertyInfoExtractor([new ReflectionExtractor()]))->getProperties($classType) as $property) {
$propertyValue = $this->propertyAccessor->getValue($value, $property);
Expand Down Expand Up @@ -540,6 +544,10 @@ private function hydrateObjectValue(mixed $value, string $className, bool $allow
}
}

if (interface_exists($className)) {
throw new \LogicException(sprintf('Cannot hydrate value typed as interface "%s" on component "%s". Change this to a concrete type that can be hydrated. Or set the hydrateWith/dehydrateWith options in LiveProp or set "useSerializerForHydration: true" on the LiveProp to use the serializer.', $className, $component::class));
}

if (\is_array($value)) {
$object = new $className();
foreach ($value as $propertyName => $propertyValue) {
Expand Down
16 changes: 16 additions & 0 deletions src/LiveComponent/tests/Fixtures/Dto/SimpleDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\LiveComponent\Tests\Fixtures\Dto;

class SimpleDto implements SimpleDtoInterface
{
}
16 changes: 16 additions & 0 deletions src/LiveComponent/tests/Fixtures/Dto/SimpleDtoInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\LiveComponent\Tests\Fixtures\Dto;

interface SimpleDtoInterface
{
}
37 changes: 37 additions & 0 deletions src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\Embeddable2;
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\Money;
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\ParentDTO;
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\SimpleDto;
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\SimpleDtoInterface;
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\Temperature;
use Symfony\UX\LiveComponent\Tests\Fixtures\Entity\CategoryFixtureEntity;
use Symfony\UX\LiveComponent\Tests\Fixtures\Entity\Embeddable1;
Expand Down Expand Up @@ -1307,6 +1309,41 @@ public function __construct()
);
}

public function testInterfaceTypedLivePropCannotBeHydrated(): void
{
$componentClass = new class() {
#[LiveProp(writable: true)]
public ?SimpleDtoInterface $prop = null;
};

$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('/Unable to hydrate value of type "Symfony\UX\LiveComponent\Tests\Fixtures\Dto\SimpleDto" for property "prop" typed as "Symfony\UX\LiveComponent\Tests\Fixtures\Dto\SimpleDtoInterface" on component/');
$this->expectExceptionMessageMatches('/ Change this to a concrete type that can be hydrate/');

$this->hydrator()->hydrate(
component: new $componentClass(),
props: $this->hydrator()->addChecksumToData(['prop' => 'foo']),
updatedProps: ['prop' => 'bar'],
componentMetadata: $this->createLiveMetadata($componentClass),
);
}

public function testInterfaceTypedLivePropCannotBeDehydrated(): void
{
$componentClass = new class() {
#[LiveProp(writable: true)]
public ?SimpleDtoInterface $prop = null;
};
$component = new $componentClass();
$component->prop = new SimpleDto();

$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('/Cannot dehydrate value typed as interface "Symfony\UX\LiveComponent\Tests\Fixtures\Dto\SimpleDtoInterface" on component ');
$this->expectExceptionMessageMatches('/ Change this to a concrete type that can be dehydrated/');

$this->hydrator()->dehydrate($component, new ComponentAttributes([]), $this->createLiveMetadata($component));
}

/**
* @dataProvider provideInvalidHydrationTests
*/
Expand Down