Skip to content

Commit 6ecb436

Browse files
committed
fix swagger
1 parent aae76e1 commit 6ecb436

7 files changed

Lines changed: 28 additions & 21 deletions

File tree

src/Annotation/ApiDefinition.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace HPlus\Swagger\Annotation;
55

6+
use Attribute;
67
use Hyperf\Di\Annotation\AbstractAnnotation;
78
/**
89
* @Annotation

src/Annotation/ApiDefinitions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace HPlus\Swagger\Annotation;
55

6+
use Attribute;
67
use Hyperf\Di\Annotation\AbstractAnnotation;
78
/**
89
* @Annotation

src/Annotation/ApiServer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
declare(strict_types=1);
33
namespace HPlus\Swagger\Annotation;
44

5+
use Attribute;
56
use Hyperf\Di\Annotation\AbstractAnnotation;
67
/**
78
* @Annotation

src/Annotation/ApiVersion.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
declare(strict_types=1);
33
namespace HPlus\Swagger\Annotation;
44

5+
use Attribute;
56
use Hyperf\Di\Annotation\AbstractAnnotation;
67

78
/**

src/ApiAnnotation.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
declare(strict_types=1);
33
namespace HPlus\Swagger;
44

5-
use Doctrine\Common\Annotations\AnnotationReader;
65
use Hyperf\Di\Annotation\AnnotationCollector;
6+
use Hyperf\Di\Annotation\AnnotationReader;
77
use Hyperf\Di\ReflectionManager;
88

99
class ApiAnnotation
1010
{
1111
public static function methodMetadata($className, $methodName)
1212
{
1313
$reflectMethod = ReflectionManager::reflectMethod($className, $methodName);
14-
$reader = new AnnotationReader();
15-
14+
$reader = new AnnotationReader(config('annotations.scan.ignore_annotations', []));
1615
return $reader->getMethodAnnotations($reflectMethod);
1716
}
1817

src/BootAppConfListener.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function listen(): array
2222
];
2323
}
2424

25-
public function process(object $event)
25+
public function process(object $event):void
2626
{
2727
$container = ApplicationContext::getContainer();
2828
$logger = $container->get(LoggerFactory::class)->get('swagger');
@@ -52,10 +52,13 @@ public function process(object $event)
5252
$ignore = $config->get('swagger.ignore', function ($controller, $action) {
5353
return false;
5454
});
55-
5655
array_walk_recursive($data, function ($item) use ($swagger, $ignore) {
5756
if ($item instanceof Handler && !($item->callback instanceof \Closure)) {
5857
[$controller, $action] = $this->prepareHandler($item->callback);
58+
59+
60+
61+
5962
(!$ignore($controller, $action)) && $swagger->addPath($controller, $action, $item->route);
6063
}
6164
});

src/Swagger/SwaggerJson.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
namespace HPlus\Swagger\Swagger;
55

6-
use Doctrine\Common\Annotations\AnnotationReader;
6+
use Exception;
77
use HPlus\Route\Annotation\ApiController;
88
use HPlus\Route\Annotation\AdminController;
9+
use HPlus\Route\Annotation\GetApi;
10+
use HPlus\Route\Annotation\Mapping;
911
use HPlus\Route\Annotation\Query;
1012
use HPlus\Swagger\Annotation\ApiDefinition;
1113
use HPlus\Swagger\Annotation\ApiDefinitions;
@@ -18,7 +20,6 @@
1820
use HPlus\Swagger\ApiAnnotation;
1921
use Hyperf\Contract\ConfigInterface;
2022
use Hyperf\Di\ReflectionManager;
21-
use Hyperf\HttpServer\Annotation\Mapping;
2223
use Hyperf\Utils\ApplicationContext;
2324
use Hyperf\Utils\Arr;
2425
use Hyperf\Utils\Str;
@@ -42,10 +43,7 @@ public function __construct($server)
4243

4344
public function addPath($className, $methodName, $path)
4445
{
45-
$ignores = $this->config->get('annotations.scan.ignore_annotations', []);
46-
foreach ($ignores as $ignore) {
47-
AnnotationReader::addGlobalIgnoredName($ignore);
48-
}
46+
4947
$classAnnotation = ApiAnnotation::classMetadata($className);
5048
$controlerAnno = $classAnnotation[ApiController::class] ?? $classAnnotation[AdminController::class] ?? null;
5149
$serverAnno = $classAnnotation[ApiServer::class] ?? null;
@@ -56,19 +54,20 @@ public function addPath($className, $methodName, $path)
5654
$servers = $this->config->get('server.servers');
5755
$servers_name = array_column($servers, 'name');
5856
if (!in_array($bindServer, $servers_name)) {
59-
throw new \Exception(sprintf('The bind ApiServer name [%s] not found, defined in %s!', $bindServer, $className));
57+
throw new Exception(sprintf('The bind ApiServer name [%s] not found, defined in %s!', $bindServer, $className));
6058
}
6159

6260
if ($bindServer !== $this->server) {
6361
return;
6462
}
63+
6564
$methodAnnotations = ApiAnnotation::methodMetadata($className, $methodName);
6665
if (!$controlerAnno || !$methodAnnotations) {
6766
return;
6867
}
6968
$params = [];
7069
$responses = [];
71-
/** @var \HPlus\Route\Annotation\GetApi $mapping */
70+
/** @var GetApi $mapping */
7271
$mapping = null;
7372
$consumes = null;
7473
$rules = [];
@@ -77,7 +76,7 @@ public function addPath($className, $methodName, $path)
7776
if ($option instanceof RequestValidation) {
7877
$rules = array_merge($rules, $this->getValidateRule($option));
7978
if ($option->dateType == 'json') {
80-
$param = new Body();
79+
$param = new Body("");
8180
$param->rules = $this->getValidateRule($option);
8281
$param->name = "body";
8382
$param->key = "body";
@@ -99,6 +98,7 @@ public function addPath($className, $methodName, $path)
9998
$consumes = $this->getConsumes($option->dateType);
10099
continue;
101100
}
101+
102102
if ($option instanceof Mapping) {
103103
$mapping = $option;
104104
}
@@ -132,12 +132,13 @@ public function addPath($className, $methodName, $path)
132132
}
133133
$path = str_replace("/_self_path", "", $path);
134134
$path = $this->getPath($path);
135-
$method = strtolower($mapping->methods[0]);
135+
136+
$method = strtolower($mapping->methods[0] ?? '');
136137
$this->swagger['paths'][$path][$method] = [
137138
'tags' => [$tag],
138139
'summary' => $mapping->summary ?? '',
139140
'description' => $mapping->description ?? '',
140-
'operationId' => implode('', array_map('ucfirst', explode('/', $path))) . $mapping->methods[0],
141+
'operationId' => implode('', array_map('ucfirst', explode('/', $path))) . ($mapping->methods[0] ?? ''),
141142
'parameters' => $this->makeParameters($params, $path, $method),
142143
'produces' => [
143144
$consumes
@@ -147,7 +148,7 @@ public function addPath($className, $methodName, $path)
147148
if ($consumes !== null) {
148149
$this->swagger['paths'][$path][$method]['consumes'] = [$consumes];
149150
}
150-
if (property_exists($mapping, 'security') && $mapping->security && isset($this->swagger['securityDefinitions'])) {
151+
if ($mapping && property_exists($mapping, 'security') && $mapping->security && isset($this->swagger['securityDefinitions'])) {
151152
foreach ($this->swagger['securityDefinitions'] as $key => $val) {
152153
$this->swagger['paths'][$path][$method]['security'][] = [$key => $val['petstore_auth'] ?? []];
153154
}
@@ -168,7 +169,7 @@ private function getConsumes($type = 'json')
168169

169170
private function getValidateRule(RequestValidation $validation)
170171
{
171-
if (class_exists($validation->validate)) {
172+
if ($validation->validate && class_exists($validation->validate)) {
172173
$rolesModel = ReflectionManager::reflectClass($validation->validate)->getDefaultProperties();
173174
$rules = $rolesModel['scene'][$validation->scene] ?? [];
174175
$fields = $rolesModel['field'] ?? [];
@@ -254,7 +255,7 @@ private function rules2schema($name, $rules)
254255

255256
public function getTypeByRule($rule)
256257
{
257-
if (empty($rule)) {
258+
if (empty($rule)) {
258259
return 'string';
259260
}
260261
$default = explode('|', preg_replace('/\[.*\]/', '', $rule));
@@ -294,7 +295,7 @@ public function makeParameters($params, $path, $method)
294295
$parameters = [];
295296
/** @var Query $item */
296297
foreach ($params as $item) {
297-
if ($item->rule !== null && in_array('array', explode('|', $item->rule))) {
298+
if ($item->rule && $item->rule !== null && in_array('array', explode('|', $item->rule))) {
298299
$item->name .= '[]';
299300
}
300301
$parameters[$item->key] = [
@@ -467,7 +468,7 @@ public function save()
467468
$this->swagger['tags'] = array_values($this->swagger['tags'] ?? []);
468469
$outputFile = $this->config->get('swagger.output_file');
469470
if (!$outputFile) {
470-
throw new \Exception('/config/autoload/swagger.php need set output_file');
471+
throw new Exception('/config/autoload/swagger.php need set output_file');
471472
}
472473
$outputFile = str_replace('{server}', $this->server, $outputFile);
473474
file_put_contents($outputFile, json_encode($this->swagger, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

0 commit comments

Comments
 (0)