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

Skip to content

[Serializer] Fix ObjectNormalizer with property path #57187

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
Jun 16, 2024

Conversation

HypeMC
Copy link
Contributor

@HypeMC HypeMC commented May 26, 2024

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #54983
License MIT

Caused by #52917.

The ObjectNormalizer::isAllowedAttribute() method doesn't work with property paths, this is an attempt to fix the problem.

|| $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
}

if (str_contains($attribute, '.')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know a better way to fix this. The $this->propertyAccessor->isWritable() method expects an object, but the isAllowedAttribute() method is called before the object is instantiated.

Perhaps the $this->propertyAccessor->isReadable() check should be removed as well, and the method should simply return true if the attribute contains a .?

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, I think that when can only rely on the . in the first place, as it'll stick a bit more with the legacy behavior.

But later on I do think that it can be great to be able to know if foo.bar.baz is readable/writable only based on reflection, so we have a more accurate behavior.

@bendavies
Copy link
Contributor

thanks for taking a look at this.
@mtarld can you take a look?

Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

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

Works for me as-is on 5.4.

@@ -194,7 +194,12 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
$class = \is_object($classOrObject) ? \get_class($classOrObject) : $classOrObject;

if ($context['_read_attributes'] ?? true) {
return $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
return (\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute))
|| $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
Copy link
Member

Choose a reason for hiding this comment

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

return (\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute)) || $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@xabbuh
Copy link
Member

xabbuh commented Jun 16, 2024

works for me as well with a minor code style change

@HypeMC HypeMC force-pushed the fix-objectnormalizer-with-property-path branch from 62efed3 to 3857545 Compare June 16, 2024 14:41
@fabpot
Copy link
Member

fabpot commented Jun 16, 2024

Thank you @HypeMC.

@fabpot fabpot merged commit 8e13b17 into symfony:5.4 Jun 16, 2024
10 of 12 checks passed
@HypeMC HypeMC deleted the fix-objectnormalizer-with-property-path branch June 16, 2024 16:12
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.

7 participants