Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
71c4a93
init
astral-data May 8, 2025
70abf1e
Merge remote-tracking branch 'origin/master' into featrue/open-api
astral-data Jun 5, 2025
578e3be
ref v2
astral-data Jun 6, 2025
3e0d541
ref v2
astral-data Jun 6, 2025
ab7f5d1
RC1
astral-data Jun 12, 2025
5e1c858
fix phpstan RC2
astral-data Jun 12, 2025
71a0e29
fix phpstan RC2
astral-data Jun 12, 2025
baf4007
fix phpstan RC2
astral-data Jun 12, 2025
7f6e732
composer 固定8.1
astral-data Jun 12, 2025
7197714
change SerializeBench
astral-data Jun 12, 2025
0e63639
phpstan fix
astral-data Jun 12, 2025
8b5359c
add openapi test
astral-data Jun 13, 2025
49d5adf
add openapi test
astral-data Jun 13, 2025
8a8eb3e
add customerRoute
astral-data Jun 13, 2025
2797635
add config
astral-data Jun 13, 2025
b3bfaa4
add config
astral-data Jun 16, 2025
60ec382
add config
astral-data Jun 16, 2025
f84b001
add openapi Optional values by enums
astral-data Jun 18, 2025
613aa9d
fix tests
astral-data Jun 18, 2025
2e99b84
add openapiAnnotation
astral-data Jun 20, 2025
74d2d0d
add openapiAnnotation
astral-data Jun 20, 2025
6265c71
add openapiAnnotation
astral-data Jun 20, 2025
fbe8af0
test docs
astral-data Jun 20, 2025
c1b25b9
test docs
astral-data Jun 20, 2025
829ecb6
test docs
astral-data Jun 20, 2025
0d0d785
test docs
astral-data Jun 20, 2025
05aa13f
add config
astral-data Jun 20, 2025
dab4600
add config
astral-data Jun 20, 2025
d974fde
gitbook test
astral-data Jun 20, 2025
8ddc8bf
gitbook test
astral-data Jun 20, 2025
3c5fcea
gitbook test
astral-data Jun 20, 2025
e684ad1
gitbook test
astral-data Jun 20, 2025
9d9d261
gitbook test
astral-data Jun 20, 2025
86de98c
gitbook test
astral-data Jun 20, 2025
be44e20
gitbook test
astral-data Jun 20, 2025
ca7e79f
gitbook zh success
astral-data Jun 20, 2025
cab9685
gitbook zh success
astral-data Jun 20, 2025
3220fe4
gitbook zh success
astral-data Jun 20, 2025
171df3c
gitbook zh success
astral-data Jun 20, 2025
4c4d50b
gitbook zh success
astral-data Jun 20, 2025
7d74bbc
gitbook zh success
astral-data Jun 20, 2025
a3722bb
gitbook zh success
astral-data Jun 20, 2025
8fc8475
gitbook zh success
astral-data Jun 20, 2025
7719415
gitbook zh success
astral-data Jun 20, 2025
531211f
Merge branch 'master' into featrue/open-api
astral-data Jun 20, 2025
6b2e165
change composer.json
astral-data Jun 25, 2025
ae652ba
change composer.json
astral-data Jun 25, 2025
0daf3cd
Merge branch 'refs/heads/master' into featrue/open-api
astral-data Jun 25, 2025
ed67905
fix docs
astral-data Jun 25, 2025
928df42
add group and ignore tests
astral-data Jun 25, 2025
83ba742
add group and ignore tests
astral-data Jun 25, 2025
ec00f5c
add group and ignore tests
astral-data Jun 26, 2025
2e35860
add frankenphp
astral-data Jun 26, 2025
5d57ce9
add frankenphp
astral-data Jun 26, 2025
3ca941c
add frankenphp
astral-data Jun 26, 2025
f8323a1
add frankenphp
astral-data Jun 26, 2025
2aa6f1e
add Swagger ui
astral-data Jun 26, 2025
92d6126
add redoc ui
astral-data Jun 26, 2025
13f0c32
add ui
astral-data Jun 27, 2025
a174708
add doc
astral-data Jun 27, 2025
c58f6a4
add doc
astral-data Jun 27, 2025
ce77b88
fix tests
astral-data Jun 27, 2025
c463b72
add openapi 文档
astral-data Jun 27, 2025
2cf75af
Merge branch 'master' into featrue/open-api
astral-data Jun 27, 2025
194416a
add base response
astral-data Jul 7, 2025
6680e1f
fix tests
astral-data Jul 7, 2025
ae0dd8f
add base response success
astral-data Aug 5, 2025
19c1dc0
Merge branch 'master' into featrue/open-api
astral-data Aug 5, 2025
4819b64
add base response success
astral-data Aug 5, 2025
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
12 changes: 12 additions & 0 deletions .openapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,16 @@
* You can override or extend this list in your config file.
*/
'exclude_dirs' => [],

