Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04d9850

Browse files
gnoddepnicolas-grekas
authored andcommitted
[Serializer] Fix removing nested values
1 parent 83f0df3 commit 04d9850

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ private function getNestedAttributes(string $class): array
859859
private function removeNestedValue(array $path, array $data): array
860860
{
861861
$element = array_shift($path);
862-
if (!$path || !$data[$element] = $this->removeNestedValue($path, $data[$element])) {
862+
if (!$path || !$data[$element] || !$data[$element] = $this->removeNestedValue($path, $data[$element])) {
863863
unset($data[$element]);
864864
}
865865

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Normalizer;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\PropertyAccess\PropertyPath;
1516
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1617
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1718
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
@@ -873,6 +874,42 @@ public function testDenormalizeMissingAndNullNestedValues()
873874
$this->assertFalse((new \ReflectionProperty($obj, 'bar'))->isInitialized($obj));
874875
}
875876

877+
public function testDenormalizeNullCoalescingValues()
878+
{
879+
if (!method_exists(PropertyPath::class, 'isNullSafe')) {
880+
$this->markTestSkipped('null coalescing property path is not supported before symfony/property-access 6.2');
881+
}
882+
883+
$normalizer = new AbstractObjectNormalizerWithMetadata();
884+
885+
$data = [
886+
'data' => [
887+
'foo' => 'test',
888+
],
889+
'empty_data' => null,
890+
];
891+
892+
$obj = new class {
893+
#[SerializedPath('[data][foo?]')]
894+
public ?string $foo;
895+
896+
#[SerializedPath('[data][bar?]')]
897+
public ?string $bar;
898+
899+
#[SerializedPath('[empty_data?][nothing]')]
900+
public ?string $nothing;
901+
902+
#[SerializedPath('[not_set?][nothing]')]
903+
public ?string $notSet;
904+
};
905+
906+
$test = $normalizer->denormalize($data, $obj::class);
907+
$this->assertSame('test', $test->foo);
908+
$this->assertFalse((new \ReflectionProperty($obj, 'bar'))->isInitialized($obj));
909+
$this->assertNull($test->nothing);
910+
$this->assertFalse((new \ReflectionProperty($obj, 'notSet'))->isInitialized($obj));
911+
}
912+
876913
public function testNormalizeBasedOnAllowedAttributes()
877914
{
878915
$normalizer = new class extends AbstractObjectNormalizer {

0 commit comments

Comments
 (0)