Skip to content

Commit

Permalink
[Serializer] add a context key to return always as collection for Xml…
Browse files Browse the repository at this point in the history
…Encoder
  • Loading branch information
Amrouche Hamza committed Feb 16, 2018
1 parent b0facfe commit adb428d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private function parseXmlValue(\DOMNode $node, array $context = array())
}

foreach ($value as $key => $val) {
if (\is_array($val) && 1 === \count($val)) {
if (empty($context['as_collection']) && \is_array($val) && 1 === \count($val)) {
$value[$key] = current($val);
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,33 @@ public function testDecodeIgnoreWhiteSpace()
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
}

public function testDecodeAlwaysAsCollection()
{
$this->encoder = new XmlEncoder('response', null);
$serializer = new Serializer(array(new CustomNormalizer()), array('xml' => new XmlEncoder()));
$this->encoder->setSerializer($serializer);

$source = <<<'XML'
<?xml version="1.0"?>
<order_rows nodeType="order_row" virtualEntity="true">
<order_row>
<id><![CDATA[16]]></id>
<test><![CDATA[16]]></test>
</order_row>
</order_rows>
XML;
$expected = array(
'@nodeType' => 'order_row',
'@virtualEntity' => 'true',
'order_row' => array(array(
'id' => array(16),
'test' => array(16),
)),
);

$this->assertEquals($expected, $this->encoder->decode($source, 'xml', array('as_collection' => true)));
}

public function testDecodeWithoutItemHash()
{
$obj = new ScalarDummy();
Expand Down

0 comments on commit adb428d

Please sign in to comment.