8000 [Serializer] Skip uninitialized properties with deep_object_to_populate by mtarld · Pull Request #53140 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Skip uninitialized properties with deep_object_to_populate #53140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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