Description
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