Skip to content

Commit 177fc5f

Browse files
committed
schema/openapi parameter
1 parent aac5327 commit 177fc5f

File tree

5 files changed

+87
-92
lines changed

5 files changed

+87
-92
lines changed

src/Doctrine/Orm/Filter/AbstractUuidFilter.php

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -168,61 +168,56 @@ protected function getDoctrineArrayParameterType(): ?ArrayParameterType
168168
return null;
169169
}
170170

171-
public function getOpenApiParameters(Parameter $parameter): array
171+
public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null
172172
{
173173
$in = $parameter instanceof QueryParameter ? 'query' : 'header';
174-
$key = $parameter->getKey();
175174
$schema = $parameter->getSchema();
176-
$addStringParam = false;
177-
$addArrayParam = false;
178-
$openApiParameters = [];
175+
$isArraySchema = 'array' === ($schema['type'] ?? null);
176+
$hasNonArrayType = isset($schema['type']) && 'array' !== $schema['type'];
179177

180-
if (null === $schema) {
181-
$addStringParam = true;
182-
$addArrayParam = true;
183-
}
184-
185-
foreach ($schema['oneOf'] ?? [$schema] as $item) {
186-
if (!isset($item['type']) || 'string' === $item['type']) {
187-
$addStringParam = true;
188-
}
189-
190-
if (!isset($item['type']) || 'array' === $item['type']) {
191-
$addArrayParam = true;
192-
}
193-
}
178+
// Get filter's base schema
179+
$baseSchema = self::UUID_SCHEMA;
180+
$arraySchema = ['type' => 'array', 'items' => $baseSchema];
194181

195-
if ($addStringParam) {
196-
$openApiParameters[] = new OpenApiParameter(
197-
name: $key,
182+
if ($isArraySchema) {
183+
return new OpenApiParameter(
184+
name: $parameter->getKey().'[]',
198185
in: $in,
199-
schema: self::UUID_SCHEMA,
200-
style: 'form',
201-
explode: false
186+
schema: $arraySchema,
187+
style: 'deepObject',
188+
explode: true,
202189
);
203190
}
204191

205-
if ($addArrayParam) {
206-
$openApiParameters[] = new OpenApiParameter(
207-
name: $key.'[]',
192+
if ($hasNonArrayType) {
193+
return new OpenApiParameter(
194+
name: $parameter->getKey(),
208195
in: $in,
209-
description: 'One or more Uuids',
210-
schema: [
211-
'type' => 'array',
212-
'items' => self::UUID_SCHEMA,
213-
],
214-
style: 'deepObject',
215-
explode: true
196+
schema: $baseSchema,
216197
);
217198
}
218199

219-
return $openApiParameters;
200+
// oneOf or no specific type constraint - return both with explicit schemas
201+
return [
202+
new OpenApiParameter(
203+
name: $parameter->getKey(),
204+
in: $in,
205+
schema: $baseSchema,
206+
),
207+
new OpenApiParameter(
208+
name: $parameter->getKey().'[]',
209+
in: $in,
210+
schema: $arraySchema,
211+
style: 'deepObject',
212+
explode: true,
213+
),
214+
];
220215
}
221216

222217
public function getSchema(Parameter $parameter): array
223218
{
224-
if (null !== $parameter->getSchema()) {
225-
return $parameter->getSchema();
219+
if (false === $parameter->getCastToArray()) {
220+
return self::UUID_SCHEMA;
226221
}
227222

228223
return [

src/Doctrine/Orm/Filter/UlidFilter.php

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,65 @@ final class UlidFilter extends AbstractUuidFilter
2424
'format' => 'ulid',
2525
];
2626

27-
public function getOpenApiParameters(Parameter $parameter): array
27+
public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null
2828
{
2929
$in = $parameter instanceof QueryParameter ? 'query' : 'header';
30-
$key = $parameter->getKey();
30+
$schema = $parameter->getSchema();
31+
$isArraySchema = 'array' === ($schema['type'] ?? null);
32+
$hasNonArrayType = isset($schema['type']) && 'array' !== $schema['type'];
3133

34+
$baseSchema = self::ULID_SCHEMA;
35+
$arraySchema = ['type' => 'array', 'items' => $baseSchema];
36+
37+
if ($isArraySchema) {
38+
return new OpenApiParameter(
39+
name: $parameter->getKey().'[]',
40+
in: $in,
41+
schema: $arraySchema,
42+
style: 'deepObject',
43+
explode: true,
44+
);
45+
}
46+
47+
if ($hasNonArrayType) {
48+
return new OpenApiParameter(
49+
name: $parameter->getKey(),
50+
in: $in,
51+
schema: $baseSchema,
52+
);
53+
}
54+
55+
// oneOf or no specific type constraint - return both with explicit schemas
3256
return [
3357
new OpenApiParameter(
34-
name: $key,
58+
name: $parameter->getKey(),
3559
in: $in,
36-
schema: self::ULID_SCHEMA,
37-
style: 'form',
38-
explode: false
60+
schema: $baseSchema,
3961
),
4062
new OpenApiParameter(
41-
name: $key.'[]',
63+
name: $parameter->getKey().'[]',
4264
in: $in,
43-
description: 'One or more Ulids',
44-
schema: [
45-
'type' => 'array',
46-
'items' => self::ULID_SCHEMA,
47-
],
65+
schema: $arraySchema,
4866
style: 'deepObject',
49-
explode: true
67+
explode: true,
5068
),
5169
];
5270
}
5371

5472
public function getSchema(Parameter $parameter): array
5573
{
56-
return self::ULID_SCHEMA;
74+
if (false === $parameter->getCastToArray()) {
75+
return self::ULID_SCHEMA;
76+
}
77+
78+
return [
79+
'oneOf' => [
80+
self::ULID_SCHEMA,
81+
[
82+
'type' => 'array',
83+
'items' => self::ULID_SCHEMA,
84+
],
85+
],
86+
];
5787
}
5888
}

tests/Functional/Uuid/UuidFilterBaseTestCase.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,26 +286,20 @@ public function testGetOpenApiDescription(): void
286286
[
287287
'name' => 'id',
288288
'in' => 'query',
289-
'description' => '',
290289
'required' => false,
291-
'deprecated' => false,
292290
'schema' => [
293291
'type' => 'string',
294292
'format' => 'uuid',
295293
],
296-
'style' => 'form',
297-
'explode' => false,
298294
],
299-
$json['paths']['/'.$this->getUrlPrefix().'_device_endpoints']['get']['parameters']
295+
array_map(fn ($p) => array_intersect_key($p, ['name' => 1, 'in' => 1, 'required' => 1, 'schema' => 1]), $json['paths']['/'.$this->getUrlPrefix().'_device_endpoints']['get']['parameters'])
300296
);
301297

302298
self::assertContains(
303299
[
304300
'name' => 'id[]',
305301
'in' => 'query',
306-
'description' => 'One or more Uuids',
307302
'required' => false,
308-
'deprecated' => false,
309303
'schema' => [
310304
'type' => 'array',
311305
'items' => [
@@ -316,7 +310,7 @@ public function testGetOpenApiDescription(): void
316310
'style' => 'deepObject',
317311
'explode' => true,
318312
],
319-
$json['paths']['/'.$this->getUrlPrefix().'_device_endpoints']['get']['parameters']
313+
array_map(fn ($p) => array_intersect_key($p, ['name' => 1, 'in' => 1, 'required' => 1, 'schema' => 1, 'style' => 1, 'explode' => 1]), $json['paths']['/'.$this->getUrlPrefix().'_device_endpoints']['get']['parameters'])
320314
);
321315
}
322316

tests/Functional/Uuid/UuidFilterWithCustomSchemaTest.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,20 @@ public function testGetOpenApiDescriptionWhenNoCustomerSchema(): void
5656
[
5757
'name' => 'id',
5858
'in' => 'query',
59-
'description' => '',
6059
'required' => false,
61-
'deprecated' => false,
6260
'schema' => [
6361
'type' => 'string',
6462
'format' => 'uuid',
6563
],
66-
'style' => 'form',
67-
'explode' => false,
6864
],
69-
$json['paths']['/uuid_filter_with_custom_schemas']['get']['parameters']
65+
array_map(fn ($p) => array_intersect_key($p, ['name' => 1, 'in' => 1, 'required' => 1, 'schema' => 1]), $json['paths']['/uuid_filter_with_custom_schemas']['get']['parameters'])
7066
);
7167

7268
self::assertContains(
7369
[
7470
'name' => 'id[]',
7571
'in' => 'query',
76-
'description' => 'One or more Uuids',
7772
'required' => false,
78-
'deprecated' => false,
7973
'schema' => [
8074
'type' => 'array',
8175
'items' => [
@@ -86,7 +80,7 @@ public function testGetOpenApiDescriptionWhenNoCustomerSchema(): void
8680
'style' => 'deepObject',
8781
'explode' => true,
8882
],
89-
$json['paths']['/uuid_filter_with_custom_schemas']['get']['parameters']
83+
array_map(fn ($p) => array_intersect_key($p, ['name' => 1, 'in' => 1, 'required' => 1, 'schema' => 1, 'style' => 1, 'explode' => 1]), $json['paths']['/uuid_filter_with_custom_schemas']['get']['parameters'])
9084
);
9185
}
9286

@@ -102,17 +96,13 @@ public function testGetOpenApiDescriptionWhenSchemaIsOnlyString(): void
10296
[
10397
'name' => 'idfoo',
10498
'in' => 'query',
105-
'description' => '',
10699
'required' => false,
107-
'deprecated' => false,
108100
'schema' => [
109101
'type' => 'string',
110102
'format' => 'uuid',
111103
],
112-
'style' => 'form',
113-
'explode' => false,
114104
],
115-
$json['paths']['/uuid_filter_with_custom_schemas']['get']['parameters']
105+
array_map(fn ($p) => array_intersect_key($p, ['name' => 1, 'in' => 1, 'required' => 1, 'schema' => 1]), $json['paths']['/uuid_filter_with_custom_schemas']['get']['parameters'])
116106
);
117107

118108
self::assertNotContains(
@@ -137,9 +127,7 @@ public function testGetOpenApiDescriptionWhenSchemaIsOnlyArray(): void
137127
[
138128
'name' => 'idbar[]',
139129
'in' => 'query',
140-
'description' => 'One or more Uuids',
141130
'required' => false,
142-
'deprecated' => false,
143131
'schema' => [
144132
'type' => 'array',
145133
'items' => [
@@ -150,7 +138,7 @@ public function testGetOpenApiDescriptionWhenSchemaIsOnlyArray(): void
150138
'style' => 'deepObject',
151139
'explode' => true,
152140
],
153-
$json['paths']['/uuid_filter_with_custom_schemas']['get']['parameters']
141+
array_map(fn ($p) => array_intersect_key($p, ['name' => 1, 'in' => 1, 'required' => 1, 'schema' => 1, 'style' => 1, 'explode' => 1]), $json['paths']['/uuid_filter_with_custom_schemas']['get']['parameters'])
154142
);
155143
}
156144

@@ -166,26 +154,20 @@ public function testGetOpenApiDescriptionIsOneOfArrayOrString(): void
166154
[
167155
'name' => 'idquz',
168156
'in' => 'query',
169-
'description' => '',
170157
'required' => false,
171-
'deprecated' => false,
172158
'schema' => [
173159
'type' => 'string',
174160
'format' => 'uuid',
175161
],
176-
'style' => 'form',
177-
'explode' => false,
178162
],
179-
$json['paths']['/uuid_filter_with_custom_schemas']['get']['parameters']
163+
array_map(fn ($p) => array_intersect_key($p, ['name' => 1, 'in' => 1, 'required' => 1, 'schema' => 1]), $json['paths']['/uuid_filter_with_custom_schemas']['get']['parameters'])
180164
);
181165

182166
self::assertContains(
183167
[
184168
'name' => 'idquz[]',
185169
'in' => 'query',
186-
'description' => 'One or more Uuids',
187170
'required' => false,
188-
'deprecated' => false,
189171
'schema' => [
190172
'type' => 'array',
191173
'items' => [
@@ -196,7 +178,7 @@ public function testGetOpenApiDescriptionIsOneOfArrayOrString(): void
196178
'style' => 'deepObject',
197179
'explode' => true,
198180
],
199-
$json['paths']['/uuid_filter_with_custom_schemas']['get']['parameters']
181+
array_map(fn ($p) => array_intersect_key($p, ['name' => 1, 'in' => 1, 'required' => 1, 'schema' => 1, 'style' => 1, 'explode' => 1]), $json['paths']['/uuid_filter_with_custom_schemas']['get']['parameters'])
200182
);
201183
}
202184

tests/Functional/Uuid/UuidFilterWithSymfonyUlidTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,20 @@ public function testGetOpenApiDescription(): void
6767
[
6868
'name' => 'id',
6969
'in' => 'query',
70-
'description' => '',
7170
'required' => false,
72-
'deprecated' => false,
7371
'schema' => [
7472
'type' => 'string',
7573
'format' => 'ulid',
7674
],
77-
'style' => 'form',
78-
'explode' => false,
7975
],
80-
$json['paths']['/'.$this->getUrlPrefix().'_device_endpoints']['get']['parameters']
76+
array_map(fn ($p) => array_intersect_key($p, ['name' => 1, 'in' => 1, 'required' => 1, 'schema' => 1]), $json['paths']['/'.$this->getUrlPrefix().'_device_endpoints']['get']['parameters'])
8177
);
8278

8379
self::assertContains(
8480
[
8581
'name' => 'id[]',
8682
'in' => 'query',
87-
'description' => 'One or more Ulids',
8883
'required' => false,
89-
'deprecated' => false,
9084
'schema' => [
9185
'type' => 'array',
9286
'items' => [
@@ -97,7 +91,7 @@ public function testGetOpenApiDescription(): void
9791
'style' => 'deepObject',
9892
'explode' => true,
9993
],
100-
$json['paths']['/'.$this->getUrlPrefix().'_device_endpoints']['get']['parameters']
94+
array_map(fn ($p) => array_intersect_key($p, ['name' => 1, 'in' => 1, 'required' => 1, 'schema' => 1, 'style' => 1, 'explode' => 1]), $json['paths']['/'.$this->getUrlPrefix().'_device_endpoints']['get']['parameters'])
10195
);
10296
}
10397
}

0 commit comments

Comments
 (0)