Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d6b7e2d

Browse filesBrowse 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 5c5fd0c commit d6b7e2d
Copy full SHA for d6b7e2d

File tree

3 files changed

+21
-0
lines changed
Filter options

3 files changed

+21
-0
lines changed

‎src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+5Lines 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().'[]';

‎src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectDummy.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectDummy.php
+3Lines 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;

‎src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php
+13Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.