8000 [Serializer] Skip uninitialized properties with deep_object_to_populate · symfony/symfony@fab19f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit fab19f1

Browse files
committed
[Serializer] Skip uninitialized properties with deep_object_to_populate
1 parent 449f224 commit fab19f1

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,14 @@ public function denormalize($data, string $type, string $format = null, array $c
403403
? $this->classDiscriminatorResolver->getTypeForMappedObject($object)
404404
: $this->getAttributeValue($object, $attribute, $format, $attributeContext);
405405
} catch (NoSuchPropertyException $e) {
406+
} catch (UninitializedPropertyException $e) {
407+
if (!($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? true)) {
408+
throw $e;
409+
}
410+
} catch (\Error $e) {
411+
if (!(($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? true) && $this->isUninitializedValueError($e))) {
412+
throw $e;
413+
}
406414
}
407415
}
408416

src/Symfony/Component/Serializer/Tests/Normalizer/Features/SkipUninitializedValuesTestTrait.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
1313

1414
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
15-
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
15+
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
1616

1717
/**
1818
* Test AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES.
1919
*/
2020
trait SkipUninitializedValuesTestTrait
2121
{
22-
abstract protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface;
22+
abstract protected function getNormalizerForSkipUninitializedValues(): AbstractObjectNormalizer;
2323

2424
/**
2525
* @requires PHP 7.4
@@ -33,6 +33,15 @@ public function testSkipUninitializedValues(array $context)
3333
$normalizer = $this->getNormalizerForSkipUninitializedValues();
3434
$result = $normalizer->normalize($object, null, $context);
3535
$this->assertSame(['initialized' => 'value'], $result);
36+
37+
$normalizer->denormalize(
38+
['unInitialized' => 'value'],
39+
TypedPropertiesObjectWithGetters::class,
40+
null,
41+
['object_to_populate' => $objectToPopulate = new TypedPropertiesObjectWithGetters(), 'deep_object_to_populate' => true] + $context,
42+
);
43+
44+
$this->assertSame('value', $objectToPopulate->getUninitialized());
3645
}
3746

3847
public function skipUninitializedValuesFlagProvider(): iterable

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ protected function getNormalizerForCacheableObjectAttributesTest(): GetSetMethod
496496
return new GetSetMethodNormalizer();
497497
}
498498

499-
protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface
499+
protected function getNormalizerForSkipUninitializedValues(): GetSetMethodNormalizer
500500
{
501501
return new GetSetMethodNormalizer(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
502502
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ protected function getNormalizerForCacheableObjectAttributesTest(): AbstractObje
455455
return new PropertyNormalizer();
456456
}
457457

458-
protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface
458+
protected function getNormalizerForSkipUninitializedValues(): PropertyNormalizer
459459
{
460460
return new PropertyNormalizer(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
461461
}

0 commit comments

Comments
 (0)
0