Skip to content
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 @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down