8000 bug #53140 [Serializer] Skip uninitialized properties with deep_objec… · symfony/symfony@4fee3d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4fee3d9

Browse files
bug #53140 [Serializer] Skip uninitialized properties with deep_object_to_populate (mtarld)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Skip uninitialized properties with deep_object_to_populate | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #52883 | License | MIT Commits ------- 79c39c1 [Serializer] Skip uninitialized properties with deep_object_to_populate
2 parents 386e238 + 79c39c1 commit 4fee3d9

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
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-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
2727
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
2828
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
29-
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
3029
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
3130
use Symfony\Component\Serializer\Serializer;
3231
use Symfony\Component\Serializer\SerializerInterface;
@@ -455,7 +454,7 @@ protected function getNormalizerForCacheableObjectAttributesTest(): AbstractObje
455454
return new PropertyNormalizer();
456455
}
457456

458-
protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface
457+
protected function getNormalizerForSkipUninitializedValues(): PropertyNormalizer
459458
{
460459
return new PropertyNormalizer(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
461460
}

0 commit comments

Comments
 (0)
0