Improve "no describer found" error message (#1979)

This commit is contained in:
Guilhem Niot 2022-04-13 19:54:31 +02:00 committed by GitHub
parent 1302bc7568
commit f8c030d096
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -100,7 +100,11 @@ final class ModelRegistry
}
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);
}
}
}
@ -174,7 +178,7 @@ final class ModelRegistry
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).'[]';

View File

@ -234,7 +234,20 @@ class ModelRegistryTest extends TestCase
{
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();
}
}