Skip to content

Commit 613aa9d

Browse files
committed
fix tests
1 parent f84b001 commit 613aa9d

File tree

7 files changed

+126
-10
lines changed

7 files changed

+126
-10
lines changed

composer.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
"Astral\\Benchmarks\\": "benchmarks/"
4040
}
4141
},
42-
"bin": [
43-
"src/OpenApi/bin/art"
44-
],
4542
"config": {
4643
"allow-plugins": {
4744
"pestphp/pest-plugin": true

src/OpenApi/Enum/ParameterTypeEnum.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,32 @@ public static function getByTypes(array $types): ParameterTypeEnum
9898
}
9999

100100
$hasUnion = false;
101+
$unionTypes = [];
101102
foreach ($types as $type){
103+
104+
if($type->kind === TypeKindEnum::ENUM){
105+
$unionTypes[TypeKindEnum::STRING->name] = $type;
106+
}else{
107+
$unionTypes[$type->kind->name] = $type;
108+
}
109+
102110
if($type->kind === TypeKindEnum::COLLECT_UNION_OBJECT){
103111
$hasUnion = true;
104112
}
105113
}
106114

107-
return $hasUnion ? self::ANY_OF : self::ONE_OF;
115+
if(!$hasUnion && count($unionTypes) === 1){
116+
$type = current($types)->kind;
117+
return match (true){
118+
$type === TypeKindEnum::INT => self::INTEGER,
119+
$type === TypeKindEnum::FLOAT => self::NUMBER,
120+
$type === TypeKindEnum::BOOLEAN => self::BOOLEAN,
121+
in_array($type, [TypeKindEnum::OBJECT, TypeKindEnum::CLASS_OBJECT, TypeKindEnum::ARRAY, TypeKindEnum::COLLECT_SINGLE_OBJECT, TypeKindEnum::COLLECT_UNION_OBJECT], true) => self::ONE_OF,
122+
default => self::STRING,
123+
};
124+
}
108125

126+
return $hasUnion ? self::ANY_OF : self::ONE_OF;
109127

110128
}
111129
}

