8000 [Serializer] Fix serializedpath for non scalar types · symfony/symfony@d82ec41 · GitHub
[go: up one dir, main page]

Skip to content

Commit d82ec41

Browse files
boennernicolas-grekas
authored andcommitted
[Serializer] Fix serializedpath for non scalar types
1 parent 2db7d6f commit d82ec41

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,15 @@ public function normalize(mixed $object, string $format = null, array $context =
198198

199199
$attributeValue = $this->applyCallbacks($attributeValue, $object, $attribute, $format, $attributeContext);
200200

201-
if (null !== $attributeValue && !\is_scalar($attributeValue)) {
202-
$stack[$attribute] = $attributeValue;
203-
}
204-
205-
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
201+
$stack[$attribute] = $attributeValue;
206202
}
207203

208204
foreach ($stack as $attribute => $attributeValue) {
205+
if (null === $attributeValue || \is_scalar($attributeValue)) {
206+
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $context, $attributesMetadata, $classMetadata);
207+
continue;
208+
}
209+
209210
if (!$this->serializer instanceof NormalizerInterface) {
210211
throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer.', $attribute));
211212
}

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,21 @@ public function testDenormalizeUsesContextAttributeForPropertiesInConstructorWit
542542

543543
$this->assertSame($obj->propertyWithSerializedName->format('Y-m-d'), $obj->propertyWithoutSerializedName->format('Y-m-d'));
544544
}
545+
546+
public function testNormalizeUsesContextAttributeForPropertiesInConstructorWithSerializedPath()
547+
{
548+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
549+
550+
$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);
551+
$normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory), null, $extractor);
552+
$serializer = new Serializer([new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => 'd-m-Y']), $normalizer]);
553+
554+
$obj = new ObjectDummyWithContextAttributeAndSerializedPath(new \DateTimeImmutable('22-02-2023'));
555+
556+
$data = $serializer->normalize($obj);
557+
558+
$this->assertSame(['property' => ['with_path' => '22-02-2023']], $data);
559+
}
545560
}
546561

547562
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -643,6 +658,16 @@ class DuplicateKeyNestedDummy
643658
public $notquux;
644659
}
645660

661+
class ObjectDummyWithContextAttributeAndSerializedPath
662+
{
663+
public function __construct(
664+
#[Context([DateTimeNormalizer::FORMAT_KEY => 'm-d-Y'])]
665+
#[SerializedPath('[property][with_path]')]
666+
public \DateTimeImmutable $propertyWithPath,
667+
) {
668+
}
669+
}
670+
646671
class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer
647672
{
648673
public function __construct()

0 commit comments

Comments
 (0)
0