diff --git a/src/SerializationContext.php b/src/SerializationContext.php index 34aacd2f8..52a3295b0 100644 --- a/src/SerializationContext.php +++ b/src/SerializationContext.php @@ -153,23 +153,4 @@ public function getInitialType(): ?string ? $this->initialType : $this->hasAttribute('initial_type') ? $this->getAttribute('initial_type') : null; } - - /** - * Set if NULLs should be serialized (TRUE) ot not (FALSE) - */ - public function setSerializeNull(bool $bool): self - { - $this->serializeNull = $bool; - - return $this; - } - - /** - * Returns TRUE when NULLs should be serialized - * Returns FALSE when NULLs should not be serialized - */ - public function shouldSerializeNull(): bool - { - return $this->serializeNull; - } } diff --git a/tests/Serializer/BaseSerializationTest.php b/tests/Serializer/BaseSerializationTest.php index f8053450e..4a24dbe3d 100644 --- a/tests/Serializer/BaseSerializationTest.php +++ b/tests/Serializer/BaseSerializationTest.php @@ -746,8 +746,7 @@ public function testDeserializingNull() self::assertEquals($this->getContent('blog_post_unauthored'), $this->serialize($post, SerializationContext::create()->setSerializeNull(true))); if ($this->hasDeserializer()) { - $ctx = DeserializationContext::create() - $deserialized = $this->deserialize($this->getContent('blog_post_unauthored'), get_class($post), $ctx); + $deserialized = $this->deserialize($this->getContent('blog_post_unauthored'), get_class($post)); self::assertEquals('2011-07-30T00:00:00+00:00', $this->getField($deserialized, 'createdAt')->format(\DateTime::ATOM)); self::assertAttributeEquals('This is a nice title.', 'title', $deserialized); @@ -758,6 +757,37 @@ public function testDeserializingNull() } } + public function testDeserializingNullAllowed() + { + $objectConstructor = new InitializedBlogPostConstructor(); + + $builder = SerializerBuilder::create(); + $builder->setObjectConstructor($objectConstructor); + $this->serializer = $builder->build(); + + $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), new Publisher('Bar Foo')); + + $this->setField($post, 'author', null); + $this->setField($post, 'publisher', null); + + $ctx = SerializationContext::create()->setSerializeNull(true); + self::assertEquals($this->getContent('blog_post_unauthored'), $this->serialize($post, $ctx)); + + if ($this->hasDeserializer()) { + $ctx = DeserializationContext::create(); + $ctx->setDeserializeNull(true); + + $deserialized = $this->deserialize($this->getContent('blog_post_unauthored'), get_class($post), $ctx); + + self::assertEquals('2011-07-30T00:00:00+00:00', $this->getField($deserialized, 'createdAt')->format(\DateTime::ATOM)); + self::assertAttributeEquals('This is a nice title.', 'title', $deserialized); + self::assertAttributeSame(false, 'published', $deserialized); + self::assertAttributeSame(false, 'reviewed', $deserialized); + self::assertAttributeEquals(new ArrayCollection(), 'comments', $deserialized); + self::assertEquals(null, $this->getField($deserialized, 'author')); + } + } + public function testExpressionAuthor() { $evaluator = new ExpressionEvaluator(new ExpressionLanguage()); diff --git a/tests/Serializer/XmlSerializationTest.php b/tests/Serializer/XmlSerializationTest.php index 855b78197..b33254293 100644 --- a/tests/Serializer/XmlSerializationTest.php +++ b/tests/Serializer/XmlSerializationTest.php @@ -368,6 +368,11 @@ public function testDeserializingNull() $this->markTestSkipped('Not supported in XML.'); } + public function testDeserializingNullAllowed() + { + $this->markTestSkipped('Not supported in XML.'); + } + public function testObjectWithXmlNamespaces() { $object = new ObjectWithXmlNamespaces('This is a nice title.', 'Foo Bar', new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), 'en');