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 cca2161
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/RouteDescriber/FosRestDescriber.php
Original file line number Diff line number Diff line change
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
27 changes: 27 additions & 0 deletions tests/RouteDescriber/FosRestDescriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,31 @@ public function testQueryParamWithChoiceConstraintCallbackIsAddedAsEnum()

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

Check failure on line 74 in tests/RouteDescriber/FosRestDescriberTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Dynamic call to static method PHPUnit\Framework\Assert::assertSame().
}

public function testQueryParamWithChoiceConstraintAsArray()

Check failure on line 77 in tests/RouteDescriber/FosRestDescriberTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Nelmio\ApiDocBundle\Tests\RouteDescriber\FosRestDescriberTest::testQueryParamWithChoiceConstraintAsArray() has no return type specified.
{
$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)
);

$this->assertEquals('array', $api->paths[0]->get->parameters[0]->schema->type);

Check failure on line 100 in tests/RouteDescriber/FosRestDescriberTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Dynamic call to static method PHPUnit\Framework\Assert::assertEquals().
$this->assertSame($choices, $api->paths[0]->get->parameters[0]->schema->items->enum);

Check failure on line 101 in tests/RouteDescriber/FosRestDescriberTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Dynamic call to static method PHPUnit\Framework\Assert::assertSame().
}
}

0 comments on commit cca2161

Please sign in to comment.