/**
* Response Data Structure Definition
*
* Defines the basic structure of API responses, including status code, return message, and data body
*
*/
'response' => [
'code' => ['description' =>'状态码', 'example'=> 200],
'message' => ['description' =>'返回信息', 'example'=> '操作成功'],
'data' => 'T',
]
];
12 changes: 12 additions & 0 deletions docs/en/openapi/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,17 @@ return [
*
*/
'exclude_dirs' => [],

/**
* Response Data Structure Definition
*
* Defines the basic structure of API responses, including status code, return message, and data body
*
*/
'response' => [
'code' => ['description' =>'状态码', 'example'=> 200],
'message' => ['description' =>'返回信息', 'example'=> '操作成功'],
'data' => 'T',
]
];
```
14 changes: 14 additions & 0 deletions docs/zh/openapi/base-openapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,18 @@ docker run -v $PWD/vendor/astral/php-serialize/src/OpenApi/Frankenphp/Caddyfile
```
访问 `http://127.0.0.1:8089/docs` 查看文档

![UI-IMG](./ui.png)

### Docker启动

先进入项目根目录

```shell
docker run -v $PWD/vendor/astral/php-serialize/src/OpenApi/Frankenphp/Caddyfile:/etc/frankenphp/Caddyfile -v $PWD:/app -p 8089:80 dunglas/frankenphp
```

访问 `http://127.0.0.1:8089` 展示OpenApi JSON文档

访问 `http://127.0.0.1:8089/docs` 展示UI文档

