Skip to content

[Serializer] [ObjectNormalizer] Fix isser methods where the property has the same name #61097

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

Draft
wants to merge 2 commits into
base: 6.4
Choose a base branch
from

Conversation

RafaelKr
Copy link

@RafaelKr RafaelKr commented Jul 10, 2025

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

Notes

Although technically this is a bugfix and its pretty much an edge-case, this may affect existing projects and may alter their serialization results. So it could be a breaking change.

Edit: Also see comments below for further edge cases.

Before/After comparison

class MyClass {
  private string $owner = 'foo';
  private bool $isOwner = true;

  public function getOwner(): string {
    return $this->owner;
  }

  public function setOwner(string $owner): void {
    $this->owner = $owner;
  }

  public function isOwner(): bool {
    return $this->isOwner;
  }

  public function setIsOwner(bool $isOwner): void {
    $this->isOwner = $isOwner;
  }
}

$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$normalizer = new ObjectNormalizer($classMetadataFactory);

$object = new MyClass();
$normalized = $normalizer->normalize($object);

Before this PR

It was normalized as:

[
  'owner' => 'foo',
]

Removing the $owner property and getters would result in:

[
  'owner' => true,
]

After this PR

It will be normalized as:

[
  'owner' => 'foo',
  'isOwner'  => true,
]

@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 7.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

@carsonbot carsonbot changed the title [Serializer][ObjectNormalizer] Fix isser methods where the property has the same name [Serializer] [ObjectNormalizer] Fix isser methods where the property has the same name Jul 10, 2025
@RafaelKr
Copy link
Author

RafaelKr commented Jul 10, 2025

Another Edge-Case

After I just found #61023 I also tried having properties and getter methods called isFoo and canFoo (no getFoo at this time) in the ObjectWithBooleanPropertyAndIsserWithSameName class:

class ObjectWithBooleanPropertyAndIsserWithSameName
{
    private $foo = 'foo';

    private $isFoo = true;

    private $canFoo = false;

    public function isFoo()
    {
        return $this->isFoo;
    }

    public function canFoo()
    {
        return $this->canFoo;
    }
}

This results in

[
  'isFoo' => true,
  'foo' => true, // it's using the isFoo isser method!
]

Notice, that for the canFoo property it's using the isFoo method to get the value and prints foo as result name.

Reading through the linked PR I wonder if my PR also classifies as "new feature" instead of "bugfix"?
cc @nicolas-grekas

@RafaelKr
Copy link
Author

RafaelKr commented Jul 10, 2025

Extended Edge-Case

class ObjectWithBooleanPropertyAndIsserWithSameName
{
    public $foo = 'foo';

    private $getFoo = 'getFoo';

    private $hasFoo = 'hasFoo';

    private $canFoo = 'canFoo';

    private $isFoo = 'isFoo';

    public function getFoo()
    {
        return $this->getFoo;
    }

    public function hasFoo()
    {
        return $this->hasFoo;
    }

    public function canFoo()
    {
        return $this->canFoo;
    }

    public function isFoo()
    {
        return $this->isFoo;
    }
}

Results in

[
  'foo' => 'getFoo',
  'isFoo' => 'isFoo'
]

So we may also want to add the $reflClass->hasProperty($name) check I added for issers for get, has and can?

That would result in:

[
    'getFoo' => 'getFoo',
    'hasFoo' => 'hasFoo',
    'canFoo' => 'canFoo',
    'isFoo' => 'isFoo',
    'foo' => 'getFoo'
]

@RafaelKr RafaelKr marked this pull request as draft July 10, 2025 15:08
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.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy