Browse Source

Modifies the namespace prefix autogeneration so it can be predicted

Antonio J. García Lagar 12 years ago
parent
commit
daacf8b5c7

+ 6 - 6
src/JMS/Serializer/XmlSerializationVisitor.php

@@ -209,11 +209,11 @@ class XmlSerializationVisitor extends AbstractVisitor
             $attributeName = $this->namingStrategy->translateName($metadata);
             if ('' !== $namespace = (string) $metadata->xmlNamespace) {
                 if (!$prefix = $this->currentNode->lookupPrefix($namespace)) {
-                    $prefix = uniqid('ns-');
+                    $prefix = 'ns-'.  substr(sha1($namespace), 0, 8);
                 }
-                $this->currentNode->setAttributeNS($namespace, $prefix.':'.$attributeName, $node->nodeValue);    
+                $this->currentNode->setAttributeNS($namespace, $prefix.':'.$attributeName, $node->nodeValue);
             } else {
-                $this->currentNode->setAttribute($attributeName, $node->nodeValue);    
+                $this->currentNode->setAttribute($attributeName, $node->nodeValue);
             }
 
             return;
@@ -256,9 +256,9 @@ class XmlSerializationVisitor extends AbstractVisitor
 
                 if ('' !== $namespace = (string) $metadata->xmlNamespace) {
                     if (!$prefix = $this->currentNode->lookupPrefix($namespace)) {
-                        $prefix = uniqid('ns-');
+                        $prefix = 'ns-'.  substr(sha1($namespace), 0, 8);
                     }
-                    $this->currentNode->setAttributeNS($namespace, $prefix.':'.$key, $node->nodeValue);    
+                    $this->currentNode->setAttributeNS($namespace, $prefix.':'.$key, $node->nodeValue);
                 } else {
                     $this->currentNode->setAttribute($key, $node->nodeValue);
                 }
@@ -271,7 +271,7 @@ class XmlSerializationVisitor extends AbstractVisitor
             $elementName = $this->namingStrategy->translateName($metadata);
             if ('' !== $namespace = (string) $metadata->xmlNamespace) {
                 if (!$prefix = $this->document->lookupPrefix($namespace)) {
-                    $prefix = uniqid('ns-');
+                    $prefix = 'ns-'.  substr(sha1($namespace), 0, 8);
                 }
                 $element = $this->document->createElementNS($namespace, $prefix.':'.$elementName);
             } else {

+ 16 - 9
tests/JMS/Serializer/Tests/Serializer/XmlSerializationTest.php

@@ -180,23 +180,26 @@ class XmlSerializationTest extends BaseSerializationTest
     {
         $this->markTestSkipped('Not supported in XML.');
     }
-    
+
     public function testObjectWithXmlNamespaces()
     {
         $object = new ObjectWithXmlNamespaces('This is a nice title.', 'Foo Bar', new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), 'en');
-        
+
+        $serialized = $this->serialize($object);
+        $this->assertEquals($this->getContent('object_with_xml_namespaces'), $this->serialize($object));
+
         $xml = simplexml_load_string($this->serialize($object));
         $xml->registerXPathNamespace('ns1', "http://purl.org/dc/elements/1.1/");
         $xml->registerXPathNamespace('ns2', "http://schemas.google.com/g/2005");
         $xml->registerXPathNamespace('ns3', "http://www.w3.org/2005/Atom");
-        
+
         $this->assertEquals('2011-07-30T00:00:00+0000', $this->xpathFirstToString($xml, './@created_at'));
         $this->assertEquals('1edf9bf60a32d89afbb85b2be849e3ceed5f5b10', $this->xpathFirstToString($xml, './@ns2:etag'));
         $this->assertEquals('en', $this->xpathFirstToString($xml, './@ns1:language'));
         $this->assertEquals('This is a nice title.', $this->xpathFirstToString($xml, './ns1:title'));
         $this->assertEquals('Foo Bar', $this->xpathFirstToString($xml, './ns3:author'));
 
-        $deserialized = $this->deserialize($this->getContent('object_with_xml_namespaces'), get_class($object));
+        $deserialized = $this->deserialize($this->getContent('object_with_xml_namespacesalias'), get_class($object));
         $this->assertEquals('2011-07-30T00:00:00+0000', $this->getField($deserialized, 'createdAt')->format(\DateTime::ISO8601));
         $this->assertAttributeEquals('This is a nice title.', 'title', $deserialized);
         $this->assertAttributeSame('1edf9bf60a32d89afbb85b2be849e3ceed5f5b10', 'etag', $deserialized);
@@ -204,23 +207,27 @@ class XmlSerializationTest extends BaseSerializationTest
         $this->assertAttributeEquals('Foo Bar', 'author', $deserialized);
 
     }
-    
+
     public function testXmlNamespacesInheritance()
     {
         $object = new SimpleClassObject();
         $object->foo = 'foo';
         $object->bar = 'bar';
-        
+        $object->moo = 'moo';
+
         $this->assertEquals($this->getContent('simple_class_object'), $this->serialize($object));
-        
+
         $childObject = new SimpleSubClassObject();
         $childObject->foo = 'foo';
         $childObject->bar = 'bar';
         $childObject->moo = 'moo';
-        
+        $childObject->baz = 'baz';
+        $childObject->qux = 'qux';
+
+
         $this->assertEquals($this->getContent('simple_subclass_object'), $this->serialize($childObject));
     }
-    
+
     private function xpathFirstToString(\SimpleXMLElement $xml, $xpath)
     {
         $nodes = $xml->xpath($xpath);

+ 3 - 3
tests/JMS/Serializer/Tests/Serializer/xml/object_with_xml_namespaces.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<test-object xmlns="http://example.com/namespace" xmlns:name1="http://schemas.google.com/g/2005" xmlns:name2="http://www.w3.org/2005/Atom" xmlns:name3="http://purl.org/dc/elements/1.1/" created_at="2011-07-30T00:00:00+0000" name1:etag="1edf9bf60a32d89afbb85b2be849e3ceed5f5b10" name3:language="en">
-  <name3:title><![CDATA[This is a nice title.]]></name3:title>
-  <name2:author><![CDATA[Foo Bar]]></name2:author>
+<test-object xmlns="http://example.com/namespace" xmlns:gd="http://schemas.google.com/g/2005" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:ns-fde543a0="http://purl.org/dc/elements/1.1/" created_at="2011-07-30T00:00:00+0000" gd:etag="1edf9bf60a32d89afbb85b2be849e3ceed5f5b10" ns-fde543a0:language="en">
+  <ns-fde543a0:title xmlns:ns-fde543a0="http://purl.org/dc/elements/1.1/"><![CDATA[This is a nice title.]]></ns-fde543a0:title>
+  <atom:author><![CDATA[Foo Bar]]></atom:author>
 </test-object>

+ 5 - 0
tests/JMS/Serializer/Tests/Serializer/xml/object_with_xml_namespacesalias.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<test-object xmlns="http://example.com/namespace" xmlns:name1="http://schemas.google.com/g/2005" xmlns:name2="http://www.w3.org/2005/Atom" xmlns:name3="http://purl.org/dc/elements/1.1/" created_at="2011-07-30T00:00:00+0000" name1:etag="1edf9bf60a32d89afbb85b2be849e3ceed5f5b10" name3:language="en">
+  <name3:title><![CDATA[This is a nice title.]]></name3:title>
+  <name2:author><![CDATA[Foo Bar]]></name2:author>
+</test-object>