Skip to content

Commit 5cb0dc3

Browse files
[Serializer] Fix denormalizing of array with empty body
This happens for example with XML empty tags denormalizing to an object array attribute.
1 parent a10b610 commit 5cb0dc3

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Normalizer/AbstractObjectNormalizer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
1717
use Symfony\Component\PropertyInfo\Type;
1818
use Symfony\Component\Serializer\Encoder\JsonEncoder;
19+
use Symfony\Component\Serializer\Encoder\XmlEncoder;
1920
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
2021
use Symfony\Component\Serializer\Exception\LogicException;
2122
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
@@ -411,6 +412,10 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
411412
$data = [$data];
412413
}
413414

415+
if (XmlEncoder::FORMAT === $format && '' === $data && Type::BUILTIN_TYPE_ARRAY === $type->getBuiltinType()) {
416+
return [];
417+
}
418+
414419
if (null !== $collectionValueType && Type::BUILTIN_TYPE_OBJECT === $collectionValueType->getBuiltinType()) {
415420
$builtinType = Type::BUILTIN_TYPE_OBJECT;
416421
$class = $collectionValueType->getClassName().'[]';

Tests/Normalizer/Features/ObjectDummy.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
class ObjectDummy
66
{
77
protected $foo;
8+
/**
9+
* @var array
10+
*/
811
public $bar;
912
private $baz;
1013
protected $camelCase;

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ public function testDenormalize()
165165
$this->assertTrue($obj->isBaz());
166166
}
167167

168+
public function testDenormalizeEmptyXmlArray()
169+
{
170+
$normalizer = $this->getDenormalizerForObjectToPopulate();
171+
$obj = $normalizer->denormalize(
172+
['bar' => ''],
173+
ObjectDummy::class,
174+
'xml'
175+
);
176+
177+
$this->assertIsArray($obj->bar);
178+
$this->assertEmpty($obj->bar);
179+
}
180+
168181
public function testDenormalizeWithObject()
169182
{
170183
$data = new \stdClass();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy