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

ObjectNormalizer doesn't deal with scope of get() functions correctly #58041

Copy link
Copy link
Closed as not planned
@vidarl

Description

@vidarl
Issue body actions

Symfony version(s) affected

5.4.41 and later (5.4.42 is latest ATM):

Description

Introduced in 5.4.41, the ObjectNormalizer do not take scope of get functions properly. Protected properties will be attempt fetched but not included in the exported data anyway

How to reproduce

The following code will work on 5.4.40 and earlier. Inline comment describes what happens when it fails in 5.4.41 and later :

class SomeData
{
    protected string $data;

    public function __construct(string $data)
    {
        $this->data = $data;
    }

    protected function getSomethingProtected(): array
    {
        return "protected";
    }

    public function __get($property)
    {
        if (property_exists($this, $property)) {
            return $this->$property;
        }
        throw new \Exception("Unknown property $property"); // <------- Since v5.4.41 it will fail here because getSomethingProtected() has protected scope
    }

    public function getData(): string
    {
        return $this->data;
    }
}
        $someData = new SomeData('foobar');
        $jsonContent = $this->serializer->serialize($someData, 'json');

If I change __get() to anyway return the property somethingProtected instead of throwing an exception, it will anyway not be included in the serialized data (which is correct and inline with previous versions )

Possible Solution

Problem was introduced by #57187
The change in Normalizer/ObjectNormalizer.php in symfony/serializer@296df0c is causing the problem

Additional Context

FYI : #58012 seems to be related

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.