From bc17a0fc52860f29ac235b14392b822984f2445d Mon Sep 17 00:00:00 2001 From: tw Date: Wed, 11 Feb 2026 16:15:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81json=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Controller/DemoController.php | 11 ++++++++++ example/DTO/Request/DemoQuery.php | 2 +- src/Swagger/GenerateParameters.php | 31 +++++++++++++++++++++------ src/Swagger/SwaggerCommon.php | 2 +- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/example/Controller/DemoController.php b/example/Controller/DemoController.php index e8537b5..a349c33 100644 --- a/example/Controller/DemoController.php +++ b/example/Controller/DemoController.php @@ -65,6 +65,17 @@ public function api(#[RequestQuery] #[Valid] DemoQuery $request): DataType return new DataType(); } + /** + * @param DemoQuery[] $request + */ + #[ApiOperation(summary: '查询测试POST Arr')] + #[PostMapping(path: 'apiArr')] + public function apiArr(#[RequestBody] #[Valid] array $request, Address $address): array + { + dump($request); + return $request; + } + #[ApiOperation(summary: '查询测试POST')] #[PostMapping(path: 'api')] #[ApiHeader(name: 'test', required: true, type: 'string')] diff --git a/example/DTO/Request/DemoQuery.php b/example/DTO/Request/DemoQuery.php index df7d9e0..79bf406 100644 --- a/example/DTO/Request/DemoQuery.php +++ b/example/DTO/Request/DemoQuery.php @@ -22,7 +22,7 @@ class DemoQuery #[ApiModelProperty('类型')] #[In(['a', 'b'])] - private string $type; + public string $type; diff --git a/src/Swagger/GenerateParameters.php b/src/Swagger/GenerateParameters.php index abce312..f941b6f 100644 --- a/src/Swagger/GenerateParameters.php +++ b/src/Swagger/GenerateParameters.php @@ -16,6 +16,7 @@ use Hyperf\DTO\ApiAnnotation; use Hyperf\DTO\DtoConfig; use Hyperf\DTO\Scan\MethodParametersManager; +use Hyperf\DTO\Scan\Property; use Hyperf\DTO\Scan\PropertyManager; use OpenApi\Attributes as OA; use Psr\Container\ContainerInterface; @@ -67,12 +68,19 @@ public function generate(): array continue; } + $methodParameter = $this->methodParametersManager->getMethodParameter($this->controller, $this->action, $paramName); + if ($parameterClassName === 'array' && $methodParameter->isRequestBody()) { + $requestBody = new OA\RequestBody(); + $requestBody->required = true; + $property = $this->methodParametersManager->getProperty($this->controller, $this->action, $paramName); + $requestBody->content = $this->getContent($property->arrClassName ?? '', property: $property); + $result['requestBody'] = $requestBody; + } + if ($this->container->has($parameterClassName)) { - $methodParameter = $this->methodParametersManager->getMethodParameter($this->controller, $this->action, $paramName); if ($methodParameter == null) { continue; } - if ($methodParameter->isRequestBody()) { $requestBody = new OA\RequestBody(); $requestBody->required = true; @@ -177,21 +185,32 @@ public function getParameterArrByClass(string $parameterClassName, string $in): return $parameters; } - protected function getContent(string $className, string $mediaTypeStr = 'application/json'): array + protected function getContent(string $className, string $mediaTypeStr = 'application/json', ?Property $property = null): array { $arr = []; $mediaType = new OA\MediaType(); $mediaType->mediaType = $mediaTypeStr; - $mediaType->schema = $this->getJsonContent($className); + $mediaType->schema = $this->getJsonContent($className, $property); $arr[] = $mediaType; return $arr; } - protected function getJsonContent(string $className): OA\JsonContent + protected function getJsonContent(string $className, ?Property $property = null): OA\JsonContent { $jsonContent = new OA\JsonContent(); $this->swaggerComponents->generateSchemas($className); - $jsonContent->ref = $this->common->getComponentsName($className); + if ($property?->phpSimpleType == 'array') { + $jsonContent->type = 'array'; + $items = new OA\Items(); + if ($property->arrClassName) { + $items->ref = $this->common->getComponentsName($property->arrClassName); + } else { + $items->type = $this->common->getSwaggerType($property->arrSimpleType); + } + $jsonContent->items = $items; + } else { + $jsonContent->ref = $this->common->getComponentsName($className); + } return $jsonContent; } diff --git a/src/Swagger/SwaggerCommon.php b/src/Swagger/SwaggerCommon.php index 6873808..c8f2ba5 100644 --- a/src/Swagger/SwaggerCommon.php +++ b/src/Swagger/SwaggerCommon.php @@ -32,7 +32,7 @@ public function simpleClassNameClear(): void */ public function getSimpleClassName(?string $className): string { - if ($className === null) { + if (empty($className)) { $className = 'Null'; } $className = ltrim($className, '\\');