Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 98fff21

Browse filesBrowse files
committed
take into account the context when preserving empty array objects
1 parent d87b666 commit 98fff21
Copy full SHA for 98fff21

File tree

Expand file treeCollapse file tree

2 files changed

+23
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+23
-1
lines changed

‎src/Symfony/Component/Serializer/Serializer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Serializer.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Serializer\Exception\LogicException;
2121
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
2222
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
23+
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
2324
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
2425
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
2526
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
@@ -157,7 +158,7 @@ public function normalize($data, $format = null, array $context = [])
157158
}
158159

159160
if (\is_array($data) || $data instanceof \Traversable) {
160-
if ($data instanceof \Countable && 0 === $data->count()) {
161+
if (($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) === true && $data instanceof \Countable && 0 === $data->count()) {
161162
return $data;
162163
}
163164

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,27 @@ public function testNotNormalizableValueExceptionMessageForAResource()
491491
(new Serializer())->normalize(tmpfile());
492492
}
493493

494+
public function testNormalizeTransformEmptyArrayObjectToArray()
495+
{
496+
$serializer = new Serializer(
497+
[
498+
new PropertyNormalizer(),
499+
new ObjectNormalizer(),
500+
new ArrayDenormalizer(),
501+
],
502+
[
503+
'json' => new JsonEncoder(),
504+
]
505+
);
506+
507+
$object = [];
508+
$object['foo'] = new \ArrayObject();
509+
$object['bar'] = new \ArrayObject(['notempty']);
510+
$object['baz'] = new \ArrayObject(['nested' => new \ArrayObject()]);
511+
512+
$this->assertSame('{"foo":[],"bar":["notempty"],"baz":{"nested":[]}}', $serializer->serialize($object, 'json'));
513+
}
514+
494515
public function testNormalizePreserveEmptyArrayObject()
495516
{
496517
$serializer = new Serializer(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.