diff --git a/Normalizer/AbstractObjectNormalizer.php b/Normalizer/AbstractObjectNormalizer.php index 527fa8e13..3c790d03d 100644 --- a/Normalizer/AbstractObjectNormalizer.php +++ b/Normalizer/AbstractObjectNormalizer.php @@ -16,6 +16,7 @@ use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; use Symfony\Component\PropertyInfo\Type; use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Encoder\XmlEncoder; use Symfony\Component\Serializer\Exception\ExtraAttributesException; use Symfony\Component\Serializer\Exception\LogicException; use Symfony\Component\Serializer\Exception\NotNormalizableValueException; @@ -411,6 +412,10 @@ private function validateAndDenormalize(string $currentClass, string $attribute, $data = [$data]; } + if (XmlEncoder::FORMAT === $format && '' === $data && Type::BUILTIN_TYPE_ARRAY === $type->getBuiltinType()) { + return []; + } + if (null !== $collectionValueType && Type::BUILTIN_TYPE_OBJECT === $collectionValueType->getBuiltinType()) { $builtinType = Type::BUILTIN_TYPE_OBJECT; $class = $collectionValueType->getClassName().'[]'; diff --git a/Tests/Normalizer/Features/ObjectDummy.php b/Tests/Normalizer/Features/ObjectDummy.php index e12772457..ac610f098 100644 --- a/Tests/Normalizer/Features/ObjectDummy.php +++ b/Tests/Normalizer/Features/ObjectDummy.php @@ -5,6 +5,9 @@ class ObjectDummy { protected $foo; + /** + * @var array + */ public $bar; private $baz; protected $camelCase; diff --git a/Tests/Normalizer/ObjectNormalizerTest.php b/Tests/Normalizer/ObjectNormalizerTest.php index 4d145a5c8..477027cef 100644 --- a/Tests/Normalizer/ObjectNormalizerTest.php +++ b/Tests/Normalizer/ObjectNormalizerTest.php @@ -165,6 +165,19 @@ public function testDenormalize() $this->assertTrue($obj->isBaz()); } + public function testDenormalizeEmptyXmlArray() + { + $normalizer = $this->getDenormalizerForObjectToPopulate(); + $obj = $normalizer->denormalize( + ['bar' => ''], + ObjectDummy::class, + 'xml' + ); + + $this->assertIsArray($obj->bar); + $this->assertEmpty($obj->bar); + } + public function testDenormalizeWithObject() { $data = new \stdClass();