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
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function supportsNormalization(mixed $data, string $format = null, array

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

public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = [])
Expand Down
20 changes: 20 additions & 0 deletions src/LiveComponent/tests/Fixtures/Component/WithNullableEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\LiveComponent\Tests\Fixtures\Entity\Entity1;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
#[AsLiveComponent('with_nullable_entity')]
final class WithNullableEntity
{
use DefaultActionTrait;

#[LiveProp]
public ?Entity1 $prop1 = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prop1: {{ prop1.id|default('default') }}
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,16 @@ public function testInjectsLiveArgs(): void
->assertContains('Arg3: 33.3')
;
}

public function testWithNullableEntity(): void
{
$dehydrated = $this->dehydrateComponent($this->mountComponent('with_nullable_entity'));

$this->browser()
->throwExceptions()
->get('/_components/with_nullable_entity?data='.urlencode(json_encode($dehydrated)))
->assertSuccessful()
->assertContains('Prop1: default')
;
}
}
2 changes: 1 addition & 1 deletion src/TwigComponent/src/ComponentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function create(string $name, array $data = []): MountedComponent

// ensure remaining data is scalar
foreach ($data as $key => $value) {
if (!is_scalar($value) && null !== $value) {
if (!\is_scalar($value) && null !== $value) {
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));
}
}
Expand Down