Skip to content

Commit

Permalink
fixes overwriting default values when explicit null is passed (closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
schmittjoh committed Dec 1, 2012
1 parent c848d9e commit f388a84
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
7 changes: 2 additions & 5 deletions Serializer/GenericDeserializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,15 @@ public function visitProperty(PropertyMetadata $metadata, $data)
{
$name = $this->namingStrategy->translateName($metadata);

if ( ! isset($data[$name])) {
if (null === $data || ! array_key_exists($name, $data)) {
return;
}

if ( ! $metadata->type) {
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);
Expand Down
16 changes: 6 additions & 10 deletions Tests/Fixtures/InitializedObjectConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
}
}
5 changes: 1 addition & 4 deletions Tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions Tests/Serializer/XmlSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit f388a84

Please sign in to comment.