From 48c47df601798cdc2192a23f281f8251a03c9bd0 Mon Sep 17 00:00:00 2001 From: zenas1210 Date: Tue, 31 May 2022 17:58:40 +0300 Subject: [PATCH] [Serializer] Get attributeContext after converting name --- .../Normalizer/AbstractObjectNormalizer.php | 6 ++--- .../Features/ContextMetadataTestTrait.php | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index 951eb9d4a59b8..7a5c1bf9d0522 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -359,12 +359,12 @@ public function denormalize($data, string $type, string $format = null, array $c $resolvedClass = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object); foreach ($normalizedData as $attribute => $value) { - $attributeContext = $this->getAttributeDenormalizationContext($resolvedClass, $attribute, $context); - if ($this->nameConverter) { - $attribute = $this->nameConverter->denormalize($attribute, $resolvedClass, $format, $attributeContext); + $attribute = $this->nameConverter->denormalize($attribute, $resolvedClass, $format, $context); } + $attributeContext = $this->getAttributeDenormalizationContext($resolvedClass, $attribute, $context); + if ((false !== $allowedAttributes && !\in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($resolvedClass, $attribute, $format, $context)) { if (!($context[self::ALLOW_EXTRA_ATTRIBUTES] ?? $this->defaultContext[self::ALLOW_EXTRA_ATTRIBUTES])) { $extraAttributes[] = $attribute; diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ContextMetadataTestTrait.php b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ContextMetadataTestTrait.php index 374cacaf79d02..4dbba913b7272 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ContextMetadataTestTrait.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ContextMetadataTestTrait.php @@ -17,6 +17,7 @@ use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; +use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; @@ -70,6 +71,17 @@ public function testContextMetadataContextDenormalize() ]); self::assertEquals('2011-07-28', $dummy->date->format('Y-m-d'), 'a specific denormalization context is used for this group'); } + + public function testContextDenormalizeWithNameConverter() + { + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); + $normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter(), null, new PhpDocExtractor()); + new Serializer([new DateTimeNormalizer(), $normalizer]); + + /** @var ContextMetadataNamingDummy $dummy */ + $dummy = $normalizer->denormalize(['created_at' => '28/07/2011'], ContextMetadataNamingDummy::class); + self::assertEquals('2011-07-28', $dummy->createdAt->format('Y-m-d')); + } } class ContextMetadataDummy @@ -90,3 +102,13 @@ class ContextMetadataDummy */ public $date; } + +class ContextMetadataNamingDummy +{ + /** + * @var \DateTime + * + * @Context({ DateTimeNormalizer::FORMAT_KEY = "d/m/Y" }) + */ + public $createdAt; +}