Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[ObjectSerializer] invalid normalization when getter-like method (isser, hasser, canner) and property have the same name #61096

Copy link
Copy link
@RafaelKr

Description

@RafaelKr
Issue body actions

Symfony version(s) affected

Since the introduction of Serializer up until 7.4+

Description

When we create a class property where the name starts with is (followed by an uppercase letter) and we create a getter method of the same name, the serializer will remove the is prefix in the normalized array.

E.g. having the following class:

class MyClass {
  private bool $isOwner = true;

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

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

Doing a serialization to JSON results in

{
  "owner": true
}

If we try to denormalize it again, the property will be ignored. It works if the key in the JSON object is named isOwner, though.

This means normalization and denormalization are inconsistent.

Sidenote: When using the JetBrains IDE Generate Getter feature on a boolean property called like $isX, it will automatically create an isser method with the same name like the property. This is how I ran into this.

How to reproduce

Use MyClass from the description and run

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

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

/*
inspecting $normalized in the debugger:

$normalized = [
  'owner' => true
]
*/

Possible Solution

I propose to keep the is prefix in the resulting attribute, if there is a property exactly named like this. I will create a PR for that.

Additional Context

Edit: Also see PR #61097 for further getter-like methods where this happens: getters, issers, hassers, canners.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.