Skip to content

Commit

Permalink
During XML deserialization, do not visit property if that node is abs…
Browse files Browse the repository at this point in the history
…ent.

fixes Deserializing XMLList with Namespaces not (always) working as intended schmittjoh#695
  • Loading branch information
Björn Bösel committed Jan 9, 2017
1 parent 8a29560 commit eeb3e45
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/JMS/Serializer/XmlDeserializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,7 @@ public function visitArray($data, array $type, Context $context)
$namespace = isset($classMetadata->xmlNamespaces[''])?$classMetadata->xmlNamespaces['']:$namespace;
}

if (0 === $data->count()){
$hasNode = false;
} else {
$hasNode = null !== $namespace ? isset($data->children($namespace)->$entryName) : isset($data->$entryName);
}
$hasNode = null !== $namespace ? isset($data->children($namespace)->$entryName) : isset($data->$entryName);

if (false === $hasNode) {
if (null === $this->result) {
Expand Down Expand Up @@ -249,6 +245,10 @@ public function visitProperty(PropertyMetadata $metadata, $data, Context $contex
if ($metadata->xmlCollection) {
$enclosingElem = $data;
if (!$metadata->xmlCollectionInline) {
if (!isset($data->children($metadata->xmlNamespace)->$name)) {
$metadata->setValue($this->currentObject, array());
return;
}
$enclosingElem = $data->children($metadata->xmlNamespace)->$name;
}

Expand Down
27 changes: 27 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/XmlSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,33 @@ public function testObjectWithNamespacesAndList()
);
}


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())
);
$this->assertEquals(
$object,
$this->deserialize($this->getContent('object_with_only_namespaces_and_list'), get_class($object))
);
}

public function testArrayKeyValues()
{
$this->assertEquals($this->getContent('array_key_values'), $this->serializer->serialize(new ObjectWithXmlKeyValuePairs(), '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 eeb3e45

Please sign in to comment.