Skip to content

Commit 06185b7

Browse files
authored
feat: add groups filter whitelist info to swagger (#5244)
1 parent a5e0616 commit 06185b7

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/Api/FilterInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ interface FilterInterface
4343
* 'type' => 'integer',
4444
* ]
4545
* ]
46+
* - schema (optional): schema definition,
47+
* e.g. 'schema' => [
48+
* 'type' => 'string',
49+
* 'enum' => ['value_1', 'value_2'],
50+
* ]
4651
* The description can contain additional data specific to a filter.
4752
*
4853
* @see \ApiPlatform\OpenApi\Factory\OpenApiFactory::getFiltersParameters

src/Serializer/Filter/GroupFilter.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,23 @@ public function apply(Request $request, bool $normalization, array $attributes,
5858
*/
5959
public function getDescription(string $resourceClass): array
6060
{
61-
return [
62-
"$this->parameterName[]" => [
63-
'property' => null,
64-
'type' => 'string',
65-
'is_collection' => true,
66-
'required' => false,
67-
],
61+
$description = [
62+
'property' => null,
63+
'type' => 'string',
64+
'is_collection' => true,
65+
'required' => false,
6866
];
67+
68+
if ($this->whitelist) {
69+
$description['schema'] = [
70+
'type' => 'array',
71+
'items' => [
72+
'type' => 'string',
73+
'enum' => $this->whitelist,
74+
],
75+
];
76+
}
77+
78+
return ["$this->parameterName[]" => $description];
6979
}
7080
}

tests/Serializer/Filter/GroupFilterTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,26 @@ public function testGetDescription(): void
125125

126126
$this->assertSame($expectedDescription, $groupFilter->getDescription(DummyGroup::class));
127127
}
128+
129+
public function testGetDescriptionWithWhitelist(): void
130+
{
131+
$groupFilter = new GroupFilter('custom_groups', false, ['default_group', 'another_default_group']);
132+
$expectedDescription = [
133+
'custom_groups[]' => [
134+
'property' => null,
135+
'type' => 'string',
136+
'is_collection' => true,
137+
'required' => false,
138+
'schema' => [
139+
'type' => 'array',
140+
'items' => [
141+
'type' => 'string',
142+
'enum' => ['default_group', 'another_default_group'],
143+
],
144+
],
145+
],
146+
];
147+
148+
$this->assertSame($expectedDescription, $groupFilter->getDescription(DummyGroup::class));
149+
}
128150
}

0 commit comments

Comments
 (0)