Browse Source

Added xmlns:xsi namespace and fixed tests

Dmitry Chirkin 12 years ago
parent
commit
fff18ede73

+ 20 - 4
src/JMS/Serializer/XmlSerializationVisitor.php

@@ -2,13 +2,13 @@
 
 /*
  * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *     http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -40,6 +40,7 @@ class XmlSerializationVisitor extends AbstractVisitor
     private $currentNode;
     private $currentMetadata;
     private $hasValue;
+    private $nullWasVisited;
 
     public function setDefaultRootName($name)
     {
@@ -77,12 +78,14 @@ class XmlSerializationVisitor extends AbstractVisitor
             $node->value = 'true';
             $this->currentNode->appendChild($node);
 
+            $this->attachNullNamespace();
+
             return;
         }
 
         $node = $this->document->createAttribute('xsi:nil');
         $node->value = 'true';
-
+        $this->attachNullNamespace();
         return $node;
     }
 
@@ -319,4 +322,17 @@ class XmlSerializationVisitor extends AbstractVisitor
     {
         return $name && false === strpos($name, ' ') && preg_match('#^[\pL_][\pL0-9._-]*$#ui', $name);
     }
+
+    private function attachNullNamespace()
+    {
+        if (!$this->nullWasVisited) {
+            $this->document->documentElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
+            $this->nullWasVisited = true;
+        }
+    }
+
+    public function prepare($data) {
+        $this->nullWasVisited = false;
+        return $data;
+    }
 }

+ 3 - 3
tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php

@@ -2,13 +2,13 @@
 
 /*
  * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *     http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

+ 3 - 3
tests/JMS/Serializer/Tests/Serializer/XmlSerializationTest.php

@@ -2,13 +2,13 @@
 
 /*
  * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *     http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

+ 1 - 1
tests/JMS/Serializer/Tests/Serializer/xml/null.xml

@@ -1,2 +1,2 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<result xsi:nil="true"/>
+<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>

+ 1 - 1
tests/JMS/Serializer/Tests/Serializer/xml/nullable.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<result>
+<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <entry><![CDATA[bar]]></entry>
   <entry xsi:nil="true"/>
   <entry xsi:nil="true"/>

+ 1 - 1
tests/JMS/Serializer/Tests/Serializer/xml/object_when_null_and_serialized.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<result>
+<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <author xsi:nil="true"/>
   <text><![CDATA[foo]]></text>
 </result>

+ 1 - 1
tests/JMS/Serializer/Tests/Serializer/xml/simple_object_nullable.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<result>
+<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <foo><![CDATA[foo]]></foo>
   <moo><![CDATA[bar]]></moo>
   <camel_case><![CDATA[boo]]></camel_case>