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

Skip to content

[Serializer] fix context attribute with serializedName #48661

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
Dec 16, 2022
Merged

[Serializer] fix context attribute with serializedName #48661

merged 1 commit into from
Dec 16, 2022

Conversation

nikophil
Copy link
Contributor

Q A
Branch? 6.2
Bug fix? yes
New feature? no
Deprecations? no
License MIT

Hello,

this PR fixes a bug where the #[Context] attribute is not read in denormalization process when it is used along with #[SerializedName] in a constructor.

In the test I provided, the two dates are not equals without the fix, although the same context is provided.

@@ -331,8 +331,8 @@ protected function instantiateObject(array &$data, string $class, array &$contex
$params = [];
foreach ($constructorParameters as $constructorParameter) {
$paramName = $constructorParameter->name;
$attributeContext = $this->getAttributeDenormalizationContext($class, $paramName, $context);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

here getAttributeDenormalizationContext() was getting passed $key which represents the serializedName, although we want the name of the property, to access its metadata

@mtarld
Copy link
Contributor

mtarld commented Dec 15, 2022

It is quite strange that it do the exact opposite of what #46847 does

@nicolas-grekas
Copy link
Member

Let's add a test case that covers the target behavior in #46847, this might help sort this out.

@nikophil
Copy link
Contributor Author

nikophil commented Dec 15, 2022

@nicolas-grekas code style fixed 👍

I'm not sure what else to test. #46847 says:

I realized we can't use the Context attribute context in an AdvancedNameConverterInterface::denormalize()

but we only pass through AdvancedNameConverterInterface::normalize() here.

And the PR mentions #46525 which says:

Property specific denormalization context doesn't work when property name in the array being denormalized is different from property name in the class.

which is exactly what my PR does and tests...

Copy link
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@ogizanagi ogizanagi left a comment

Choose a reason for hiding this comment

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

I guess things goes wrong in my mind when reading the bug fix in #46525 and proofreading #46680 after making the suggestion of adding the attribute-specific context to the name converter.

The diff between #46525 and this is #46525 is iterating the data keys to be denormalized to its properties, while this PR (and originally #46680) goes the other way around, iterating the constructor properties to be normalized to their counterpart in $data.

Anyway, LGTM. Thank you @nikophil

@ogizanagi ogizanagi merged commit 7df2468 into symfony:6.2 Dec 16, 2022
@fabpot fabpot mentioned this pull request Dec 16, 2022
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