Skip to content

Commit bf4ea59

Browse files
bug #51399 [Serializer] Fix deserializing of nested snake_case attributes using CamelCaseToSnakeCaseNameConverter (Victor-Truhanovich)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Serializer] Fix deserializing of nested snake_case attributes using CamelCaseToSnakeCaseNameConverter | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT It was not possible to deserialize nested attributes using CamelCaseToSnakeCaseNameConverter ### Example ```php readonly class Foo { public function __conctruct( #[SerializedPath("[one][two_three]")] public $fooBar, ) { } } $factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); $normalizer = new ObjectNormalizer($factory, new CamelCaseToSnakeCaseNameConverter()); $serializer = new Serializer([$normalizer]); $data = [ 'one' => [ 'two_three' => 'fooBar', ], ]; $foo = $serializer->denormalize($data, Foo::class, 'any'); ``` Commits ------- f114c55 [Serializer] Fix deserializing of nested snake_case attributes using CamelCaseToSnakeCaseNameConverter
2 parents e4ada73 + f114c55 commit bf4ea59

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,15 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
326326
$mappedClass = $this->getMappedClass($normalizedData, $type, $context);
327327

328328
$nestedAttributes = $this->getNestedAttributes($mappedClass);
329-
$nestedData = [];
329+
$nestedData = $originalNestedData = [];
330330
$propertyAccessor = PropertyAccess::createPropertyAccessor();
331331
foreach ($nestedAttributes as $property => $serializedPath) {
332332
if (null === $value = $propertyAccessor->getValue($normalizedData, $serializedPath)) {
333333
continue;
334334
}
335-
$nestedData[$property] = $value;
335+
$convertedProperty = $this->nameConverter ? $this->nameConverter->normalize($property, $mappedClass, $format, $context) : $property;
336+
$nestedData[$convertedProperty] = $value;
337+
$originalNestedData[$property] = $value;
336338
$normalizedData = $this->removeNestedValue($serializedPath->getElements(), $normalizedData);
337339
}
338340

@@ -345,7 +347,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
345347
if ($this->nameConverter) {
346348
$notConverted = $attribute;
347349
$attribute = $this->nameConverter->denormalize($attribute, $resolvedClass, $format, $context);
348-
if (isset($nestedData[$notConverted]) && !isset($nestedData[$attribute])) {
350+
if (isset($nestedData[$notConverted]) && !isset($originalNestedData[$attribute])) {
349351
throw new LogicException(sprintf('Duplicate values for key "%s" found. One value is set via the SerializedPath annotation: "%s", the other one is set via the SerializedName annotation: "%s".', $notConverted, implode('->', $nestedAttributes[$notConverted]->getElements()), $attribute));
350352
}
351353
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
3434
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
3535
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
36+
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
3637
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
3738
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3839
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
@@ -140,6 +141,29 @@ public function testDenormalizeWithNestedAttributesWithoutMetadata()
140141
$this->assertNull($test->notfoo);
141142
}
142143

144+
public function testDenormalizeWithSnakeCaseNestedAttributes()
145+
{
146+
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
147+
$normalizer = new ObjectNormalizer($factory, new CamelCaseToSnakeCaseNameConverter());
148+
$data = [
149+
'one' => [
150+
'two_three' => 'fooBar',
151+
],
152+
];
153+
$test = $normalizer->denormalize($data, SnakeCaseNestedDummy::class, 'any');
154+
$this->assertSame('fooBar', $test->fooBar);
155+
}
156+
157+
public function testNormalizeWithSnakeCaseNestedAttributes()
158+
{
159+
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
160+
$normalizer = new ObjectNormalizer($factory, new CamelCaseToSnakeCaseNameConverter());
161+
$dummy = new SnakeCaseNestedDummy();
162+
$dummy->fooBar = 'fooBar';
163+
$test = $normalizer->normalize($dummy, 'any');
164+
$this->assertSame(['one' => ['two_three' => 'fooBar']], $test);
165+
}
166+
143167
public function testDenormalizeWithNestedAttributes()
144168
{
145169
$normalizer = new AbstractObjectNormalizerWithMetadata();
@@ -861,6 +885,14 @@ public function __construct(
861885
}
862886
}
863887

888+
class SnakeCaseNestedDummy
889+
{
890+
/**
891+
* @SerializedPath("[one][two_three]")
892+
*/
893+
public $fooBar;
894+
}
895+
864896
/**
865897
* @DiscriminatorMap(typeProperty="type", mapping={
866898
* "first" = FirstNestedDummyWithConstructorAndDiscriminator::class,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy