Skip to content

[Serializer] Fix for method named get() #58226

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

[Serializer] Fix for method named get() #58226

merged 1 commit into from
Sep 16, 2024

Conversation

mihai-stancu
Copy link
Contributor

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

During normalization, if a class has a method called get that method will get confused for an accessor and it will go through logic relating to property-access for the property named substr('get', 3).

Eventually a new PropertyPath instance will be created with an empty path which will trigger an exception.

$serializer->serialize(new class { 
    public function get() {} 
}, 'json');

@mihai-stancu
Copy link
Contributor Author

I'll add a test for this tomorrow.

@OskarStark OskarStark changed the title [bugfix] Serializer fails on method named 'get' [Serializer] Fix for method named get() Sep 10, 2024
Comment on lines 103 to 105
if ($name !== 'get' && str_starts_with($name, 'get')
|| $name !== 'has' && str_starts_with($name, 'has')
|| $name !== 'can' && str_starts_with($name, 'can')) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if ($name !== 'get' && str_starts_with($name, 'get')
|| $name !== 'has' && str_starts_with($name, 'has')
|| $name !== 'can' && str_starts_with($name, 'can')) {
if (3 < \strlen($name) && (...)) {

@nicolas-grekas
Copy link
Member

Thank you @mihai-stancu.

@nicolas-grekas nicolas-grekas merged commit c49d6db into symfony:6.4 Sep 16, 2024
9 of 10 checks passed
@mihai-stancu mihai-stancu deleted the bugfix/serializer-fails-on-method-named-get branch September 16, 2024 16:14
This was referenced Sep 21, 2024
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