From d82ec41d1879a95e0ffddb0337e4da356d4b0746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20B=C3=B6nner?= Date: Thu, 23 Feb 2023 10:01:28 +0100 Subject: [PATCH] [Serializer] Fix serializedpath for non scalar types --- .../Normalizer/AbstractObjectNormalizer.php | 11 ++++---- .../AbstractObjectNormalizerTest.php | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index 7c4c5fb41bd49..1feff46ad3d56 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -198,14 +198,15 @@ public function normalize(mixed $object, string $format = null, array $context = $attributeValue = $this->applyCallbacks($attributeValue, $object, $attribute, $format, $attributeContext); - if (null !== $attributeValue && !\is_scalar($attributeValue)) { - $stack[$attribute] = $attributeValue; - } - - $data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata); + $stack[$attribute] = $attributeValue; } foreach ($stack as $attribute => $attributeValue) { + if (null === $attributeValue || \is_scalar($attributeValue)) { + $data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $context, $attributesMetadata, $classMetadata); + continue; + } + if (!$this->serializer instanceof NormalizerInterface) { throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer.', $attribute)); } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php index 4cc6686586e89..9a319f7ef0b8a 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php @@ -542,6 +542,21 @@ public function testDenormalizeUsesContextAttributeForPropertiesInConstructorWit $this->assertSame($obj->propertyWithSerializedName->format('Y-m-d'), $obj->propertyWithoutSerializedName->format('Y-m-d')); } + + public function testNormalizeUsesContextAttributeForPropertiesInConstructorWithSerializedPath() + { + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); + + $extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]); + $normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory), null, $extractor); + $serializer = new Serializer([new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => 'd-m-Y']), $normalizer]); + + $obj = new ObjectDummyWithContextAttributeAndSerializedPath(new \DateTimeImmutable('22-02-2023')); + + $data = $serializer->normalize($obj); + + $this->assertSame(['property' => ['with_path' => '22-02-2023']], $data); + } } class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer @@ -643,6 +658,16 @@ class DuplicateKeyNestedDummy public $notquux; } +class ObjectDummyWithContextAttributeAndSerializedPath +{ + public function __construct( + #[Context([DateTimeNormalizer::FORMAT_KEY => 'm-d-Y'])] + #[SerializedPath('[property][with_path]')] + public \DateTimeImmutable $propertyWithPath, + ) { + } +} + class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer { public function __construct()