Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit9834d95

Browse files
[Serializer] Fix deserializing of nested snake_case attributes using CamelCaseToSnakeCaseNameConverter
1 parent08018d8 commit9834d95

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ public function __construct(
138138
}
139139

140140
/**
141-
* @param array $context
142-
*
143141
* @return bool
144142
*/
145143
publicfunctionsupportsNormalization(mixed$data,string$format =null/* , array $context = [] */)
@@ -295,8 +293,6 @@ abstract protected function extractAttributes(object $object, string $format = n
295293
abstractprotectedfunctiongetAttributeValue(object$object,string$attribute,string$format =null,array$context = []);
296294

297295
/**
298-
* @param array $context
299-
*
300296
* @return bool
301297
*/
302298
publicfunctionsupportsDenormalization(mixed$data,string$type,string$format =null/* , array $context = [] */)
@@ -326,13 +322,15 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
326322
$mappedClass =$this->getMappedClass($normalizedData,$type,$context);
327323

328324
$nestedAttributes =$this->getNestedAttributes($mappedClass);
329-
$nestedData = [];
325+
$nestedData =$originalNestedData =[];
330326
$propertyAccessor = PropertyAccess::createPropertyAccessor();
331327
foreach ($nestedAttributesas$property =>$serializedPath) {
332328
if (null ===$value =$propertyAccessor->getValue($normalizedData,$serializedPath)) {
333329
continue;
334330
}
335-
$nestedData[$property] =$value;
331+
$convertedProperty =$this->nameConverter ?$this->nameConverter->normalize($property,$mappedClass,$format,$context) :$property;
332+
$nestedData[$convertedProperty] =$value;
333+
$originalNestedData[$property] =$value;
336334
$normalizedData =$this->removeNestedValue($serializedPath->getElements(),$normalizedData);
337335
}
338336

@@ -345,7 +343,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
345343
if ($this->nameConverter) {
346344
$notConverted =$attribute;
347345
$attribute =$this->nameConverter->denormalize($attribute,$resolvedClass,$format,$context);
348-
if (isset($nestedData[$notConverted]) && !isset($nestedData[$attribute])) {
346+
if (isset($nestedData[$notConverted]) && !isset($originalNestedData[$attribute])) {
349347
thrownewLogicException(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));
350348
}
351349
}

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

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

144+
publicfunctiontestDenormalizeWithSnakeCaseNestedAttributes()
145+
{
146+
$factory =newClassMetadataFactory(newAnnotationLoader(newAnnotationReader()));
147+
$normalizer =newAbstractObjectNormalizerDummy($factory,newCamelCaseToSnakeCaseNameConverter());
148+
$serializer =newSerializer([$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
publicfunctiontestDenormalizeWithNestedAttributes()
144159
{
145160
$normalizer =newAbstractObjectNormalizerWithMetadata();
@@ -770,7 +785,7 @@ protected function setAttributeValue(object $object, string $attribute, $value,
770785

771786
protectedfunctionisAllowedAttribute($classOrObject,string$attribute,string$format =null,array$context = []):bool
772787
{
773-
return\in_array($attribute, ['foo','baz','quux','value']);
788+
return\in_array($attribute, ['foo','baz','quux','value','fooBar']);
774789
}
775790

776791
publicfunctioninstantiateObject(array &$data,string$class,array &$context,\ReflectionClass$reflectionClass,$allowedAttributes,string$format =null):object
@@ -861,6 +876,14 @@ public function __construct(
861876
}
862877
}
863878

879+
class SnakeCaseNestedDummy
880+
{
881+
/**
882+
* @SerializedPath("[one][two_three]")
883+
*/
884+
public$fooBar;
885+
}
886+
864887
/**
865888
* @DiscriminatorMap(typeProperty="type", mapping={
866889
* "first" = FirstNestedDummyWithConstructorAndDiscriminator::class,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp