File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php
2+
3+ return [
4+ 'title ' => 'API Docs ' ,
5+ 'description ' => 'API Docs description. ' ,
6+ 'headers ' => [
7+
8+ ],
9+ 'service ' => [
10+
11+ ],
12+ ];
Original file line number Diff line number Diff line change 22
33declare (strict_types=1 );
44
5- namespace Astral \Serialize \ OpenApi ;
5+ namespace Astral \Serialize ;
66
77use Astral \Serialize \OpenApi \Annotations \Headers ;
88use Astral \Serialize \OpenApi \Annotations \RequestBody ;
@@ -53,13 +53,17 @@ public function buildByClass(string $className): void
5353 Headers::class => null ,
5454 ];
5555
56+
5657 foreach ($ methodAttributes as $ methodAttribute ) {
57- $ name = $ methodAttribute ->getName ();
58- if (array_key_exists ($ name ,$ instances )) {
59- $ instances [$ name ] = $ methodAttribute ->newInstance ();
58+ $ inst = $ methodAttribute ->newInstance ();
59+ foreach (array_keys ($ instances ) as $ anchorClass ) {
60+ if ($ inst instanceof $ anchorClass ) {
61+ $ instances [$ anchorClass ] = $ inst ;
62+ }
6063 }
6164 }
6265
66+
6367 if ($ instances [Route::class] === null || $ instances [Summary::class] === null ) {
6468 continue ;
6569 }
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ abstract class Handler implements HandleInterface
2121 public function __construct (
2222 protected readonly ParameterStorage $ headerParameterStorages = new ParameterStorage ()
2323 ) {
24- self ::$ OpenAPI ??= (new OpenAPI ())->withApiInfo (new ApiInfo ('API 接口 ' ,'' ));
24+ self ::$ OpenAPI ??= (new OpenAPI ())->withApiInfo (new ApiInfo ('API Doc ' ,'' ));
2525 }
2626
2727 /**
Original file line number Diff line number Diff line change 66
77use Astral \Serialize \OpenApi \Enum \ContentTypeEnum ;
88use Astral \Serialize \OpenApi \Storage \StorageInterface ;
9+ use stdClass ;
910
1011/**
1112 * 参数配置
1213 */
1314class RequestBodyStorage implements StorageInterface
1415{
15- public array $ parameters ;
16+ public array | stdClass $ parameters = [] ;
1617
1718 public function __construct (
1819 public ContentTypeEnum $ contentType = ContentTypeEnum::JSON ,
Original file line number Diff line number Diff line change 11<?php
22
3- use Astral \Serialize \OpenApi \ OpenApi ;
3+ use Astral \Serialize \OpenApi ;
44use Astral \Serialize \Serialize ;
55
66beforeAll (static function () {
@@ -59,7 +59,6 @@ public function one(TestOpenApiRequest $request): TestOpenApiResponse
5959
6060 // 顶层结构断言
6161 expect ($ openApi ->openapi )->toBe ('3.1.1 ' )
62- ->and ($ openApi ->info ->title )->toBe ('API 接口 ' )
6362 ->and ($ openApi ->info ->version )->toBe ('1.0.0 ' )
6463 ->and ($ openApi ->tags [0 ]->name )->toBe ('接口测试 ' );
6564
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ use Astral \Serialize \OpenApi ;
4+ use Astral \Serialize \Serialize ;
5+
6+ beforeAll (static function () {
7+
8+
9+ #[Attribute(Attribute::TARGET_METHOD )]
10+ class CustomerRoute extends OpenApi \Annotations \Route
11+ {
12+
13+ }
14+
15+ class TestCustomerRouteRequest extends Serialize
16+ {
17+ public string $ name ;
18+
19+ public int $ id ;
20+ }
21+
22+ #[\Astral \Serialize \OpenApi \Annotations \Tag('接口测试 ' )]
23+ class TestCustomerRouteController{
24+
25+ #[\Astral \Serialize \OpenApi \Annotations \Summary('测试方法一 ' )]
26+ #[CustomerRoute('/test/customer-route ' )]
27+ public function one (TestCustomerRouteRequest $ request ): void
28+ {
29+ }
30+
31+ }
32+
33+ });
34+
35+ test ('OpenAPI structure is correct ' , function () {
36+
37+ $ api = new OpenApi ();
38+ $ api ->buildByClass (TestCustomerRouteController::class);
39+
40+ $ openApi = $ api ::$ OpenAPI ;
41+
42+ // 路径是否存在
43+ $ paths = $ openApi ->paths ;
44+ expect ($ paths )->toHaveKey ('/test/customer-route ' );
45+
46+ });
You can’t perform that action at this time.
0 commit comments