diff --git a/Serializer/GenericDeserializationVisitor.php b/Serializer/GenericDeserializationVisitor.php index 24816d42..c1cfbb63 100644 --- a/Serializer/GenericDeserializationVisitor.php +++ b/Serializer/GenericDeserializationVisitor.php @@ -162,7 +162,7 @@ public function visitProperty(PropertyMetadata $metadata, $data) { $name = $this->namingStrategy->translateName($metadata); - if ( ! isset($data[$name])) { + if (null === $data || ! array_key_exists($name, $data)) { return; } @@ -170,10 +170,7 @@ public function visitProperty(PropertyMetadata $metadata, $data) 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); diff --git a/Tests/Fixtures/InitializedObjectConstructor.php b/Tests/Fixtures/InitializedObjectConstructor.php index 3f4d1f56..0252411a 100644 --- a/Tests/Fixtures/InitializedObjectConstructor.php +++ b/Tests/Fixtures/InitializedObjectConstructor.php @@ -26,20 +26,16 @@ 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'))); } } diff --git a/Tests/Serializer/BaseSerializationTest.php b/Tests/Serializer/BaseSerializationTest.php index 4add2d64..f7b36b1c 100644 --- a/Tests/Serializer/BaseSerializationTest.php +++ b/Tests/Serializer/BaseSerializationTest.php @@ -286,10 +286,6 @@ public function testBlogPost() 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 @@ public function testDeserializingNull() 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); diff --git a/Tests/Serializer/XmlSerializationTest.php b/Tests/Serializer/XmlSerializationTest.php index f86ddfa6..a93a4158 100644 --- a/Tests/Serializer/XmlSerializationTest.php +++ b/Tests/Serializer/XmlSerializationTest.php @@ -146,6 +146,11 @@ public function testXmlAttributeMapWithoutArray() $this->serializer->serialize(new Input($attributes), $this->getFormat()); } + public function testDeserializingNull() + { + $this->markTestSkipped('Not supported in XML.'); + } + /** * @param string $key */