8000 bug #37049 [Serializer] take into account the context when preserving… · symfony/symfony@a2f4342 · GitHub
[go: up one dir, main page]

Skip to content

Commit a2f4342

Browse files
committed
bug #37049 [Serializer] take into account the context when preserving empty array objects (xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- [Serializer] take into account the context when preserving empty array objects | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #37041 | License | MIT | Doc PR | Commits ------- 98fff21 take into account the context when preserving empty array objects
2 parents d87b666 + 98fff21 commit a2f4342

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Symfony/Component/Serializer/Serializer.php

Lines 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

Lines 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