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

[Serializer] Fix reindex normalizedData array in AbstractObjectNormalizer::denormalize() #49700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2023

Conversation

elevado
Copy link

@elevado elevado commented Mar 16, 2023

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.

{
   "1":  "John",
   "2": "Doe",
   "10031":  "john.doe@example.com",
}

Array before merge:

array (
  1 => 'John',
  2 => 'Doe',
  10031 => 'john.doe@example.com',
)

After merge with array_merge:

array (
  0 => 'John',
  1 => 'Doe',
  2 => 'john.doe@example.com',
)

After merge with array_replace:

array (
  0 => 'John',
  1 => 'Doe',
  10031 => 'john.doe@example.com',
)

All Serializer unittests run successfully.

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "6.3" but it seems your PR description refers to branch "6.2".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@elevado elevado changed the base branch from 6.3 to 6.2 March 16, 2023 10:41
@elevado
Copy link
Author

elevado commented Sep 22, 2023

@xabbuh / @derrabus

We spoked about this pull request. As discussed, I tested whether this error already existed in version 6.0, however it is only from version 6.2 onwards.

In AbstractObjectNormalizer.php, the block from line 311 to 324 has been added.

However the cause of the error is the array_merge() in line 324.

$mappedClass = $this->getMappedClass($normalizedData, $type, $context);
$nestedAttributes = $this->getNestedAttributes($mappedClass);
$nestedData = [];
$propertyAccessor = PropertyAccess::createPropertyAccessor();
foreach ($nestedAttributes as $property => $serializedPath) {
if (null === $value = $propertyAccessor->getValue($normalizedData, $serializedPath)) {
continue;
}
$nestedData[$property] = $value;
$normalizedData = $this->removeNestedValue($serializedPath->getElements(), $normalizedData);
}
$normalizedData = array_merge($normalizedData, $nestedData);

Therefore, an update from 5.4 LTS to 6.4 LTS will definitely result in a breaking change.

Can you please check and release this pull request as a second reviewer so that this fix goes into 6.4.

@xabbuh xabbuh modified the milestones: 6.4, 6.3 Sep 24, 2023
Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://3v4l.org/CUbsP shows the difference

@xabbuh xabbuh changed the base branch from 6.2 to 6.3 September 24, 2023 16:27
@xabbuh
Copy link
Member

xabbuh commented Sep 24, 2023

@elevado Can you rebase your changes on the 6.3 branch please?

@nicolas-grekas
Copy link
Member

Thank you @elevado.

@nicolas-grekas nicolas-grekas merged commit e39ff93 into symfony:6.3 Sep 29, 2023
@fabpot fabpot mentioned this pull request Sep 30, 2023
fabpot added a commit that referenced this pull request Oct 7, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.