From 67f49d4c940bf42ea99d882a577e7f59f6639130 Mon Sep 17 00:00:00 2001 From: Jeroen de Graaf Date: Tue, 3 Oct 2023 10:05:29 +0200 Subject: [PATCH] Fix order array sum normalizedData and nestedData Previously, when `array_merge` was changed array+array in 6.3.5, the combined array result is changed as well. array_merge behaves differently than array+array. e.g.: ``` $a = ['key' => 'value-a']; $b = ['key' => 'value-b']; var_dump(array_merge($a, $b)); // Results in: // array(1) { // ["key"]=> // string(7) "value-b" // } var_dump($a + $b); // Results in: // array(1) { // ["key"]=> // string(7) "value-a" // } ``` By switching left with right, the result will be the same again. --- .../Normalizer/AbstractObjectNormalizer.php | 2 +- .../AbstractObjectNormalizerTest.php | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index 069d2e3935f62..e6efb49833d0f 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -339,7 +339,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar $normalizedData = $this->removeNestedValue($serializedPath->getElements(), $normalizedData); } - $normalizedData = $normalizedData + $nestedData; + $normalizedData = $nestedData + $normalizedData; $object = $this->instantiateObject($normalizedData, $mappedClass, $context, new \ReflectionClass($mappedClass), $allowedAttributes, $format); $resolvedClass = ($this->objectClassResolver)($object); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php index 8eb77718c4ac9..97f96635167a7 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php @@ -813,6 +813,28 @@ public function testDenormalizeWithNumberAsSerializedNameAndNoArrayReindex() $this->assertSame('foo', $test->foo); $this->assertSame('baz', $test->baz); } + + public function testDenormalizeWithCorrectOrderOfAttributeAndProperty() + { + $normalizer = new AbstractObjectNormalizerWithMetadata(); + + $data = [ + 'id' => 'root-level-id', + 'data' => [ + 'id' => 'nested-id', + ], + ]; + + $obj = new class() { + /** + * @SerializedPath("[data][id]") + */ + public $id; + }; + + $test = $normalizer->denormalize($data, $obj::class); + $this->assertSame('nested-id', $test->id); + } } class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer