8000 [Serializer] Get attributeContext after converting name · symfony/symfony@48c47df · GitHub
[go: up one dir, main page]

Skip to content

Commit 48c47df

Browse files
committed
[Serializer] Get attributeContext after converting name
1 parent 834e57a commit 48c47df

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,12 @@ public function denormalize($data, string $type, string $format = null, array $c
359359
$resolvedClass = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object);
360360

361361
foreach ($normalizedData as $attribute => $value) {
362-
$attributeContext = $this->getAttributeDenormalizationContext($resolvedClass, $attribute, $context);
363-
364362
if ($this->nameConverter) {
365-
$attribute = $this->nameConverter->denormalize($attribute, $resolvedClass, $format, $attributeContext);
363+
$attribute = $this->nameConverter->denormalize($attribute, $resolvedClass, $format, $context);
366364
}
367365

366+
$attributeContext = $this->getAttributeDenormalizationContext($resolvedClass, $attribute, $context);
367+
368368
if ((false !== $allowedAttributes && !\in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($resolvedClass, $attribute, $format, $context)) {
369369
if (!($context[self::ALLOW_EXTRA_ATTRIBUTES] ?? $this->defaultContext[self::ALLOW_EXTRA_ATTRIBUTES])) {
370370
$extraAttributes[] = $attribute;

src/Symfony/Component/Serializer/Tests/Normalizer/Features/ContextMetadataTestTrait.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Serializer\Annotation\Groups;
1818
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
1919
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
20+
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
2021
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
2122
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
2223
use Symfony\Component\Serializer\Serializer;
@@ -70,6 +71,17 @@ public function testContextMetadataContextDenormalize()
7071
]);
7172
self::assertEquals('2011-07-28', $dummy->date->format('Y-m-d'), 'a specific denormalization context is used for this group');
7273
}
74+
75+
public function testContextDenormalizeWithNameConverter()
76+
{
77+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
78+
$normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter(), null, new PhpDocExtractor());
79+
new Serializer([new DateTimeNormalizer(), $normalizer]);
80+
81+
/** @var ContextMetadataNamingDummy $dummy */
82+
$dummy = $normalizer->denormalize(['created_at' => '28/07/2011'], ContextMetadataNamingDummy::class);
83+
self::assertEquals('2011-07-28', $dummy->createdAt->format('Y-m-d'));
84+
}
7385
}
7486

7587
class ContextMetadataDummy
@@ -90,3 +102,13 @@ class ContextMetadataDummy
90102
*/
91103
public $date;
92104
}
105+
106+
class ContextMetadataNamingDummy
107+
{
108+
/**
109+
* @var \DateTime
110+
*
111+
* @Context({ DateTimeNormalizer::FORMAT_KEY = "d/m/Y" })
112+
*/
113+
public $createdAt;
114+
}

0 commit comments

Comments
 (0)
0