Skip to content

Commit 38ef53c

Browse files
Merge branch '4.4' into 5.0
* 4.4: [Mime] Remove unused var [HttpClient] fix monitoring timeouts when other streams are active [PhpUnitBridge] fix syntax on PHP 5.3 [PhpUnitBridge] Fix undefined index when output of "composer show" cannot be parsed properly cascade validation to child forms [PhpUnitBridge] fix undefined var on version 3.4 Move ajax clear event listener initialization on loadToolbar [HttpClient] Throw JsonException instead of TransportException on empty response in Response::toArray() take into account the context when preserving empty array objects [VarExporter] tfix: s/markAsSkipped/markTestSkipped/ bumped Symfony version to 4.4.10 updated VERSION for 4.4.9 updated CHANGELOG for 4.4.9 bumped Symfony version to 3.4.42 updated VERSION for 3.4.41 update CONTRIBUTORS for 3.4.41 updated CHANGELOG for 3.4.41
2 parents 6417fa4 + a91ceee commit 38ef53c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Serializer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Serializer\Exception\LogicException;
2222
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
2323
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
24+
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
2425
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
2526
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
2627
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
@@ -151,7 +152,7 @@ public function normalize($data, string $format = null, array $context = [])
151152
}
152153

153154
if (\is_array($data) || $data instanceof \Traversable) {
154-
if ($data instanceof \Countable && 0 === $data->count()) {
155+
if (($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) === true && $data instanceof \Countable && 0 === $data->count()) {
155156
return $data;
156157
}
157158

Tests/SerializerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,27 @@ public function testNotNormalizableValueExceptionMessageForAResource()
489489
(new Serializer())->normalize(tmpfile());
490490
}
491491

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

0 commit comments

Comments
 (0)