Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Fix deserializing of nested snake_case attributes using CamelCaseToSnakeCaseNameConverter #51394

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

Closed
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 @@ -326,13 +326,15 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
$mappedClass = $this->getMappedClass($normalizedData, $type, $context);

$nestedAttributes = $this->getNestedAttributes($mappedClass);
$nestedData = [];
$nestedData = $originalNestedData = [];
$propertyAccessor = PropertyAccess::createPropertyAccessor();
foreach ($nestedAttributes as $property => $serializedPath) {
if (null === $value = $propertyAccessor->getValue($normalizedData, $serializedPath)) {
continue;
}
$nestedData[$property] = $value;
$convertedProperty = $this->nameConverter ? $this->nameConverter->normalize($property, $mappedClass, $format, $context) : $property;
$nestedData[$convertedProperty] = $value;
$originalNestedData[$property] = $value;
$normalizedData = $this->removeNestedValue($serializedPath->getElements(), $normalizedData);
}

Expand All @@ -345,7 +347,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
if ($this->nameConverter) {
$notConverted = $attribute;
$attribute = $this->nameConverter->denormalize($attribute, $resolvedClass, $format, $context);
if (isset($nestedData[$notConverted]) && !isset($nestedData[$attribute])) {
if (isset($nestedData[$notConverted]) && !isset($originalNestedData[$attribute])) {
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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
Expand Down Expand Up @@ -140,6 +141,20 @@ public function testDenormalizeWithNestedAttributesWithoutMetadata()
$this->assertNull($test->notfoo);
}

public function testDenormalizeWithSnakeCaseNestedAttributes()
{
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new AbstractObjectNormalizerDummy($factory, new CamelCaseToSnakeCaseNameConverter());
$serializer = new Serializer([$normalizer]);
$data = [
'one' => [
'two_three' => 'fooBar',
],
];
$test = $serializer->denormalize($data, SnakeCaseNestedDummy::class, 'any');
$this->assertSame('fooBar', $test->fooBar);
}

public function testDenormalizeWithNestedAttributes()
{
$normalizer = new AbstractObjectNormalizerWithMetadata();
Expand Down Expand Up @@ -770,7 +785,7 @@ protected function setAttributeValue(object $object, string $attribute, $value,

protected function isAllowedAttribute($classOrObject, string $attribute, string $format = null, array $context = []): bool
{
return \in_array($attribute, ['foo', 'baz', 'quux', 'value']);
return \in_array($attribute, ['foo', 'baz', 'quux', 'value', 'fooBar']);
}

public function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null): object
Expand Down Expand Up @@ -861,6 +876,14 @@ public function __construct(
}
}

class SnakeCaseNestedDummy
{
/**
* @SerializedPath("[one][two_three]")
*/
public $fooBar;
}

/**
* @DiscriminatorMap(typeProperty="type", mapping={
* "first" = FirstNestedDummyWithConstructorAndDiscriminator::class,
Expand Down