Skip to content

Commit 194416a

Browse files
committed
add base response
1 parent 2cf75af commit 194416a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

.openapi.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@
4949
* You can override or extend this list in your config file.
5050
*/
5151
'exclude_dirs' => [],
52+
'response' => [
53+
'code' => '状态码',
54+
'message' => '返回信息',
55+
'data' => 'object',
56+
]
5257
];

src/OpenApi/Collections/OpenApiCollection.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Astral\Serialize\OpenApi\Annotations\Tag;
1313
use Astral\Serialize\OpenApi\Enum\ContentTypeEnum;
1414
use Astral\Serialize\OpenApi\Enum\ParameterTypeEnum;
15+
use Astral\Serialize\OpenApi\Handler\Config;
1516
use Astral\Serialize\OpenApi\Storage\OpenAPI\Method\Method;
1617
use Astral\Serialize\OpenApi\Storage\OpenAPI\RequestBodyStorage;
1718
use Astral\Serialize\OpenApi\Storage\OpenAPI\ResponseStorage;
@@ -121,11 +122,17 @@ public function buildResponse(string $className,array $groups = []): ResponseSto
121122
{
122123
$responseStorage = new ResponseStorage();
123124

125+
$baseResponse = Config::get('response',[]);
126+
124127
if ($className) {
125128
$schemaStorage = (new SchemaStorage())->build($this->buildResponseParameterCollections($className,$groups));
126129
$responseStorage->withParameter($schemaStorage);
127130
}
128131

132+
if($baseResponse){
133+
$responseStorage->addGlobParameters($baseResponse);
134+
}
135+
129136
return $responseStorage;
130137
}
131138

src/OpenApi/Storage/OpenAPI/ResponseStorage.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ public function withParameter(SchemaStorage $schema): static
2323
return $this;
2424
}
2525

26+
public function addGlobParameters(array $vols): void
27+
{
28+
foreach ($vols as $field => $description){
29+
$parameters = $this->parameter;
30+
31+
if($description !== 'object'){
32+
$this->parameter['properties'][$field] = [
33+
'type' => 'string',
34+
'description' => $description,
35+
];
36+
}else{
37+
$this->parameter['properties'][$field] = [
38+
'type' => 'object',
39+
'description' => $description,
40+
'properties' => $parameters,
41+
];
42+
}
43+
}
44+
45+
}
46+
2647
public function getData(): array
2748
{
2849
return [

0 commit comments

Comments
 (0)