Skip to content

Commit

Permalink
bug symfony#43205 [Serializer] Fix denormalizing XML array with empty…
Browse files Browse the repository at this point in the history
… body (4.4) (alexandre-daubois)

This PR was merged into the 4.4 branch.

Discussion
----------

[Serializer] Fix denormalizing XML array with empty body (4.4)

This happens for example with XML empty tags denormalizing to an object array attribute.

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix symfony#43193 on Symfony 4.4
| License       | MIT
| Doc PR        | _NA_

Not sure how to deal with versions differences as they differ just a little bit. Anyway, here's the fix for 4.4 and you can find the fix for 5.x on symfony#43204 😄

Commits
-------

d6b7e2d [Serializer] Fix denormalizing of array with empty body
  • Loading branch information
fabpot committed Sep 29, 2021
2 parents ed494fc + d6b7e2d commit 0eac61e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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().'[]';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
class ObjectDummy
{
protected $foo;
/**
* @var array
*/
public $bar;
private $baz;
protected $camelCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 0eac61e

Please sign in to comment.