diff --git a/Model/ModelRegistry.php b/Model/ModelRegistry.php index db346fb87..7631d0bd9 100644 --- a/Model/ModelRegistry.php +++ b/Model/ModelRegistry.php @@ -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); } } } @@ -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).'[]'; diff --git a/Tests/Model/ModelRegistryTest.php b/Tests/Model/ModelRegistryTest.php index ef7fd853e..fdbbc3600 100644 --- a/Tests/Model/ModelRegistryTest.php +++ b/Tests/Model/ModelRegistryTest.php @@ -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(); + } }