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 bfd2f7d

Browse filesBrowse files
committed
bug #51825 Fix order array sum normalizedData and nestedData (jerowork)
This PR was merged into the 6.3 branch. Discussion ---------- Fix order array sum normalizedData and nestedData | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #51823 | License | MIT ### Description With the update of Serializer to 6.3.5, some deserialization of array to objects does behave differently (changed order of priority of configuration via attribute `#[SerializedPath]` vs. property name, when there is a key on root level with the same name as the private property. Related to #49700. ### How to reproduce Example to explain changed behavior: ```json { "data": { "item": { "id": "id-1" } }, "id": "id-2" } ``` ```php final class SomeEvent { #[SerializedPath('[data][item][id]')] public string $id; } ``` Before 6.3.5, the value of the id was `id-1`, with the change of #49700, the value of the id becomes `id-2`. #49700 changes `array_merge` with `array + array`. It seems that the problem stated above is related to the fact that array_merge does overwrite keys differently than array + array: ```php $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" // } ``` ### Solution As `array_merge` does behave slightly differently that array + array, the solution could be to switch array order to: ```diff - $normalizedData = $normalizedData + $nestedData; + $normalizedData = $nestedData + $normalizedData; ``` This would result in the same, while keeping the fix (#49700) for the numeric key value Commits ------- 67f49d4 Fix order array sum normalizedData and nestedData
2 parents 82f21e8 + 67f49d4 commit bfd2f7d
Copy full SHA for bfd2f7d

File tree

2 files changed

+23
-1
lines changed
Filter options

2 files changed

+23
-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 = $normalizedData + $nestedData;
342+
$normalizedData = $nestedData + $normalizedData;
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
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,28 @@ public function testDenormalizeWithNumberAsSerializedNameAndNoArrayReindex()
813813
$this->assertSame('foo', $test->foo);
814814
$this->assertSame('baz', $test->baz);
815815
}
816+
817+
public function testDenormalizeWithCorrectOrderOfAttributeAndProperty()
818+
{
819+
$normalizer = new AbstractObjectNormalizerWithMetadata();
820+
821+
$data = [
822+
'id' => 'root-level-id',
823+
'data' => [
824+
'id' => 'nested-id',
825+
],
826+
];
827+
828+
$obj = new class() {
829+
/**
830+
* @SerializedPath("[data][id]")
831+
*/
832+
public $id;
833+
};
834+
835+
$test = $normalizer->denormalize($data, $obj::class);
836+
$this->assertSame('nested-id', $test->id);
837+
}
816838
}
817839

818840
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.