Skip to content

Featrue/open api #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 62 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 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
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
42 changes: 34 additions & 8 deletions .openapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,47 @@
use Astral\Serialize\OpenApi\Storage\OpenAPI\ServersStorage;

return [
// API Document Title
'title' => 'API Docs',

// Description of the API document
'description' => 'API Docs description.',

/**
* 向全局头部参数存储中添加一个的头部参数。
* @param string $name
* @param string $example
* @param string $description
* Global headers to be added to every request.
* Each header should include name, example, and description.
*
* Example:
* [
* 'name' => 'Authorization',
* 'example' => 'Bearer token',
* 'description' => 'Authentication token'
* ]
*/
'headers' => [

],
'headers' => [],

/**
* Service base URLs (servers).
* You can define multiple environments like production, staging, etc.
*
* @type ServersStorage[] $service
*/
'service' => [
new ServersStorage('http://127.0.0.1','默认环境'),
new ServersStorage('http://127.0.0.1', 'Default Environment'),
],

/**
* Directories to exclude from scanning.
* These paths are relative to the project root directory.
*
* Default excluded directories:
* - /vendor
* - /tests
* - /migrations
* Example:
* ['/sdk', '/app']
*
* You can override or extend this list in your config file.
*/
'exclude_dirs' => [],
];
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,68 @@ class User extends Serialize {
}
}
```

## Auto Create OpenApi Doc

### Creating Request

```php
use Astral\Serialize\Serialize;

class UserAddRequest extends Serialize {
public string $name;
public int $id;
}

class UserDetailRequest extends Serialize {
public int $id;
}
```

### Creating Response
```php
use Astral\Serialize\Serialize;

class UserDto extends Serialize {
public string $name,
public int $id;
}
```

### Creating Controller
```php
use Astral\Serialize\Serialize;
use Astral\Serialize\OpenApi\Enum\MethodEnum;

#[\Astral\Serialize\OpenApi\Annotations\Tag('User Module Management')]
class UserController {

#[\Astral\Serialize\OpenApi\Annotations\Summary('Create User')]
#[\Astral\Serialize\OpenApi\Annotations\Route('/user/create')]
#[\Astral\Serialize\OpenApi\Annotations\RequestBody(UserAddRequest::class)]
#[\Astral\Serialize\OpenApi\Annotations\Response(UserDto::class)]
public function create()
{
return new UserDto();
}

#[\Astral\Serialize\OpenApi\Annotations\Summary('User Detail')]
#[\Astral\Serialize\OpenApi\Annotations\Route(route:'/user/detail', method: MethodEnum::GET)]
public function detail(UserDetailRequest $request): UserDto
{
return new UserDto();
}
}
```
### Starting the Service

#### Docker Deployment

Navigate to the project root directory first:

```shell
docker run -v $PWD/vendor/astral/php-serialize/src/OpenApi/Frankenphp/Caddyfile:/etc/frankenphp/Caddyfile -v $PWD:/app -p 8089:80 dunglas/frankenphp
```
Access `http://127.0.0.1:8089/docs` to view the documentation.

![UI-IMG](./docs/en/openapi/ui.png)
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
"illuminate/support": "^6.0||^7.0||^8.0||^9.0||^10.0||^11.0||^12.0",
"phpdocumentor/reflection-docblock": "^5.5",
"phpdocumentor/reflection": "^6.0",
"fakerphp/faker": "^1.23",
"psr/simple-cache": "^3.0",
"symfony/console": "^6.4"
"psr/simple-cache": "^3.0"
},
"require-dev" : {
"fakerphp/faker": "^1.23",
"phpstan/phpstan": "^2.0.2",
"friendsofphp/php-cs-fixer": "^3.0",
"mockery/mockery": "^1.6",
Expand Down
Loading