src/OpenApi/Handler/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class Config
1717
{
18-
public static array $config;
18+
public static ?array $config = null;
1919

2020
public static function rootPath(): string
2121
{

src/OpenApi/Storage/OpenAPI/SchemaStorage.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ public function buildOfProperties(ParameterCollection $topParameter, array &$cur
9898
$node = &$currentNode['properties'][$propertyName][$topParameter->type->value];
9999

100100
$i = 0;
101+
$addedTypes = [];
101102
foreach ($topParameter->types as $kindType){
102103
$type = ParameterTypeEnum::getBaseEnumByTypeKindEnum($kindType);
103-
if($type){
104-
$node[$i] = ['type'=> $type];
104+
if ($type && !in_array($type->value, $addedTypes, true)) {
105+
$node[$i] = ['type' => $type->value];
106+
$addedTypes[] = $type->value;
105107
$i++;
106108
}
107109
}
@@ -175,7 +177,7 @@ public function getDescriptions(ParameterCollection $parameter):string
175177
}
176178
}
177179

178-
$descriptions .= ' Optional values: ' . implode('', $names);
180+
$descriptions .= 'Optional values:' . implode('', $names);
179181

180182
return $descriptions;
181183
}

tests/Openapi/EnumOpenApiTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
use Astral\Serialize\OpenApi;
4+
use Astral\Serialize\Serialize;
5+
6+
beforeAll(static function () {
7+
8+
enum OpenapiEnum
9+
{
10+
case ENUM_1;
11+
case ENUM_2;
12+
}
13+
14+
enum OpenapiUnionEnum
15+
{
16+
case ENUM_2;
17+
case ENUM_3;
18+
19+
case ENUM_4;
20+
21+
}
22+
23+
24+
25+
class OpenapiEnumRequest extends Serialize
26+
{
27+
public OpenapiEnum $test_enum;
28+
public OpenapiEnum|OpenapiUnionEnum $test_string_enum;
29+
30+
public OpenapiEnum|OpenapiUnionEnum|string $test_string_2_enum;
31+
32+
public OpenapiEnum|OpenapiUnionEnum|string|int $test_one_of_enum;
33+
34+
}
35+
36+
#[\Astral\Serialize\OpenApi\Annotations\Tag('接口测试')]
37+
class OpenapiEnumController{
38+
39+
#[\Astral\Serialize\OpenApi\Annotations\Summary('测试方法一')]
40+
#[\Astral\Serialize\OpenApi\Annotations\Route('/test/enum-action')]
41+
public function one(OpenapiEnumRequest $request): void
42+
{
43+
}
44+
45+
}
46+
47+
});
48+
49+
test('OpenAPI enums auto create description', function () {
50+
51+
$api = new OpenApi();
52+
$api->buildByClass(OpenapiEnumController::class);
53+
54+
$openApi = $api::$OpenAPI;
55+
56+
57+
$paths = $openApi->paths;
58+
$post = $paths['/test/enum-action']['post'];
59+
$requestBody = $post->requestBody;
60+
$schema = $requestBody['content']['application/json']['schema'];
61+
62+
expect($schema['properties'])
63+
->toHaveKeys([
64+
'test_enum',
65+
'test_string_enum',
66+
'test_string_2_enum',
67+
'test_one_of_enum',
68+
])
69+
->and($schema['properties']['test_enum'])->toMatchArray([
70+
'type' => 'string',
71+
'description' => 'Optional values:ENUM_1、ENUM_2',
72+
'example' => '',
73+
])
74+
->and($schema['properties']['test_string_enum'])->toMatchArray([
75+
'type' => 'string',
76+
'description' => 'Optional values:ENUM_1、ENUM_2、ENUM_3、ENUM_4',
77+
'example' => '',
78+
])
79+
->and($schema['properties']['test_string_2_enum'])->toMatchArray([
80+
'type' => 'string',
81+
'description' => 'Optional values:ENUM_1、ENUM_2、ENUM_3、ENUM_4',
82+
'example' => '',
83+
])
84+
->and($schema['properties']['test_one_of_enum'])->toMatchArray([
85+
'type' => 'oneOf',
86+
'description' => 'Optional values:ENUM_1、ENUM_2、ENUM_3、ENUM_4',
87+
'example' => '',
88+
])
89+
->and($schema['properties']['test_one_of_enum']['oneOf'])->toBeArray()->toHaveCount(2)
90+
->and($schema['properties']['test_one_of_enum']['oneOf'][0])->toMatchArray(['type' => 'string'])
91+
->and($schema['properties']['test_one_of_enum']['oneOf'][1])->toMatchArray(['type' => 'integer'])
92+
->and($schema['required'])->toBeArray()->toEqualCanonicalizing([
93+
'test_enum',
94+
'test_string_enum',
95+
'test_string_2_enum',
96+
'test_one_of_enum',
97+
]);
98+
99+
});

tests/Openapi/OpenApi.php renamed to tests/Openapi/OpenApiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function one(TestOpenApiRequest $request): TestOpenApiResponse
8181

8282
// id 字段是 oneOf 并包含 string, integer, number
8383
$idOneOf = $schema['properties']['id']['oneOf'];
84-
$types = array_map(static fn($item) => $item['type']->value, $idOneOf);
84+
$types = array_map(static fn($item) => $item['type'], $idOneOf);
8585
expect($types)->toMatchArray(['string', 'integer', 'number']);
8686

8787
// any_array 是 oneOf 并包含至少一个 array 类型

tests/Openapi/RouteOrewriteOpenApi.php renamed to tests/Openapi/RouteOrewriteOpenApiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function one(TestCustomerRouteRequest $request): void
3232

3333
});
3434

35-
test('OpenAPI structure is correct', function () {
35+
test('OpenAPI customer route', function () {
3636

3737
$api = new OpenApi();
3838
$api->buildByClass(TestCustomerRouteController::class);

0 commit comments

Comments
 (0)