Skip to content

Commit

Permalink
Handle ObjectConstructor returning NULL
Browse files Browse the repository at this point in the history
The DeserializationGraphNavigator calls the construct method on an
instance of ObjectConstructorInterface. According to the return type
hint of that method, it is allowed to return NULL. However, when this
occurs, the navigator will pass NULL to the startVisitingObject method
of the visitor. startVisitingObject expects an object to be passed in,
thus causing the serializer to crash with a type error.

We can prevent this error by calling the `visitNull` method if we detect
that the ObjectConstructor has returned NULL instead of an object.
  • Loading branch information
jankramer committed Mar 15, 2020
1 parent 25a829b commit a9a4b0c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/GraphNavigator/DeserializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ public function accept($data, ?array $type = null)

$object = $this->objectConstructor->construct($this->visitor, $metadata, $data, $type, $this->context);

if (null === $object) {
$this->context->popClassMetadata();
$this->context->decreaseDepth();

return $this->visitor->visitNull($data, $type);
}

$this->visitor->startVisitingObject($metadata, $object, $type);
foreach ($metadata->propertyMetadata as $propertyMetadata) {
if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipProperty($propertyMetadata, $this->context)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/Serializer/GraphNavigatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Common\Annotations\AnnotationReader;
use JMS\Serializer\Accessor\DefaultAccessorStrategy;
use JMS\Serializer\Construction\ObjectConstructorInterface;
use JMS\Serializer\Construction\UnserializeObjectConstructor;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\EventDispatcher\EventDispatcher;
Expand Down Expand Up @@ -156,6 +157,19 @@ public function testExposeAcceptHandlerExceptionOnSerialization()
$navigator->accept($object, ['name' => $typeName, 'params' => []]);
}

/**
* @doesNotPerformAssertions
*/
public function testNavigatorDoesNotCrashWhenObjectConstructorReturnsNull()
{
$objectConstructor = $this->getMockBuilder(ObjectConstructorInterface::class)->getMock();
$objectConstructor->method('construct')->willReturn(null);
$navigator = new DeserializationGraphNavigator($this->metadataFactory, $this->handlerRegistry, $objectConstructor, $this->accessor, $this->dispatcher);
$navigator->initialize($this->deserializationVisitor, $this->deserializationContext);

$navigator->accept(['id' => 1234], ['name' => SerializableClass::class]);
}

protected function setUp(): void
{
$this->deserializationVisitor = $this->getMockBuilder(DeserializationVisitorInterface::class)->getMock();
Expand Down

0 comments on commit a9a4b0c

Please sign in to comment.