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

Commitbf4ea59

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 | MITIt was not possible to deserialize nested attributes using CamelCaseToSnakeCaseNameConverter### Example```phpreadonly 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 parentse4ada73 +f114c55 commitbf4ea59

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 ($nestedAttributesas$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
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));
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
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,29 @@ public function testDenormalizeWithNestedAttributesWithoutMetadata()
140141
$this->assertNull($test->notfoo);
141142
}
142143

144+
publicfunctiontestDenormalizeWithSnakeCaseNestedAttributes()
145+
{
146+
$factory =newClassMetadataFactory(newAnnotationLoader(newAnnotationReader()));
147+
$normalizer =newObjectNormalizer($factory,newCamelCaseToSnakeCaseNameConverter());
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+
publicfunctiontestNormalizeWithSnakeCaseNestedAttributes()
158+
{
159+
$factory =newClassMetadataFactory(newAnnotationLoader(newAnnotationReader()));
160+
$normalizer =newObjectNormalizer($factory,newCamelCaseToSnakeCaseNameConverter());
161+
$dummy =newSnakeCaseNestedDummy();
162+
$dummy->fooBar ='fooBar';
163+
$test =$normalizer->normalize($dummy,'any');
164+
$this->assertSame(['one' => ['two_three' =>'fooBar']],$test);
165+
}
166+
143167
publicfunctiontestDenormalizeWithNestedAttributes()
144168
{
145169
$normalizer =newAbstractObjectNormalizerWithMetadata();
@@ -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)

[8]ページ先頭

©2009-2025 Movatter.jp