Skip to content

Commit

Permalink
Merge pull request #697 from schmittjoh/xml-list
Browse files Browse the repository at this point in the history
Deserializing XMLList with Namespaces not (always) working as intended
  • Loading branch information
goetas authored Jan 19, 2017
2 parents f2200da + 17b78ad commit 30d0b79
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/JMS/Serializer/XmlDeserializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,15 @@ public function visitArray($data, array $type, Context $context)
$namespace = isset($classMetadata->xmlNamespaces[''])?$classMetadata->xmlNamespaces['']:$namespace;
}

if (0 === $data->count()){
$hasNode = false;
if (null !== $namespace) {
$prefix = uniqid('ns-');
$data->registerXPathNamespace($prefix, $namespace);
$nodes = $data->xpath("$prefix:$entryName");
} else {
$hasNode = null !== $namespace ? isset($data->children($namespace)->$entryName) : isset($data->$entryName);
$nodes = $data->xpath($entryName);
}

if (false === $hasNode) {
if (!count($nodes)) {
if (null === $this->result) {
return $this->result = array();
}
Expand All @@ -175,7 +177,6 @@ public function visitArray($data, array $type, Context $context)
$this->result = &$result;
}

$nodes = $data->children($namespace)->$entryName;
foreach ($nodes as $v) {
$result[] = $this->navigator->accept($v, $type['params'][0], $context);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/XmlSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,31 @@ public function testXmlAttributeMapWithoutArray()
$this->serializer->serialize(new Input($attributes), $this->getFormat());
}

public function testObjectWithOnlyNamespacesAndList()
{
$object = new ObjectWithNamespacesAndList();

$object->phones = array();
$object->addresses = array();

$object->phonesAlternativeB = array();
$object->addressesAlternativeB = array();

$object->phonesAlternativeC = array('777', '888');
$object->addressesAlternativeC = array('A'=>'Street 7', 'B'=>'Street 8');

$object->phonesAlternativeD = array();
$object->addressesAlternativeD = array();

$this->assertEquals(
$this->getContent('object_with_only_namespaces_and_list'),
$this->serialize($object, SerializationContext::create())
);

$deserialized = $this->deserialize($this->getContent('object_with_only_namespaces_and_list'), get_class($object));
$this->assertEquals($object, $deserialized);
}

public function testDeserializingNull()
{
$this->markTestSkipped('Not supported in XML.');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ObjectWithNamespacesAndList xmlns="http://example.com/namespace" xmlns:x="http://example.com/namespace2">
<x:phone><![CDATA[777]]></x:phone>
<x:phone><![CDATA[888]]></x:phone>
<x:address id="A"><![CDATA[Street 7]]></x:address>
<x:address id="B"><![CDATA[Street 8]]></x:address>
</ObjectWithNamespacesAndList>

0 comments on commit 30d0b79

Please sign in to comment.