Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix order array sum normalizedData and nestedData #51825

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

Conversation

jerowork
Copy link
Contributor

@jerowork jerowork commented Oct 3, 2023

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:

{
  "data": {
    "item": {
      "id": "id-1"
    }
  },
  "id": "id-2"
}
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:

$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:

- $normalizedData = $normalizedData + $nestedData;
+ $normalizedData = $nestedData + $normalizedData;

This would result in the same, while keeping the fix (#49700) for the numeric key value

@carsonbot
Copy link

Hey!

To help keep things organized, we don't allow "Draft" pull requests. Could you please click the "ready for review" button or close this PR and open a new one when you are done?

Note that a pull request does not have to be "perfect" or "ready for merge" when you first open it. We just want it to be ready for a first review.

Cheers!

Carsonbot

@jerowork jerowork marked this pull request as ready for review October 3, 2023 08:11
@jerowork jerowork requested a review from dunglas as a code owner October 3, 2023 08:11
@carsonbot carsonbot added this to the 6.3 milestone Oct 3, 2023
@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

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.
@jerowork jerowork force-pushed the jerowork/fix/change-order-array-plus-on-object-deserialization branch from 158ca81 to 67f49d4 Compare October 3, 2023 08:16
@jerowork jerowork changed the title Fix order array sum normalizedData and nestedData [Serializer] Fix order array sum normalizedData and nestedData Oct 3, 2023
@HypeMC
Copy link
Contributor

HypeMC commented Oct 3, 2023

I've encountered the same issue and can confirm that this fixes it without (at least in my case) introducing any additional problems.

@carsonbot carsonbot changed the title [Serializer] Fix order array sum normalizedData and nestedData Fix order array sum normalizedData and nestedData Oct 7, 2023
@fabpot
Copy link
Member

fabpot commented Oct 7, 2023

Thank you @jerowork.

@fabpot fabpot merged commit bfd2f7d into symfony:6.3 Oct 7, 2023
@fabpot fabpot mentioned this pull request Oct 21, 2023
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.

5 participants