Skip to content

[Serializer] Fix serializer normalize attribute context #50015

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
Apr 14, 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 @@ -196,22 +196,21 @@ public function normalize(mixed $object, string $format = null, array $context =
$attributeValue = $maxDepthHandler($attributeValue, $object, $attribute, $format, $attributeContext);
}

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

$stack[$attribute] = $attributeValue;
$stack[$attribute] = $this->applyCallbacks($attributeValue, $object, $attribute, $format, $attributeContext);
}

foreach ($stack as $attribute => $attributeValue) {
$attributeContext = $this->getAttributeNormalizationContext($object, $attribute, $context);

if (null === $attributeValue || \is_scalar($attributeValue)) {
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $context, $attributesMetadata, $classMetadata);
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $attributeContext, $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));
}

$attributeContext = $this->getAttributeNormalizationContext($object, $attribute, $context);
$childContext = $this->createChildContext($attributeContext, $attribute, $format);

$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $childContext), $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Symfony\Component\Serializer\Annotation\SerializedPath;
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
Expand Down Expand Up @@ -549,13 +550,28 @@ public function testNormalizeUsesContextAttributeForPropertiesInConstructorWithS

$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]);
$serializer = new Serializer([new DateTimeNormalizer(), $normalizer]);

$obj = new ObjectDummyWithContextAttributeAndSerializedPath(new \DateTimeImmutable('22-02-2023'));

$data = $serializer->normalize($obj);

$this->assertSame(['property' => ['with_path' => '22-02-2023']], $data);
$this->assertSame(['property' => ['with_path' => '02-22-2023']], $data);
}

public function testNormalizeUsesContextAttributeForProperties()
{
$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([$normalizer]);

$obj = new ObjectDummyWithContextAttributeSkipNullValues();

$data = $serializer->normalize($obj);

$this->assertSame(['propertyWithoutNullSkipNullValues' => 'foo'], $data);
}
}

Expand Down Expand Up @@ -668,6 +684,15 @@ public function __construct(
}
}

class ObjectDummyWithContextAttributeSkipNullValues
{
#[Context([AbstractObjectNormalizer::SKIP_NULL_VALUES => true])]
public ?string $propertyWithoutNullSkipNullValues = 'foo';

#[Context([AbstractObjectNormalizer::SKIP_NULL_VALUES => true])]
public ?string $propertyWithNullSkipNullValues = null;
}

class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer
{
public function __construct()
Expand Down