-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Closed
Description
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))