Skip to content

Commit f39d8b4

Browse files
authored
Merge pull request #675 from schmittjoh/xml-array
Fixed xml arrays with namespaced entry triggers error
2 parents c0c1af0 + 6d53eca commit f39d8b4

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/JMS/Serializer/XmlDeserializationVisitor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ public function visitArray($data, array $type, Context $context)
150150
$namespace = isset($classMetadata->xmlNamespaces[''])?$classMetadata->xmlNamespaces['']:$namespace;
151151
}
152152

153-
$hasNode = null !== $namespace ? isset($data->children($namespace)->$entryName) : isset($data->$entryName);
153+
if (0 === $data->count()){
154+
$hasNode = false;
155+
} else {
156+
$hasNode = null !== $namespace ? isset($data->children($namespace)->$entryName) : isset($data->$entryName);
157+
}
158+
154159
if (false === $hasNode) {
155160
if (null === $this->result) {
156161
return $this->result = array();

tests/JMS/Serializer/Tests/Fixtures/ObjectWithAbsentXmlListNode.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,10 @@ class ObjectWithAbsentXmlListNode
3838
* @Serializer\Type("array<string>")
3939
*/
4040
public $skipDefault;
41+
42+
/**
43+
* @Serializer\XmlList(inline=false, namespace="http://www.example.com")
44+
* @Serializer\Type("array<string>")
45+
*/
46+
public $absentAndNs;
4147
}

tests/JMS/Serializer/Tests/Serializer/XmlSerializationTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,22 @@ public function testVirtualXmlMap()
184184
$this->serialize(new ObjectWithVirtualXmlProperties(), SerializationContext::create()->setGroups(array('map')))
185185
);
186186
}
187+
188+
public function testUnserializeMissingArray()
189+
{
190+
$xml = '<result></result>';
191+
$object = $this->serializer->deserialize($xml, 'JMS\Serializer\Tests\Fixtures\ObjectWithAbsentXmlListNode', 'xml');
192+
$this->assertEquals($object->absentAndNs, array());
193+
194+
$xml = '<result xmlns:x="http://www.example.com">
195+
<absent_and_ns>
196+
<x:entry>foo</x:entry>
197+
</absent_and_ns>
198+
</result>';
199+
$object = $this->serializer->deserialize($xml, 'JMS\Serializer\Tests\Fixtures\ObjectWithAbsentXmlListNode', 'xml');
200+
$this->assertEquals($object->absentAndNs, array("foo"));
201+
}
202+
187203
public function testObjectWithNamespacesAndList()
188204
{
189205
$object = new ObjectWithNamespacesAndList();

0 commit comments

Comments
 (0)