Skip to content

Serializer throw exception when Ignore attribute is used #49710

Closed
@ludekbenedik

Description

@ludekbenedik

Symfony version(s) affected

5.4.21

Description

Hi,
the serializer throws an exception when the Ignore attribute is used and an entity contains a private getter/isser.

How to reproduce

This is OK:

class EntityWithSerializerIgnore
{
    public function getFoo(): int { return 1; }
    public function getBar(): int { return 2; }
    private function isSomething(): bool { return true; }
}

$entity = new EntityWithSerializerIgnore();
$serializer->serialize($entity, 'json'); // {"foo":1,"bar":2}

This throws an exception:

class EntityWithSerializerIgnore
{
    public function getFoo(): int { return 1; }
    #[Ignore]
    public function getBar(): int { return 2; }
    private function isSomething(): bool { return true; }
}

$entity = new EntityWithSerializerIgnore();
$serializer->serialize($entity, 'json'); // throws: Can't get a way to read the property "something" in class "EntityWithSerializerIgnore".

Possible Solution

The AbstractNormalizer::getAllowedAttributes method returns all attributes (public, private) loaded by the AnnotationLoader when the Ignore attribute is used. But the ObjectNormalizer load only public attributes (methods).

The AbstractObjectNormalizer::getAttributes method cannot returns all allowed attributes, but have to combine these attributes with extracted attributes.

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