Skip to content

Commit d1151fc

Browse files
Merge branch '5.0' into 5.1
* 5.0: [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 5.0.10 updated VERSION for 5.0.9 updated CHANGELOG for 5.0.9 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 f3ea48e + 38ef53c commit d1151fc

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;
@@ -158,7 +159,7 @@ public function normalize($data, string $format = null, array $context = [])
158159
}
159160

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

Tests/SerializerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,27 @@ public function testNotNormalizableValueExceptionMessageForAResource()
492492
(new Serializer())->normalize(tmpfile());
493493
}
494494

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

0 commit comments

Comments
 (0)