Skip to content

Commit 8a8eb3e

Browse files
committed
add customerRoute
1 parent 49d5adf commit 8a8eb3e

File tree

6 files changed

+70
-8
lines changed

6 files changed

+70
-8
lines changed

.openapi.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
return [
4+
'title' => 'API Docs',
5+
'description' => 'API Docs description.',
6+
'headers' => [
7+
8+
],
9+
'service' => [
10+
11+
],
12+
];

src/OpenApi/OpenApi.php renamed to src/OpenApi.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Astral\Serialize\OpenApi;
5+
namespace Astral\Serialize;
66

77
use Astral\Serialize\OpenApi\Annotations\Headers;
88
use 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
}

src/OpenApi/Handler/Handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
/**

src/OpenApi/Storage/OpenAPI/RequestBodyStorage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
use Astral\Serialize\OpenApi\Enum\ContentTypeEnum;
88
use Astral\Serialize\OpenApi\Storage\StorageInterface;
9+
use stdClass;
910

1011
/**
1112
* 参数配置
1213
*/
1314
class RequestBodyStorage implements StorageInterface
1415
{
15-
public array $parameters;
16+
public array|stdClass $parameters = [];
1617

1718
public function __construct(
1819
public ContentTypeEnum $contentType = ContentTypeEnum::JSON,

tests/Openapi/OpenApi.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Astral\Serialize\OpenApi\OpenApi;
3+
use Astral\Serialize\OpenApi;
44
use Astral\Serialize\Serialize;
55

66
beforeAll(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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
});

0 commit comments

Comments
 (0)