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 44c7114

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 9c2a9c8 commit 44c7114
Copy full SHA for 44c7114

File tree

3 files changed

+24
-2
lines changed
Filter options

3 files changed

+24
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,14 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
422422
// if a value is meant to be a string, float, int or a boolean value from the serialized representation.
423423
// That's why we have to transform the values, if one of these non-string basic datatypes is expected.
424424
if (\is_string($data) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
425-
if ('' === $data && $type->isNullable() && \in_array($type->getBuiltinType(), [Type::BUILTIN_TYPE_BOOL, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT], true)) {
426-
return null;
425+
if ('' === $data) {
426+
if (Type::BUILTIN_TYPE_ARRAY === $builtinType = $type->getBuiltinType()) {
427+
return [];
428+
}
429+
430+
if ($type->isNullable() && \in_array($builtinType, [Type::BUILTIN_TYPE_BOOL, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT], true)) {
431+
return null;
432+
}
427433
}
428434

429435
switch ($type->getBuiltinType()) {

‎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
@@ -164,6 +164,19 @@ public function testDenormalize()
164164
$this->assertTrue($obj->isBaz());
165165
}
166166

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.