![UI-IMG](./ui.png)
12 changes: 12 additions & 0 deletions docs/zh/openapi/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,17 @@ return [
*
*/
'exclude_dirs' => [],

/**
* 响应数据结构定义
*
* 定义API响应的基本结构,包含状态码、返回信息和数据主体
*
*/
'response' => [
'code' => ['description' =>'状态码', 'example'=> 200],
'message' => ['description' =>'返回信息', 'example'=> '操作成功'],
'data' => 'T',
]
];
```
7 changes: 7 additions & 0 deletions src/OpenApi/Collections/OpenApiCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Astral\Serialize\OpenApi\Annotations\Tag;
use Astral\Serialize\OpenApi\Enum\ContentTypeEnum;
use Astral\Serialize\OpenApi\Enum\ParameterTypeEnum;
use Astral\Serialize\OpenApi\Handler\Config;
use Astral\Serialize\OpenApi\Storage\OpenAPI\Method\Method;
use Astral\Serialize\OpenApi\Storage\OpenAPI\RequestBodyStorage;
use Astral\Serialize\OpenApi\Storage\OpenAPI\ResponseStorage;
Expand Down Expand Up @@ -121,11 +122,17 @@ public function buildResponse(string $className,array $groups = []): ResponseSto
{
$responseStorage = new ResponseStorage();

$baseResponse = Config::get('response',[]);

if ($className) {
$schemaStorage = (new SchemaStorage())->build($this->buildResponseParameterCollections($className,$groups));
$responseStorage->withParameter($schemaStorage);
}

if($baseResponse){
$responseStorage->addGlobParameters($baseResponse);
}

return $responseStorage;
}

Expand Down
26 changes: 26 additions & 0 deletions src/OpenApi/Storage/OpenAPI/ResponseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ public function withParameter(SchemaStorage $schema): static
return $this;
}

public function addGlobParameters(array $vols): void
{
$dates = $this->parameter['properties'] ?? [];
$required = $this->parameter['required'] ?? [];

$this->parameter['properties'] = [];

foreach ($vols as $field => $item){
if($item === 'T'){
$this->parameter['properties'][$field] = [
'type' => 'object',
'properties' => $dates,
'required' => $required,
];
}else{
$this->parameter['properties'][$field] = [
'type' => 'string',
'description' => $item['description'] ?? '',
'example' => $item['example'],
];
}

}

}

public function getData(): array
{
return [
Expand Down
22 changes: 21 additions & 1 deletion src/Serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Astral\Serialize;

use Astral\Serialize\OpenApi\Handler\Config;
use Astral\Serialize\Support\Context\SerializeContext;
use Astral\Serialize\Support\Factories\ContextFactory;
use JsonSerializable;

abstract class Serialize
abstract class Serialize implements JsonSerializable
{
private ?SerializeContext $_context = null;

Expand Down Expand Up @@ -71,4 +73,22 @@ public function __debugInfo()

return $res;
}

public function jsonSerialize(): array
{
$baseResponse = Config::get('response',[]);
if($baseResponse){
$resultData = [];
foreach ($baseResponse as $field => $item){
if($item === 'T'){
$resultData[$field] = $this->toArray();
}else{
$resultData[$field] = $item['example'] ?? '';
}
}
return $resultData;
}

return $this->toArray();
}
}
2 changes: 1 addition & 1 deletion tests/Openapi/BaseGroupOpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function one(TestBaseGroupRequest $request): void
expect($schema['properties'])->toHaveCount(3)->toHaveKeys(['input_change_group_1_33_2_11', 'input_group_2_22', 'input_group_2_33']);


$schema = $post->responses[200]['content']['application/json']['schema'];
$schema = $post->responses[200]['content']['application/json']['schema']['properties']['data'];
expect($schema['properties'])->toHaveCount(3)->toHaveKeys(['out_group_1_11', 'out_group_1_22', 'out_group_1_33_2_11']);

});
20 changes: 11 additions & 9 deletions tests/Openapi/EnumOpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,34 @@ public function one(OpenapiEnumRequest $request): void
$requestBody = $post->requestBody;
$schema = $requestBody['content']['application/json']['schema'];

expect($schema['properties'])

expect($schema)->toHaveKey('properties')
->and($schema['properties'])
->toHaveKeys([
'test_enum',
'test_string_enum',
'test_string_2_enum',
'test_one_of_enum',
])
->and($schema['properties']['test_enum'])->toMatchArray([
'type' => 'string',
'type' => 'string',
'description' => 'optional values:ENUM_1、ENUM_2',
'example' => '',
'example' => '',
])
->and($schema['properties']['test_string_enum'])->toMatchArray([
'type' => 'string',
'type' => 'string',
'description' => 'optional values:ENUM_1、ENUM_2、ENUM_3、ENUM_4',
'example' => '',
'example' => '',
])
->and($schema['properties']['test_string_2_enum'])->toMatchArray([
'type' => 'string',
'type' => 'string',
'description' => 'optional values:ENUM_1、ENUM_2、ENUM_3、ENUM_4',
'example' => '',
'example' => '',
])
->and($schema['properties']['test_one_of_enum'])->toMatchArray([
'type' => 'oneOf',
'type' => 'oneOf',
'description' => 'optional values:ENUM_1、ENUM_2、ENUM_3、ENUM_4',
'example' => '',
'example' => '',
])
->and($schema['properties']['test_one_of_enum']['oneOf'])->toBeArray()->toHaveCount(2)
->and($schema['properties']['test_one_of_enum']['oneOf'][0])->toMatchArray(['type' => 'string'])
Expand Down
9 changes: 5 additions & 4 deletions tests/Openapi/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ public function one(TestOpenApiRequest $request): TestOpenApiResponse
$response200 = $post->responses[200];
expect($response200['description'])->toBe('成功');

$schema = $response200['content']['application/json']['schema'];
expect($schema['properties'])->toHaveKeys(['name', 'id'])
->and($schema['required'])->toHaveCount(1)
->and($schema['required'][0])->toBeString('id');
$data = $response200['content']['application/json']['schema']['properties']['data'];

expect($data['properties'])->toHaveKeys(['name', 'id'])
->and($data['required'])->toHaveCount(1)
->and($data['required'][0])->toBeString('id');

});