Skip to content

Commit

Permalink
Fix order array sum normalizedData and nestedData
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jerowork committed Oct 3, 2023
1 parent 855fc05 commit 94236cb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Normalizer/AbstractObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions Tests/Normalizer/AbstractObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 94236cb

Please sign in to comment.