Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ab9049e

Browse filesBrowse files
[Serializer] Fix deserializing of nested snake_case attributes using CamelCaseToSnakeCaseNameConverter
1 parent 6a9f47c commit ab9049e
Copy full SHA for ab9049e

File tree

2 files changed

+29
-4
lines changed
Filter options

2 files changed

+29
-4
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,15 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
311311
$mappedClass = $this->getMappedClass($normalizedData, $type, $context);
312312

313313
$nestedAttributes = $this->getNestedAttributes($mappedClass);
314-
$nestedData = [];
314+
$nestedData = $originalNestedData = [];
315315
$propertyAccessor = PropertyAccess::createPropertyAccessor();
316316
foreach ($nestedAttributes as $property => $serializedPath) {
317317
if (null === $value = $propertyAccessor->getValue($normalizedData, $serializedPath)) {
318318
continue;
319319
}
320-
$nestedData[$property] = $value;
320+
$convertedProperty = $this->nameConverter ? $this->nameConverter->normalize($property, $mappedClass, $format, $context) : $property;
321+
$nestedData[$convertedProperty] = $value;
322+
$originalNestedData[$property] = $value;
321323
$normalizedData = $this->removeNestedValue($serializedPath->getElements(), $normalizedData);
322324
}
323325

@@ -330,7 +332,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
330332
if ($this->nameConverter) {
331333
$notConverted = $attribute;
332334
$attribute = $this->nameConverter->denormalize($attribute, $resolvedClass, $format, $context);
333-
if (isset($nestedData[$notConverted]) && !isset($nestedData[$attribute])) {
335+
if (isset($nestedData[$notConverted]) && !isset($originalNestedData[$attribute])) {
334336
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));
335337
}
336338
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php
+24-1Lines changed: 24 additions & 1 deletion
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,20 @@ 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 AbstractObjectNormalizerDummy($factory, new CamelCaseToSnakeCaseNameConverter());
148+
$serializer = new Serializer([$normalizer]);
149+
$data = [
150+
'one' => [
151+
'two_three' => 'fooBar',
152+
],
153+
];
154+
$test = $serializer->denormalize($data, SnakeCaseNestedDummy::class, 'any');
155+
$this->assertSame('fooBar', $test->fooBar);
156+
}
157+
143158
public function testDenormalizeWithNestedAttributes()
144159
{
145160
$normalizer = new AbstractObjectNormalizerWithMetadata();
@@ -744,7 +759,7 @@ protected function setAttributeValue(object $object, string $attribute, $value,
744759

745760
protected function isAllowedAttribute($classOrObject, string $attribute, string $format = null, array $context = []): bool
746761
{
747-
return \in_array($attribute, ['foo', 'baz', 'quux', 'value']);
762+
return \in_array($attribute, ['foo', 'baz', 'quux', 'value', 'fooBar']);
748763
}
749764

750765
public function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null): object
@@ -835,6 +850,14 @@ public function __construct(
835850
}
836851
}
837852

853+
class SnakeCaseNestedDummy
854+
{
855+
/**
856+
* @SerializedPath("[one][two_three]")
857+
*/
858+
public $fooBar;
859+
}
860+
838861
/**
839862
* @DiscriminatorMap(typeProperty="type", mapping={
840863
* "first" = FirstNestedDummyWithConstructorAndDiscriminator::class,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.