Ver Fonte

fixes null-handling behavior when serializing null object (closes #233)

Johannes M. Schmitt há 12 anos atrás
pai
commit
7de7897e9f

+ 5 - 0
Serializer/GraphNavigator.php

@@ -114,6 +114,11 @@ final class GraphNavigator
 
             $type = array('name' => $typeName, 'params' => array());
         }
+        // If the data is null, we have to force the type to null regardless of the input in order to
+        // guarantee correct handling of null values, and not have any internal auto-casting behavior.
+        else if (self::DIRECTION_SERIALIZATION === $this->direction && null === $data) {
+            $type = array('name' => 'NULL', 'params' => array());
+        }
 
         switch ($type['name']) {
             case 'NULL':

+ 1 - 1
Tests/Fixtures/Comment.php

@@ -32,7 +32,7 @@ class Comment
      */
     private $text;
 
-    public function __construct(Author $author, $text)
+    public function __construct(Author $author = null, $text)
     {
         $this->author = $author;
         $this->text = $text;

+ 14 - 0
Tests/Serializer/BaseSerializationTest.php

@@ -80,6 +80,8 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
 {
     protected $factory;
     protected $dispatcher;
+
+    /** @var Serializer */
     protected $serializer;
     protected $handlerRegistry;
     protected $serializationVisitors;
@@ -543,6 +545,18 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($this->getContent('hash_empty'), $this->serializer->serialize(new ObjectWithEmptyHash(), $this->getFormat()));
     }
 
+    /**
+     * @group null
+     */
+    public function testSerializeObjectWhenNull()
+    {
+        $this->serializer->setSerializeNull(false);
+        $this->assertEquals($this->getContent('object_when_null'), $this->serialize(new Comment(null, 'foo')));
+
+        $this->serializer->setSerializeNull(true);
+        $this->assertEquals($this->getContent('object_when_null_and_serialized'), $this->serialize(new Comment(null, 'foo')));
+    }
+
     abstract protected function getContent($key);
     abstract protected function getFormat();
 

+ 2 - 0
Tests/Serializer/JsonSerializationTest.php

@@ -79,6 +79,8 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['simple_object_nullable'] = '{"foo":"foo","moo":"bar","camel_case":"boo","null_property":null}';
             $outputs['input'] = '{"attributes":{"type":"text","name":"firstname","value":"Adrien"}}';
             $outputs['hash_empty'] = '{"hash":{}}';
+            $outputs['object_when_null'] = '{"text":"foo"}';
+            $outputs['object_when_null_and_serialized'] = '{"author":null,"text":"foo"}';
         }
 
         if (!isset($outputs[$key])) {

+ 4 - 0
Tests/Serializer/xml/object_when_null.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result>
+  <text><![CDATA[foo]]></text>
+</result>

+ 5 - 0
Tests/Serializer/xml/object_when_null_and_serialized.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result>
+  <author xsi:nil="true"/>
+  <text><![CDATA[foo]]></text>
+</result>

+ 1 - 0
Tests/Serializer/yml/object_when_null.yml

@@ -0,0 +1 @@
+text: foo

+ 2 - 0
Tests/Serializer/yml/object_when_null_and_serialized.yml

@@ -0,0 +1,2 @@
+author: null
+text: foo