Skip to content

Commit

Permalink
https://github.com/Tobion/OpenAPI-Symfony-Routing/issues/12
Browse files Browse the repository at this point in the history
add path parameter enum

Also need to add tests for new features like OA\PathParameter and enums, see example zircote/swagger-php#1060
  • Loading branch information
sonrac committed Apr 20, 2022
1 parent de35b92 commit cbf3084
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
10 changes: 8 additions & 2 deletions src/OpenApiRouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,14 @@ private function createRoute(Operation $operation, string $controller, FormatSuf
}
if (self::$openApiUndefined !== $operation->parameters) {
foreach ($operation->parameters as $parameter) {
if ('path' === $parameter->in && self::$openApiUndefined !== $parameter->schema && self::$openApiUndefined !== $parameter->schema->pattern) {
$route->setRequirement($parameter->name, $parameter->schema->pattern);
if ('path' === $parameter->in && self::$openApiUndefined !== $parameter->schema) {
if (self::$openApiUndefined !== $parameter->schema->pattern) {
$route->setRequirement($parameter->name, $parameter->schema->pattern);
}

if (self::$openApiUndefined !== $parameter->schema->enum) {
$route->setRequirement($parameter->name, implode('|', $parameter->schema->enum));
}
}
}
}
Expand Down
26 changes: 14 additions & 12 deletions tests/Attributes/Fixtures/PathParameterPattern/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,36 @@

namespace Tobion\OpenApiSymfonyRouting\Tests\Attributes\Fixtures\PathParameterPattern;

use mysql_xdevapi\Schema;
use OpenApi\Attributes as OAT;

#[OAT\Info(title: "My API", version: "1.0")]
class Controller
{
#[OAT\Get(path: "/foo/{id}")]
#[OAT\Parameter(name:"id", in:"path", required:true, schema: new OAT\Schema(type:"string"))]
#[OAT\Response(response:"200", description:"Success")]
public function noPattern(): void
public function noPattern(
#[OAT\Parameter]string $id
): void
{
}

#[OAT\Get(path: "/baz/{id}")]
#[OAT\Parameter(name:"id", in:"path", required:true)]
#[OAT\Response(response:"200", description:"Success")]
public function noSchema(): void
public function noSchema(
#[OAT\Parameter]string $id
): void
{
}

#[OAT\Get(path: "/bar/{id}")]
#[OAT\Parameter(
name:"id",
in:"path",
required:true,
schema: new OAT\Schema(type:"string", pattern:"^[a-zA-Z0-9]+$")
)]
#[OAT\Get(path: "/bar/{id}/{type}")]
#[OAT\Response(response:"200", description:"Success")]
public function withPattern(): void
public function withPattern(
#[OAT\PathParameter(required: true, schema: new OAT\Schema(pattern:"^[a-zA-Z0-9]+$"))]
string $id,
#[OAT\PathParameter(required: true, schema: new OAT\Schema(enum: ["internal", "external"]))]
string $type
): void
{
}
}
6 changes: 5 additions & 1 deletion tests/Attributes/OpenApiRouteLoaderAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ public function testPathParameterPattern(): void
self::FIXTURES_ROUTE_NAME_PREFIX.'pathparameterpattern_noschema',
(new Route('/baz/{id}'))->setMethods('GET')->setDefault('_controller', PathParameterPatternController::class.'::noSchema')
);

// OpenAPI needs the param pattern to be anchored (^$) to have the desired effect. Symfony automatically trims those to get a valid full path regex.
$expectedRoutes->add(
self::FIXTURES_ROUTE_NAME_PREFIX.'pathparameterpattern_withpattern',
(new Route('/bar/{id}'))->setRequirement('id', '^[a-zA-Z0-9]+$')->setMethods('GET')->setDefault('_controller', PathParameterPatternController::class.'::withPattern')
(new Route('/bar/{id}/{type}'))
->setRequirement('id', '^[a-zA-Z0-9]+$')
->setRequirement('type', 'internal|external')
->setMethods('GET')->setDefault('_controller', PathParameterPatternController::class.'::withPattern')
);

self::assertEquals($expectedRoutes, $routes);
Expand Down

0 comments on commit cbf3084

Please sign in to comment.