Skip to content

Commit

Permalink
Add arrays of enums in QueryParam using the multiple option
Browse files Browse the repository at this point in the history
  • Loading branch information
indexxd committed Jun 4, 2024
1 parent c1a9594 commit 655e143
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/RouteDescriber/FosRestDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function getFormat($requirements): ?string
private function getEnum($requirements): ?array
{
if ($requirements instanceof Choice) {
if ($requirements->callback) {
if (null != $requirements->callback) {
if (!\is_callable($choices = $requirements->callback)) {
return null;
}
Expand Down Expand Up @@ -225,7 +225,14 @@ private function describeCommonSchemaFromAnnotation(OA\Schema $schema, AbstractS

$enum = $this->getEnum($annotation->requirements);
if (null !== $enum) {
$schema->enum = $enum;
if ($annotation->requirements instanceof Choice) {
if ($annotation->requirements->multiple) {
$schema->type = 'array';
$schema->items = Util::createChild($schema, OA\Items::class, ['type' => 'string', 'enum' => $enum]);
} else {
$schema->enum = $enum;
}
}
}
}

Expand Down
31 changes: 29 additions & 2 deletions tests/RouteDescriber/FosRestDescriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testQueryParamWithChoiceConstraintIsAddedAsEnum(): void
self::assertSame($choices, $api->paths[0]->get->parameters[0]->schema->enum);
}

public function testQueryParamWithChoiceConstraintCallbackIsAddedAsEnum()
public function testQueryParamWithChoiceConstraintCallbackIsAddedAsEnum(): void
{
$queryParam = new QueryParam();
$choice = new Choice();
Expand All @@ -71,6 +71,33 @@ public function testQueryParamWithChoiceConstraintCallbackIsAddedAsEnum()
$this->createMock(\ReflectionMethod::class)
);

$this->assertSame(['foo', 'bar'], $api->paths[0]->get->parameters[0]->schema->enum);
self::assertSame(['foo', 'bar'], $api->paths[0]->get->parameters[0]->schema->enum);
}

public function testQueryParamWithChoiceConstraintAsArray(): void
{
$choices = ['foo', 'bar'];

$queryParam = new QueryParam();
$choice = new Choice($choices);
$choice->multiple = true;
$queryParam->requirements = $choice;

$readerMock = $this->createMock(Reader::class);
$readerMock->method('getMethodAnnotations')->willReturn([
$queryParam,
]);

$fosRestDescriber = new FosRestDescriber($readerMock, []);
$api = new OpenApi([]);

$fosRestDescriber->describe(
$api,
new Route('/'),
$this->createMock(\ReflectionMethod::class)
);

self::assertEquals('array', $api->paths[0]->get->parameters[0]->schema->type);
self::assertSame($choices, $api->paths[0]->get->parameters[0]->schema->items->enum);
}
}

0 comments on commit 655e143

Please sign in to comment.