Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixed (un)marshalling of elements following a namespaced element #24

Closed
wants to merge 1 commit into from

1 participant

@jakzal

Elements following a namespaced element are not unmarshalled. In the following example bas element would be null:

<qux xmlns="http://www.foobar.com/schema"
         xmlns:bat="http://www.foobaz.com/schema">
    <bat:baz>Baz</bat:baz>
    <bas>Bas</bas>
</qux>

Also, element prefix is not added during the marshalling. Classes:

namespace Doctrine\Tests\OXM\Entities\NamespaceEntities;

class Baz
{
    /**
     * @XmlValue(type="string")
     */
    public $title;
}

/**
 * @XmlRootEntity
 * @XmlNamespaces({
 *   @XmlNamespace(url="http://www.foobar.com/schema"),
 *   @XmlNamespace(url="http://www.foobaz.com/schema", prefix="bat")
 * })
 */
class Qux
{
    /**
     * @XmlElement(type="Doctrine\Tests\OXM\Entities\NamespaceEntities\Baz", name="baz", prefix="bat")
     */
    public $baz;

    /**
     * @XmlText(type="string", name="bas")
     */
    public $bas;
}

Would be marshalled to (baz would lack the namespace):

<qux xmlns="http://www.foobar.com/schema" 
        xmlns:bat="http://www.foobaz.com/schema">
    <baz>Baz</baz>
    <bas>Bas</bas>
</qux>

Note that doUnmarshal() method doesn't take prefix into account. I'm not sure if it's correct behavior. However, my fix is consistent with it.

@jakzal jakzal Fixed unmarshalling of elements which are present after a namespaced …
…element. Fixed marshalling of namespaced elements.
46d8e8b
@jakzal jakzal closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 1, 2012
  1. @jakzal

    Fixed unmarshalling of elements which are present after a namespaced …

    jakzal authored
    …element. Fixed marshalling of namespaced elements.
This page is out of date. Refresh to see the latest.
View
8 lib/Doctrine/OXM/Marshaller/XmlMarshaller.php
@@ -263,7 +263,7 @@ private function doUnmarshal(XMLReader $cursor)
$collectionElements = array();
while ($cursor->read()) {
- if ($cursor->nodeType === XMLReader::END_ELEMENT && $cursor->name === $elementName) {
+ if ($cursor->nodeType === XMLReader::END_ELEMENT && $cursor->localName === $elementName) {
// we're at the original element closing node, bug out
break;
}
@@ -399,7 +399,7 @@ public function marshalToStream($mappedObject, $streamUri)
* @param WriterHelper $writer
* @return void
*/
- private function doMarshal($mappedObject, WriterHelper $writer)
+ private function doMarshal($mappedObject, WriterHelper $writer, $prefix = null)
{
$className = get_class($mappedObject);
$classMetadata = $this->classMetadataFactory->getMetadataFor($className);
@@ -419,7 +419,7 @@ private function doMarshal($mappedObject, WriterHelper $writer)
$this->visited[spl_object_hash($mappedObject)] = true;
- $writer->startElement($classMetadata->getXmlName());
+ $writer->startElement($classMetadata->getXmlName(), $prefix);
$namespaces = $classMetadata->getXmlNamespaces();
if (!empty($namespaces)) {
@@ -592,7 +592,7 @@ private function writeElement(WriterHelper $writer, ClassMetadata $classMetadata
$writer->endElement();
}
} else {
- $this->doMarshal($fieldValue, $writer);
+ $this->doMarshal($fieldValue, $writer, $prefix);
}
}
}
View
32 tests/Doctrine/Tests/OXM/Entities/NamespaceEntities/Baz.php
@@ -0,0 +1,32 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\Tests\OXM\Entities\NamespaceEntities;
+
+/**
+ * @XmlEntity
+ */
+class Baz
+{
+ /**
+ * @XmlValue(type="string")
+ */
+ public $title;
+}
+
View
41 tests/Doctrine/Tests/OXM/Entities/NamespaceEntities/Qux.php
@@ -0,0 +1,41 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\Tests\OXM\Entities\NamespaceEntities;
+
+/**
+ * @XmlRootEntity
+ * @XmlNamespaces({
+ * @XmlNamespace(url="http://www.foobar.com/schema"),
+ * @XmlNamespace(url="http://www.foobaz.com/schema", prefix="bat")
+ * })
+ */
+class Qux
+{
+ /**
+ * @XmlElement(type="Doctrine\Tests\OXM\Entities\NamespaceEntities\Baz", name="baz", prefix="bat")
+ */
+ public $baz;
+
+ /**
+ * @XmlText(type="string", name="bas")
+ */
+ public $bas;
+}
+
View
43 tests/Doctrine/Tests/OXM/Marshaller/NamespacesTest.php
@@ -20,7 +20,9 @@
namespace Doctrine\Tests\OXM\Marshaller;
use \Doctrine\Tests\OxmTestCase,
- \Doctrine\Tests\OXM\Entities\NamespaceEntities\Foo;
+ \Doctrine\Tests\OXM\Entities\NamespaceEntities\Foo,
+ \Doctrine\Tests\OXM\Entities\NamespaceEntities\Qux,
+ \Doctrine\Tests\OXM\Entities\NamespaceEntities\Baz;
class NamespacesTest extends OxmTestCase
{
@@ -52,4 +54,43 @@ public function itShouldWriteNamespacesCorrectly()
$this->assertEquals(1, $otherRequest->id);
$this->assertEquals("bar", $otherRequest->bo);
}
+
+ /**
+ * @test
+ */
+ public function itShouldMarshalNamespacedNode()
+ {
+ $qux = new Qux();
+ $qux->baz = new Baz();
+ $qux->baz->title = "Baz";
+ $qux->bas = "Bas";
+
+ $xml = $this->marshaller->marshalToString($qux);
+
+ $expectedXml = '<?xml version="1.0" encoding="UTF-8"?>
+ <qux xmlns="http://www.foobar.com/schema" xmlns:bat="http://www.foobaz.com/schema">
+ <bas>Bas</bas>
+ <bat:baz>Baz</bat:baz>
+ </qux>';
+
+ $this->assertXmlStringEqualsXmlString($expectedXml, $xml);
+ }
+
+ /**
+ * @test
+ */
+ public function itShouldUnmarshalNonNamespacedNodePresentAfterNamespacedNode()
+ {
+ $xml = '<?xml version="1.0" encoding="UTF-8"?>
+ <qux xmlns="http://www.foobar.com/schema" xmlns:bat="http://www.foobaz.com/schema">
+ <bat:baz>Baz</bat:baz>
+ <bas>Bas</bas>
+ </qux>';
+
+ $qux = $this->marshaller->unmarshalFromString($xml);
+
+ $this->assertInstanceOf('Doctrine\Tests\OXM\Entities\NamespaceEntities\Baz', $qux->baz);
+ $this->assertEquals('Baz', $qux->baz->title);
+ $this->assertEquals('Bas', $qux->bas);
+ }
}
Something went wrong with that request. Please try again.