Skip to content

Commit

Permalink
do not throw exception when visiting null in custom handler
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Apr 23, 2019
1 parent 90e1063 commit ffc0fee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/GraphNavigator/SerializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,11 @@ public function accept($data, ?array $type = null)
// before loading metadata because the type name might not be a class, but
// could also simply be an artifical type.
if (null !== $handler = $this->handlerRegistry->getHandler(GraphNavigatorInterface::DIRECTION_SERIALIZATION, $type['name'], $this->format)) {
$rs = \call_user_func($handler, $this->visitor, $data, $type, $this->context);
$this->context->stopVisiting($data);
try {
$rs = \call_user_func($handler, $this->visitor, $data, $type, $this->context);
} finally {
$this->context->stopVisiting($data);
}

return $rs;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,22 @@ public function testCustomHandler()
self::assertEquals('customly_unserialized_value', $object->someProperty);
}

/**
* @doesNotPerformAssertions
*/
public function testCustomHandlerVisitingNull()
{
$handler = static function ($visitor, $attachment, array $type, Context $context) {
return $context->getNavigator()->accept(null);
};

$this->handlerRegistry->registerHandler(GraphNavigatorInterface::DIRECTION_SERIALIZATION, Author::class, $this->getFormat(), $handler);

$author = new Author('me');
$comment = new Comment($author, 'too');
$this->serializer->serialize($comment, $this->getFormat());
}

public function testInput()
{
self::assertEquals($this->getContent('input'), $this->serializer->serialize(new Input(), $this->getFormat()));
Expand Down

0 comments on commit ffc0fee

Please sign in to comment.