Skip to content

Serializer throw exception when Ignore attribute is used #49710

@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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy