Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Normalizer incorrectly caching allowedAttributes when typed properties are used. #36594

Closed
@fl0cke

Description

@fl0cke

Symfony version(s) affected: 5.0.5

Description
When using typed properties in a class, the serializer correctly skips those that are not initialized yet. However, when serializing a collection of objects that are instances of the same class, the serializer's attribute caching kicks in, which may lead to "Typed property must not be accessed before initialization" errors, e.g. when property "a" of object 1 is initialized, but on object 2 it is not.

How to reproduce

class Test {
    public string $a;
    public string $b;
}

$test1 = new Test();
$test1->a = "a";
$test1->b = "b";

$test2 = new Test();
$test2->a = "a";

$objects = [$test1, $test2];

$serializer->serialize($objects, "json");

(where $serializer is the default serializer service that is configured by Symfony when enabling the serializer component)

Possible Solution
The "allowed" status of typed properties must not be cached and checked every time an object is serialized.

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