Skip to content

Commit 828be1d

Browse files
committed
bug #295 [Live] fix bug when trying to hydrate nullable Doctrine Entity (kbond)
This PR was merged into the 2.x branch. Discussion ---------- [Live] fix bug when trying to hydrate nullable Doctrine Entity | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | n/a | License | MIT Fixes a regression in 2.1 where a nullable entity is attempted to be loaded from Doctrine. In 2.0, `null` was dropped from the component data loading the entity wasn't attempted. Commits ------- 03188d8 [Live] fix bug when trying to hydrate nullable Doctrine Entity
2 parents aaa334d + 03188d8 commit 828be1d

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

src/LiveComponent/src/Normalizer/DoctrineObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function supportsNormalization(mixed $data, string $format = null, array
6666

6767
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): ?object
6868
{
69-
return $this->objectManagerFor($type)->find($type, $data);
69+
return null === $data ? null : $this->objectManagerFor($type)->find($type, $data);
7070
}
7171

7272
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = [])
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;
4+
5+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
6+
use Symfony\UX\LiveComponent\Attribute\LiveProp;
7+
use Symfony\UX\LiveComponent\DefaultActionTrait;
8+
use Symfony\UX\LiveComponent\Tests\Fixtures\Entity\Entity1;
9+
10+
/**
11+
* @author Kevin Bond <[email protected]>
12+
*/
13+
#[AsLiveComponent('with_nullable_entity')]
14+
final class WithNullableEntity
15+
{
16+
use DefaultActionTrait;
17+
18+
#[LiveProp]
19+
public ?Entity1 $prop1 = null;
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prop1: {{ prop1.id|default('default') }}

src/LiveComponent/tests/Functional/EventListener/LiveComponentSubscriberTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,16 @@ public function testInjectsLiveArgs(): void
210210
->assertContains('Arg3: 33.3')
211211
;
212212
}
213+
214+
public function testWithNullableEntity(): void
215+
{
216+
$dehydrated = $this->dehydrateComponent($this->mountComponent('with_nullable_entity'));
217+
218+
$this->browser()
219+
->throwExceptions()
220+
->get('/_components/with_nullable_entity?data='.urlencode(json_encode($dehydrated)))
221+
->assertSuccessful()
222+
->assertContains('Prop1: default')
223+
;
224+
}
213225
}

src/TwigComponent/src/ComponentFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function create(string $name, array $data = []): MountedComponent
7171

7272
// ensure remaining data is scalar
7373
foreach ($data as $key => $value) {
74-
if (!is_scalar($value) && null !== $value) {
74+
if (!\is_scalar($value) && null !== $value) {
7575
throw new \LogicException(sprintf('Unable to use "%s" (%s) as an attribute. Attributes must be scalar or null. If you meant to mount this value on "%s", make sure "$%1$s" is a writable property or create a mount() method with a "$%1$s" argument.', $key, get_debug_type($value), $component::class));
7676
}
7777
}

0 commit comments

Comments
 (0)