Skip to content

Commit

Permalink
Merge pull request #1172 from jankramer/fix/graph-navigator-type-error
Browse files Browse the repository at this point in the history
Handle ObjectConstructor returning NULL
  • Loading branch information
goetas authored Mar 21, 2020
2 parents 4125166 + a9a4b0c commit 054c136
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 054c136

Please sign in to comment.