-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Symfony version(s) affected
6.4.26, 7.3.4
Description
The changes introduced in #61097 appear not to properly handle inherited properties.
In earlier versions of the serializer, "is"-properties from both the serialized class and any parent classes were (incorrectly) normalized without the "is" prefix, if the getter had the same name as the property, as is often the case with "isser" methods. The fix introduced by the aforementioned PR currently only works when the property is declared in the normalized class itself. If the property is declared in a parent class, the old behavior still occurs.
How to reproduce
composer require symfony/property-access symfony/serializer
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class Base
{
private bool $isTest = false;
public function isTest(): bool
{
return $this->isTest;
}
public function setIsTest(bool $test): void
{
$this->isTest = $test;
}
}
class Child extends Base {}
$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
echo $serializer->serialize(new Base(), 'json') . PHP_EOL;
echo $serializer->serialize(new Child(), 'json') . PHP_EOL;
Old output (up until 6.4.25):
{"test":false}
{"test":false}
New output:
{"isTest":false}
{"test":false}
Expected output:
{"isTest":false}
{"isTest":false}
Possible Solution
I only briefly looked at the implementation, but I believe that $reflClass->hasProperty(...)
returns false
for inherited properties and should therefore be replaced with something like $reflMethod->getDeclaringClass()->hasProperty(...)
. I am not sure if this has any other implications though.
Additional Context
No response