Skip to content

[Serializer] preserving empty object breaks current array lists #42282

@Foxprodev

Description

@Foxprodev

Symfony version(s) affected: 5.4

Description
PR #42240 silently breaks BC.

How to reproduce

class Foo {
    public ArrayObject $object;

    public array $list = [];

    public function __construct()
    {
        $this->object = new ArrayObject();
    }
}
$serializer = new Serializer([new ObjectNormalizer()], ['json' => new JsonEncoder()]);
$serializer->serialize(new Foo(), 'json', [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true]);

At 5.3
{"object":{},"list":[]}
At 5.4
{"object":{},"list":{}}

Possible Solution
Create a new context option like ARRAY_FORCE_OBJECT. This option should apply only for current property and do not propagate deeper. This option also should transform plain array to ArrayObject always.

Additional context
Current solution produces many complications with empty array normalization too (described here #42240 (comment))

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