Skip to content

Commit

Permalink
[Serializer] Skip uninitialized properties with deep_object_to_populate
Browse files Browse the repository at this point in the history
  • Loading branch information
mtarld committed Dec 27, 2023
1 parent a363d0b commit b8353e4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Normalizer/AbstractObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,14 @@ public function denormalize($data, string $type, string $format = null, array $c
? $this->classDiscriminatorResolver->getTypeForMappedObject($object)
: $this->getAttributeValue($object, $attribute, $format, $attributeContext);
} catch (NoSuchPropertyException $e) {
} catch (UninitializedPropertyException $e) {
if (!($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? true)) {
throw $e;
}
} catch (\Error $e) {
if (!(($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? true) && $this->isUninitializedValueError($e))) {
throw $e;
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions Tests/Normalizer/Features/SkipUninitializedValuesTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;

use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

/**
* Test AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES.
*/
trait SkipUninitializedValuesTestTrait
{
abstract protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface;
abstract protected function getNormalizerForSkipUninitializedValues(): AbstractObjectNormalizer;

/**
* @requires PHP 7.4
Expand All @@ -33,6 +33,15 @@ public function testSkipUninitializedValues(array $context)
$normalizer = $this->getNormalizerForSkipUninitializedValues();
$result = $normalizer->normalize($object, null, $context);
$this->assertSame(['initialized' => 'value'], $result);

$normalizer->denormalize(
['unInitialized' => 'value'],
TypedPropertiesObjectWithGetters::class,
null,
['object_to_populate' => $objectToPopulate = new TypedPropertiesObjectWithGetters(), 'deep_object_to_populate' => true] + $context
);

$this->assertSame('value', $objectToPopulate->getUninitialized());
}

public function skipUninitializedValuesFlagProvider(): iterable
Expand Down
2 changes: 1 addition & 1 deletion Tests/Normalizer/GetSetMethodNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ protected function getNormalizerForCacheableObjectAttributesTest(): GetSetMethod
return new GetSetMethodNormalizer();
}

protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface
protected function getNormalizerForSkipUninitializedValues(): GetSetMethodNormalizer
{
return new GetSetMethodNormalizer(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
}
Expand Down
3 changes: 1 addition & 2 deletions Tests/Normalizer/PropertyNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
Expand Down Expand Up @@ -455,7 +454,7 @@ protected function getNormalizerForCacheableObjectAttributesTest(): AbstractObje
return new PropertyNormalizer();
}

protected function getNormalizerForSkipUninitializedValues(): NormalizerInterface
protected function getNormalizerForSkipUninitializedValues(): PropertyNormalizer
{
return new PropertyNormalizer(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
}
Expand Down

0 comments on commit b8353e4

Please sign in to comment.