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 e39ff93

Browse filesBrowse files
bug #49700 [Serializer] Fix reindex normalizedData array in AbstractObjectNormalizer::denormalize() (André Laugks)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Serializer] Fix reindex normalizedData array in AbstractObjectNormalizer::denormalize() | Q | A | ------------- | --- | Branch | 6.2 | Bug fix | yes | New feature? | no | Deprecations? | no | Tickets | #49538 | License | MIT | Doc PR | no In the method `AbstractObjectNormalizer::denormalize()` the index of the array `$normalizedData` is reindexed after an `array_merge`. This error occurs when a JSON is deserialised and when the SerializedName is numeric. This results in an incorrect mapping to the properties. ```json { "1": "John", "2": "Doe", "10031": "john.doe@example.com", } ``` Array before merge: ```php array ( 1 => 'John', 2 => 'Doe', 10031 => 'john.doe@example.com', ) ``` After merge with `array_merge`: ```php array ( 0 => 'John', 1 => 'Doe', 2 => 'john.doe@example.com', ) ``` After merge with `array_replace`: ```php array ( 0 => 'John', 1 => 'Doe', 10031 => 'john.doe@example.com', ) ``` All Serializer unittests run successfully. Commits ------- 91d9427 [Serializer] Fix reindex normalizedData array in AbstractObjectNormalizer::denormalize()
2 parents 6f20a87 + 91d9427 commit e39ff93
Copy full SHA for e39ff93

File tree

2 files changed

+27
-1
lines changed
Filter options

2 files changed

+27
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
339339
$normalizedData = $this->removeNestedValue($serializedPath->getElements(), $normalizedData);
340340
}
341341

342-
$normalizedData = array_merge($normalizedData, $nestedData);
342+
$normalizedData = $normalizedData + $nestedData;
343343

344344
$object = $this->instantiateObject($normalizedData, $mappedClass, $context, new \ReflectionClass($mappedClass), $allowedAttributes, $format);
345345
$resolvedClass = ($this->objectClassResolver)($object);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,32 @@ classMetadataFactory: new ClassMetadataFactory(new AnnotationLoader()),
787787
$normalized = $serializer->normalize(new DummyWithEnumUnion(EnumB::B));
788788
$this->assertEquals(new DummyWithEnumUnion(EnumB::B), $serializer->denormalize($normalized, DummyWithEnumUnion::class));
789789
}
790+
791+
public function testDenormalizeWithNumberAsSerializedNameAndNoArrayReindex()
792+
{
793+
$normalizer = new AbstractObjectNormalizerWithMetadata();
794+
795+
$data = [
796+
'1' => 'foo',
797+
'99' => 'baz',
798+
];
799+
800+
$obj = new class() {
801+
/**
802+
* @SerializedName("1")
803+
*/
804+
public $foo;
805+
806+
/**
807+
* @SerializedName("99")
808+
*/
809+
public $baz;
810+
};
811+
812+
$test = $normalizer->denormalize($data, $obj::class);
813+
$this->assertSame('foo', $test->foo);
814+
$this->assertSame('baz', $test->baz);
815+
}
790816
}
791817

792818
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer

0 commit comments

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