|
12 | 12 | namespace Symfony\Component\Serializer\Tests\Normalizer; |
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\PropertyAccess\PropertyPath; |
15 | 16 | use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; |
16 | 17 | use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; |
17 | 18 | use Symfony\Component\PropertyInfo\PropertyInfoExtractor; |
@@ -873,6 +874,42 @@ public function testDenormalizeMissingAndNullNestedValues() |
873 | 874 | $this->assertFalse((new \ReflectionProperty($obj, 'bar'))->isInitialized($obj)); |
874 | 875 | } |
875 | 876 |
|
| 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 | + |
876 | 913 | public function testNormalizeBasedOnAllowedAttributes() |
877 | 914 | { |
878 | 915 | $normalizer = new class extends AbstractObjectNormalizer { |
|
0 commit comments