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

[Serializer] Inherited "is"-properties are normalized incorrectly #61988

Copy link
Copy link
@frozenbrain

Description

@frozenbrain
Issue body actions

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

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.