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

Skip to content

Commit 54a3436

Browse files
committed
[Serializer] Deprecate support for returning empty, iterable, countable, raw object when normalizing
1 parent 682b76f commit 54a3436

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add support of PHP backed enumerations
88
* Add support for serializing empty array as object
9+
* Deprecate support for returning empty, iterable, countable, raw object when normalizing
910

1011
5.3
1112
---

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ public function normalize($data, string $format = null, array $context = [])
168168
if (is_countable($data) && 0 === \count($data)) {
169169
switch (true) {
170170
case ($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && \is_object($data):
171+
if (!$data instanceof \ArrayObject) {
172+
trigger_deprecation('symfony/serializer', '5.4', 'The method "%s()" will return an instance of "%s" as of Symfony 6.0 when the object is iteratable, countable and empty.', __METHOD__, \ArrayObject::class);
173+
}
174+
171175
return $data;
172176
case ($context[self::EMPTY_ARRAY_AS_OBJECT] ?? false) && \is_array($data):
173177
return new \ArrayObject();

src/Symfony/Component/Serializer/Tests/SerializerTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,10 @@ public function testNormalizeWithCollection(Serializer $serializer, array $data)
578578
$this->assertSame($expected, $serializer->serialize($data, 'json'));
579579
}
580580

581-
/** @dataProvider provideObjectOrCollectionTests */
581+
/**
582+
* @dataProvider provideObjectOrCollectionTests
583+
* @group legacy
584+
*/
582585
public function testNormalizePreserveEmptyArrayObject(Serializer $serializer, array $data)
583586
{
584587
$expected = '{"a1":{},"a2":{"k":"v"},"b1":[],"b2":{"k":"v"},"c1":{"nested":{}},"c2":{"nested":{"k":"v"}},"d1":{"nested":[]},"d2":{"nested":{"k":"v"}},"e1":{"map":[]},"e2":{"map":{"k":"v"}},"f1":{"map":{}},"f2":{"map":{"k":"v"}},"g1":{"list":{"list":[]},"settings":[]},"g2":{"list":["greg"],"settings":[]}}';
@@ -596,7 +599,10 @@ public function testNormalizeEmptyArrayAsObject(Serializer $serializer, array $d
596599
]));
597600
}
598601

599-
/** @dataProvider provideObjectOrCollectionTests */
602+
/**
603+
* @dataProvider provideObjectOrCollectionTests
604+
* @group legacy
605+
*/
600606
public function testNormalizeEmptyArrayAsObjectAndPreserveEmptyArrayObject(Serializer $serializer, array $data)
601607
{
602608
$expected = '{"a1":{},"a2":{"k":"v"},"b1":{},"b2":{"k":"v"},"c1":{"nested":{}},"c2":{"nested":{"k":"v"}},"d1":{"nested":{}},"d2":{"nested":{"k":"v"}},"e1":{"map":{}},"e2":{"map":{"k":"v"}},"f1":{"map":{}},"f2":{"map":{"k":"v"}},"g1":{"list":{"list":[]},"settings":{}},"g2":{"list":["greg"],"settings":{}}}';

0 commit comments

Comments
 (0)