Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Serializer] BC - Revert of [bug #54635] broke normalization in our entities #54933

Open
vsemak opened this issue May 15, 2024 · 1 comment
Open

Comments

@vsemak
Copy link

vsemak commented May 15, 2024

Symfony version(s) affected

5.4.39

Description

Object deserialization ends up in data lose. Solution worked in all previous versions of symfony/serializer before 5.4.39.

How to reproduce

We need object with reference to list of other objects eg. Room with Participant[].

class Participant
{
    private string $name;

    public function __construct(array $data)
    {
        $this->name = $data['name'] ?? '';
    }

    public function setName(string $name): self
    {
        $this->name = $name;
        return $this;
    }

    public function getName()
    {
        return $this->name;
    }
}

class Room
{
    /** @var Participant[] */
    private array $participants;

    public function __construct(array $data)
    {
        $this->participants = $data['participants'] ?? [];
    }

    public function getParticipants(): array
    {
        return $this->participants;
    }

    public function setParticipants(array $participants): self
    {
        $this->participants = $participants;
        return $this;
    }
}

Serializer configuration:

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$encoders = [new XmlEncoder(), new JsonEncoder()];

$normalizers = [
    new DateTimeNormalizer(),
    new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor()),
    new ObjectNormalizer(
        $classMetadataFactory,
        null,
        null,
        new PhpDocExtractor()
    ),
    new ArrayDenormalizer(),
];

$serializer = new Serializer($normalizers, $encoders);

Possible Solution

To solve issue we need to switch GetSetNormalizer with ObjectNormalizer:

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$encoders = [new XmlEncoder(), new JsonEncoder()];

$normalizers = [
    new DateTimeNormalizer(),
-   new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor()),
    new ObjectNormalizer(
        $classMetadataFactory,
        null,
        null,
        new PhpDocExtractor()
    ),
+   new GetSetMethodNormalizer($classMetadataFactory, null, new PhpDocExtractor()),
    new ArrayDenormalizer(),
];

$serializer = new Serializer($normalizers, $encoders);

Additional Context

No response

@xabbuh
Copy link
Member

xabbuh commented May 15, 2024

Object deserialization ends up in data lose. Solution worked in all previous versions of symfony/serializer before 5.4.39.

This conclusion doesn't look right to me. The commit that was reverted in #54635 has been introduced in 5.4.38 which means that your example couldn't have worked in 5.4.37.

With that being said, can it be that the issue your experience is caused by another change? Can you explain a bit more how the bug manifests (i.e. what behaviour do you observe and what did you expect instead)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants