Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c979c0

Browse files
committed
Fix serializer do not transform empty \Traversable to Array
1 parent 0506f8c commit 1c979c0

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ public function normalize($data, $format = null, array $context = [])
157157
}
158158

159159
if (\is_array($data) || $data instanceof \Traversable) {
160+
if ($data instanceof \Countable && 0 === $data->count()) {
161+
return $data;
162+
}
163+
160164
$normalized = [];
161165
foreach ($data as $key => $val) {
162166
$normalized[$key] = $this->normalize($val, $format, $context);
@@ -173,7 +177,7 @@ public function normalize($data, $format = null, array $context = [])
173177
throw new NotNormalizableValueException(sprintf('Could not normalize object of type "%s", no supporting normalizer found.', \get_class($data)));
174178
}
175179

176-
throw new NotNormalizableValueException(sprintf('An unexpected value could not be normalized: %s.', !\is_resource($data) ? var_export($data, true) : sprintf('%s resource', get_resource_type($data))));
180+
throw new NotNormalizableValueException(sprintf('An unexpected value could not be normalized: "%s".', !\is_resource($data) ? var_export($data, true) : sprintf('"%s" resource', get_resource_type($data))));
177181
}
178182

179183
/**

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
2626
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
2727
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
28+
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
2829
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
2930
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
3031
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
@@ -490,6 +491,26 @@ public function testNotNormalizableValueExceptionMessageForAResource()
490491
(new Serializer())->normalize(tmpfile());
491492
}
492493

494+
public function testNormalizePreserveEmptyArrayObject()
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+
$this->assertEquals('{"foo":{},"bar":["notempty"],"baz":{"nested":{}}}', $serializer->serialize($object, 'json', [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true]));
512+
}
513+
493514
private function serializerWithClassDiscriminator()
494515
{
495516
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

0 commit comments

Comments
 (0)