Skip to content

Commit

Permalink
Merge branch '5.0' into 5.1
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nicolas-grekas committed Jun 7, 2020
2 parents f3ea48e + 38ef53c commit d1151fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
Expand Down Expand Up @@ -158,7 +159,7 @@ public function normalize($data, string $format = null, array $context = [])
}

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

Expand Down
21 changes: 21 additions & 0 deletions Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,27 @@ public function testNotNormalizableValueExceptionMessageForAResource()
(new Serializer())->normalize(tmpfile());
}

public function testNormalizeTransformEmptyArrayObjectToArray()
{
$serializer = new Serializer(
[
new PropertyNormalizer(),
new ObjectNormalizer(),
new ArrayDenormalizer(),
],
[
'json' => new JsonEncoder(),
]
);

$object = [];
$object['foo'] = new \ArrayObject();
$object['bar'] = new \ArrayObject(['notempty']);
$object['baz'] = new \ArrayObject(['nested' => new \ArrayObject()]);

$this->assertSame('{"foo":[],"bar":["notempty"],"baz":{"nested":[]}}', $serializer->serialize($object, 'json'));
}

public function testNormalizePreserveEmptyArrayObject()
{
$serializer = new Serializer(
Expand Down

0 comments on commit d1151fc

Please sign in to comment.