Skip to content

Commit

Permalink
apidoc 支持新版依赖,支持数组参数
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Sep 22, 2023
1 parent 7f1f53a commit f9b3c77
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/Components/apidoc/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"license": "MulanPSL-2.0",
"description": "imi 框架 HTTP API 文档生成器,支持 Swagger!",
"require": {
"zircote/swagger-php": "^3.0"
"zircote/swagger-php": "^3.0|~4.7.0",
"doctrine/annotations": "^1.7"
},
"require-dev": {
"yurunsoft/ide-helper": "~1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function login(string $username, int $password)
*
* @return void
*/
public function multiMethod1(int $id)
public function multiMethod1(int $id, int $type, array $tags)
{
}

Expand All @@ -57,9 +57,11 @@ public function multiMethod1(int $id)
*
* @Route(method={"PUT", "POST"})
*
* @param int[] $tags 标签
*
* @return void
*/
public function multiMethod2(int $id)
public function multiMethod2(int $id, int $type, array $tags)
{
}

Expand Down
55 changes: 49 additions & 6 deletions src/Components/apidoc/src/Tool/DocTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Imi\Util\DocBlock;
use OpenApi\Analysis;
use OpenApi\Annotations\Info;
use OpenApi\Annotations\Items;
use OpenApi\Annotations\MediaType;
use OpenApi\Annotations\Operation as AnnotationsOperation;
use OpenApi\Annotations\Parameter;
Expand All @@ -28,6 +29,7 @@
use OpenApi\Annotations\Response;
use OpenApi\Annotations\Schema;
use OpenApi\Context;
use OpenApi\Generator;

/**
* @Command("doc")
Expand Down Expand Up @@ -83,13 +85,16 @@ public function api(string $to, ?string $namespace): void
return;
}
// 生成
$processors = Analysis::processors();
$generator = new Generator(App::getBean('Logger')->getLogger());

$processors = $generator->getProcessors();
array_unshift($processors, function (Analysis $analysis) use ($controllerClasses) {
$this->parseRoute($analysis, $controllerClasses);
});
$openapi = \OpenApi\scan($directory, [
'processors' => $processors,
]);

$openapi = $generator
->setProcessors($processors)
->generate([$directory]);
$openapi->saveAs($to);
}

Expand All @@ -105,6 +110,10 @@ private function parseRoute(Analysis $analysis, array $controllerClasses)
$info = null;
foreach ($analysis->annotations as $annotation)
{
if (!isset($annotation->_context))
{
continue;
}
/** @var \OpenApi\Context $context */
$context = $annotation->_context;
/** @var \OpenApi\Annotations\AbstractAnnotation $annotation */
Expand Down Expand Up @@ -196,12 +205,13 @@ private function parseRoute(Analysis $analysis, array $controllerClasses)
foreach ($refMethod->getParameters() as $param)
{
$docParam = $this->getDocParam($docParams, $param->getName());

$requestParameters[] = new Parameter([
'parameter' => $controllerClass . '::' . $method . '@request.' . $param->getName(),
'name' => $param->getName(),
'in' => 'query',
'required' => !$param->isOptional(),
'description' => $docParam ? (string) $docParam->getDescription() : \OpenApi\Annotations\UNDEFINED,
'description' => $docParam ? (string) $docParam->getDescription() : Generator::UNDEFINED,
'_context' => $context ?? null,
]);
}
Expand All @@ -212,11 +222,44 @@ private function parseRoute(Analysis $analysis, array $controllerClasses)
foreach ($refMethod->getParameters() as $param)
{
$docParam = $this->getDocParam($docParams, $param->getName());
$type = $param->getType();
if ($type && ReflectionUtil::allowsType($type, 'array'))
{
if ($docParam && $type = $docParam->getType())
{
$type = $type->__toString();
$types = explode('|', $type);
foreach ($types as &$type)
{
if (str_ends_with($type, '[]'))
{
$type = substr($type, 0, -2);
break;
}
}
unset($type);
$type = implode('|', $types);
$items = new Items([
'type' => $type,
]);
}
else
{
$items = new Items([
'type' => 'mixed',
]);
}
}
else
{
$items = Generator::UNDEFINED;
}
$properties[] = new Property([
'property' => $param->getName(),
'type' => ReflectionUtil::getTypeCode($param->getType(), $refMethod->getDeclaringClass()->getName()),
'title' => $docParam ? (string) $docParam->getDescription() : \OpenApi\Annotations\UNDEFINED,
'title' => $docParam ? (string) $docParam->getDescription() : Generator::UNDEFINED,
'_context' => $context ?? null,
'items' => $items,
]);
}
$schema = new Schema([
Expand Down

0 comments on commit f9b3c77

Please sign in to comment.