Skip to content

Commit

Permalink
Improve "no describer found" error message (#1979)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhemN committed Apr 13, 2022
1 parent 1302bc7 commit f8c030d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 6 additions & 2 deletions Model/ModelRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ public function registerSchemas(): void
}

if (null === $schema) {
throw new \LogicException(sprintf('Schema of type "%s" can\'t be generated, no describer supports it.', $this->typeToString($model->getType())));
$errorMessage = sprintf('Schema of type "%s" can\'t be generated, no describer supports it.', $this->typeToString($model->getType()));
if (Type::BUILTIN_TYPE_OBJECT === $model->getType()->getBuiltinType() && !class_exists($className = $model->getType()->getClassName())) {
$errorMessage .= sprintf(' Class "\\%s" does not exist, did you forget a use statement, or typed it wrong?', $className);
}
throw new \LogicException($errorMessage);
}
}
}
Expand Down Expand Up @@ -174,7 +178,7 @@ private function getTypeShortName(Type $type): string
private function typeToString(Type $type): string
{
if (Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()) {
return $type->getClassName();
return '\\'.$type->getClassName();
} elseif ($type->isCollection()) {
if (null !== $collectionType = $this->getCollectionValueType($type)) {
return $this->typeToString($collectionType).'[]';
Expand Down
15 changes: 14 additions & 1 deletion Tests/Model/ModelRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,20 @@ public function unsupportedTypesProvider()
{
return [
[new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true), 'mixed[]'],
[new Type(Type::BUILTIN_TYPE_OBJECT, false, self::class), self::class],
[new Type(Type::BUILTIN_TYPE_OBJECT, false, self::class), '\\'.self::class],
];
}

public function testUnsupportedTypeExceptionWithNonExistentClass()
{
$className = DoesNotExist::class;
$type = new Type(Type::BUILTIN_TYPE_OBJECT, false, $className);

$this->expectException('\LogicException');
$this->expectExceptionMessage(sprintf('Schema of type "\%s" can\'t be generated, no describer supports it. Class "\Nelmio\ApiDocBundle\Tests\Model\DoesNotExist" does not exist, did you forget a use statement, or typed it wrong?', $className));

$registry = new ModelRegistry([], new OA\OpenApi([]));
$registry->register(new Model($type));
$registry->registerSchemas();
}
}

0 comments on commit f8c030d

Please sign in to comment.