Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions example/Controller/DemoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand Down
2 changes: 1 addition & 1 deletion example/DTO/Request/DemoQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DemoQuery

#[ApiModelProperty('类型')]
#[In(['a', 'b'])]
private string $type;
public string $type;



Expand Down
31 changes: 25 additions & 6 deletions src/Swagger/GenerateParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Swagger/SwaggerCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\');
Expand Down