Skip to content

Commit 79c39c1

Browse files
committed
[Serializer] Skip uninitialized properties with deep_object_to_populate
1 parent 0b9b56c commit 79c39c1

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 11 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy