diff --git a/src/OpenApiRouteLoader.php b/src/OpenApiRouteLoader.php index d51de8e..8f057ef 100644 --- a/src/OpenApiRouteLoader.php +++ b/src/OpenApiRouteLoader.php @@ -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)); + } } } } diff --git a/tests/Attributes/Fixtures/PathParameterPattern/Controller.php b/tests/Attributes/Fixtures/PathParameterPattern/Controller.php index f62855c..f63fa1f 100644 --- a/tests/Attributes/Fixtures/PathParameterPattern/Controller.php +++ b/tests/Attributes/Fixtures/PathParameterPattern/Controller.php @@ -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 { } } diff --git a/tests/Attributes/OpenApiRouteLoaderAttributesTest.php b/tests/Attributes/OpenApiRouteLoaderAttributesTest.php index 5273feb..d79ab2e 100644 --- a/tests/Attributes/OpenApiRouteLoaderAttributesTest.php +++ b/tests/Attributes/OpenApiRouteLoaderAttributesTest.php @@ -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);