Skip to content

Conversation

@Link1515
Copy link
Contributor

@Link1515 Link1515 commented Oct 8, 2025

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #61988
License MIT

Introduce hasParentProperty() to detect and use properties defined in parent classes.

@Link1515 Link1515 requested a review from dunglas as a code owner October 8, 2025 12:17
@carsonbot carsonbot added this to the 6.4 milestone Oct 8, 2025
@OskarStark OskarStark changed the title [Serializer] fix Inherited properties normalization [Serializer] fix inherited properties normalization Oct 8, 2025
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, here are some minor CS suggestions

@Link1515 Link1515 force-pushed the fix/Serializer branch 2 times, most recently from 8d8d206 to 4a93d86 Compare October 9, 2025 14:06
@frozenbrain
Copy link

I would suggest limiting the property lookup to the class the getter was declared in ($reflMethod->getDeclaringClass()) and maybe its parents.

Otherwise the results could change by introducing new properties in child classes, which could cause confusion:

class Base
{
    private bool $test = false;

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

    public function setTest(bool $test): void
    {
        $this->test = $test;
    }
}

class Child extends Base {
    private bool $isTest = true;
}

$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
echo $serializer->serialize(new Base(), 'json') . PHP_EOL;  // {"test":false}
echo $serializer->serialize(new Child(), 'json') . PHP_EOL; // {"isTest":false}

This is, of course, just a theoretical example, but I can't think of a case in which this behavior could be useful and starting at the most likely correct class would reduce the number of checks/iterations necessary considerably for classes with many inherited properties.

Another thing: I think for consistency's sake the check in line 115 should behave the same way.

Copy link

@jolelievre jolelievre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I don't have the rights to approve this, but the related bug #61988 is preventing us from updating the latest patch

So +1 on this one, it seems fine, but I'm not an expert 😅

The failing tests don't seem to be related to the PR though 🤔 Could there be some radon fails on some environments and re-running the tests would succeed?

@Link1515
Copy link
Contributor Author

Link1515 commented Nov 7, 2025

I would suggest limiting the property lookup to the class the getter was declared in ($reflMethod->getDeclaringClass()) and maybe its parents.

Otherwise the results could change by introducing new properties in child classes, which could cause confusion:

class Base
{
    private bool $test = false;

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

    public function setTest(bool $test): void
    {
        $this->test = $test;
    }
}

class Child extends Base {
    private bool $isTest = true;
}

$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
echo $serializer->serialize(new Base(), 'json') . PHP_EOL;  // {"test":false}
echo $serializer->serialize(new Child(), 'json') . PHP_EOL; // {"isTest":false}

This is, of course, just a theoretical example, but I can't think of a case in which this behavior could be useful and starting at the most likely correct class would reduce the number of checks/iterations necessary considerably for classes with many inherited properties.

Another thing: I think for consistency's sake the check in line 115 should behave the same way.

@frozenbrain, thank you for the feedback, I've made the changes.

@nicolas-grekas
Copy link
Member

Thank you @Link1515.

@nicolas-grekas nicolas-grekas merged commit fc41d4f into symfony:6.4 Nov 12, 2025
11 checks passed
This was referenced Nov 13, 2025
This was referenced Dec 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants