Skip to content

[ObjectSerializer] invalid normalization when isser method and property have the same name #61096

Open
@RafaelKr

Description

@RafaelKr

Symfony version(s) affected

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

No response

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

      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