Skip to content

Commit fe825d8

Browse files
authored
Merge pull request #41 from tw2066/json_array
支持json数组
2 parents 723c49a + bc17a0f commit fe825d8

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

example/Controller/DemoController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ public function api(#[RequestQuery] #[Valid] DemoQuery $request): DataType
6565
return new DataType();
6666
}
6767

68+
/**
69+
* @param DemoQuery[] $request
70+
*/
71+
#[ApiOperation(summary: '查询测试POST Arr')]
72+
#[PostMapping(path: 'apiArr')]
73+
public function apiArr(#[RequestBody] #[Valid] array $request, Address $address): array
74+
{
75+
dump($request);
76+
return $request;
77+
}
78+
6879
#[ApiOperation(summary: '查询测试POST')]
6980
#[PostMapping(path: 'api')]
7081
#[ApiHeader(name: 'test', required: true, type: 'string')]

example/DTO/Request/DemoQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DemoQuery
2222

2323
#[ApiModelProperty('类型')]
2424
#[In(['a', 'b'])]
25-
private string $type;
25+
public string $type;
2626

2727

2828

src/Swagger/GenerateParameters.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Hyperf\DTO\ApiAnnotation;
1717
use Hyperf\DTO\DtoConfig;
1818
use Hyperf\DTO\Scan\MethodParametersManager;
19+
use Hyperf\DTO\Scan\Property;
1920
use Hyperf\DTO\Scan\PropertyManager;
2021
use OpenApi\Attributes as OA;
2122
use Psr\Container\ContainerInterface;
@@ -67,12 +68,19 @@ public function generate(): array
6768
continue;
6869
}
6970

71+
$methodParameter = $this->methodParametersManager->getMethodParameter($this->controller, $this->action, $paramName);
72+
if ($parameterClassName === 'array' && $methodParameter->isRequestBody()) {
73+
$requestBody = new OA\RequestBody();
74+
$requestBody->required = true;
75+
$property = $this->methodParametersManager->getProperty($this->controller, $this->action, $paramName);
76+
$requestBody->content = $this->getContent($property->arrClassName ?? '', property: $property);
77+
$result['requestBody'] = $requestBody;
78+
}
79+
7080
if ($this->container->has($parameterClassName)) {
71-
$methodParameter = $this->methodParametersManager->getMethodParameter($this->controller, $this->action, $paramName);
7281
if ($methodParameter == null) {
7382
continue;
7483
}
75-
7684
if ($methodParameter->isRequestBody()) {
7785
$requestBody = new OA\RequestBody();
7886
$requestBody->required = true;
@@ -177,21 +185,32 @@ public function getParameterArrByClass(string $parameterClassName, string $in):
177185
return $parameters;
178186
}
179187

180-
protected function getContent(string $className, string $mediaTypeStr = 'application/json'): array
188+
protected function getContent(string $className, string $mediaTypeStr = 'application/json', ?Property $property = null): array
181189
{
182190
$arr = [];
183191
$mediaType = new OA\MediaType();
184192
$mediaType->mediaType = $mediaTypeStr;
185-
$mediaType->schema = $this->getJsonContent($className);
193+
$mediaType->schema = $this->getJsonContent($className, $property);
186194
$arr[] = $mediaType;
187195
return $arr;
188196
}
189197

190-
protected function getJsonContent(string $className): OA\JsonContent
198+
protected function getJsonContent(string $className, ?Property $property = null): OA\JsonContent
191199
{
192200
$jsonContent = new OA\JsonContent();
193201
$this->swaggerComponents->generateSchemas($className);
194-
$jsonContent->ref = $this->common->getComponentsName($className);
202+
if ($property?->phpSimpleType == 'array') {
203+
$jsonContent->type = 'array';
204+
$items = new OA\Items();
205+
if ($property->arrClassName) {
206+
$items->ref = $this->common->getComponentsName($property->arrClassName);
207+
} else {
208+
$items->type = $this->common->getSwaggerType($property->arrSimpleType);
209+
}
210+
$jsonContent->items = $items;
211+
} else {
212+
$jsonContent->ref = $this->common->getComponentsName($className);
213+
}
195214
return $jsonContent;
196215
}
197216

src/Swagger/SwaggerCommon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function simpleClassNameClear(): void
3232
*/
3333
public function getSimpleClassName(?string $className): string
3434
{
35-
if ($className === null) {
35+
if (empty($className)) {
3636
$className = 'Null';
3737
}
3838
$className = ltrim($className, '\\');

0 commit comments

Comments
 (0)