Jelajahi Sumber

fixes overwriting default values when explicit null is passed (closes #222)

Johannes M. Schmitt 12 tahun lalu
induk
melakukan
f388a84d21

+ 2 - 5
Serializer/GenericDeserializationVisitor.php

@@ -162,7 +162,7 @@ abstract class GenericDeserializationVisitor extends AbstractVisitor
     {
         $name = $this->namingStrategy->translateName($metadata);
 
-        if ( ! isset($data[$name])) {
+        if (null === $data || ! array_key_exists($name, $data)) {
             return;
         }
 
@@ -170,10 +170,7 @@ abstract class GenericDeserializationVisitor extends AbstractVisitor
             throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->reflection->class, $metadata->name));
         }
 
-        $v = $this->navigator->accept($data[$name], $metadata->type, $this);
-        if (null === $v) {
-            return;
-        }
+        $v = $data[$name] !== null ? $this->navigator->accept($data[$name], $metadata->type, $this) : null;
 
         if (null === $metadata->setter) {
             $metadata->reflection->setValue($this->currentObject, $v);

+ 6 - 10
Tests/Fixtures/InitializedObjectConstructor.php

@@ -26,20 +26,16 @@ use JMS\SerializerBundle\Serializer\Construction\ObjectConstructorInterface;
 use JMS\SerializerBundle\Serializer\VisitorInterface;
 
 use JMS\SerializerBundle\Tests\Fixtures\Author;
+use JMS\SerializerBundle\Serializer\Construction\UnserializeObjectConstructor;
 
-class InitializedObjectConstructor implements ObjectConstructorInterface
+class InitializedObjectConstructor extends UnserializeObjectConstructor
 {
     public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type)
     {
-        $post = new \StdClass;
-        $post->title = 'This is a nice title.';
-        $post->author = new Author('Foo Bar');
-        $post->createdAt = new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC'));
-        $post->comments = new ArrayCollection();
-        $post->published = false;
+        if ($type['name'] !== 'JMS\SerializerBundle\Tests\Fixtures\BlogPost') {
+            return parent::construct($visitor, $metadata, $data, $type);
+        }
 
-        $post->comments->add(new \StdClass);
-
-        return $post;
+        return new BlogPost('This is a nice title.', new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')));
     }
 }

+ 1 - 4
Tests/Serializer/BaseSerializationTest.php

@@ -286,10 +286,6 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
 
     public function testDeserializingNull()
     {
-        if (get_class($this) === 'JMS\SerializerBundle\Tests\Serializer\XmlSerializationTest') {
-            $this->markTestSkipped('Deserializing null not working in XML.');
-        }
-
         $objectConstructor = new InitializedObjectConstructor();
         $this->serializer = new Serializer($this->factory, $this->handlerRegistry, $objectConstructor, $this->serializationVisitors, $this->deserializationVisitors, $this->dispatcher);
         $this->serializer->setSerializeNull(true);
@@ -302,6 +298,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
 
         if ($this->hasDeserializer()) {
             $deserialized = $this->deserialize($this->getContent('blog_post_unauthored'), get_class($post));
+
             $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(false, 'published', $deserialized);

+ 5 - 0
Tests/Serializer/XmlSerializationTest.php

@@ -146,6 +146,11 @@ class XmlSerializationTest extends BaseSerializationTest
         $this->serializer->serialize(new Input($attributes), $this->getFormat());
     }
 
+    public function testDeserializingNull()
+    {
+        $this->markTestSkipped('Not supported in XML.');
+    }
+
     /**
      * @param string $key
      */