swagger-api / swagger-api/swagger-codegen
[PHP][Slim] Server fails because static routes match already defined routes with placeholders
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
The PHP Slim server stub produces its route definitions in an order that causes the application fail to run (including with the Petstore demo). It fails because a route with a placeholder (like /user/{username}) might be defined in the source BEFORE a static route (like /user/login) that matches the pattern of the placeholder route. The routing engine throws an exception in this case.
To use pet store as an example, $app->GET('/v2/user/{username}' is output in index.php BEFORE $app->GET('/v2/user/login' and $app->GET('/v2/user/logout'. The latter two routes can never be reached, as they match the route pattern placeholder of the /user/{username} route.
This results in an exception being thrown when attempting to use the server:
Slim Application Error:
Type: FastRoute\BadRouteException
Message: Static route "/v2/user/login" is shadowed by previously defined variable route "/v2/user/([^/]+)" for method "GET"
File: /home/vagrant/PetStoreSlimServer/vendor/nikic/fast-route/src/DataGenerator/RegexBasedAbstract.php
Line: 95
...FULL TRACE CLIPPED FOR BREVITY...
The router component (FastRoute) produces this error whenever a static route matches a previously defined placeholder route's regex. For reference, this happens in the FastRoute source here.
Swagger-codegen version
2.4
Command line used for generation
java -jar swagger-codegen-cli.jar generate -i ~/petstore.yaml -l slim -o ~/PetStoreSlimServer
Steps to reproduce
- Generate Slim server using above command
- If not already installed, install Composer (https://getcomposer.org)
- Install dependencies:
cd ~/PetStoreSlimServer && composer install - Serve the application:
php -S localhost:8000 - Attempt to access any route
- The result is a
FastRoute\BadRouteException(detailed above)
Related issues/PRs
N/A
Suggest a fix/enhancement
The simple workaround for a small API spec is to manually reorder the routes so that the static routes are defined before conflicting routes with placeholders.
A naive, but helpful, solution in the generator might be to sort the routes such that routes with parameters: ... in: "path" would be output in index.php AFTER the routes that don't have parameters.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/swagger-codegen/src/main/resources/slim/index.mustache and reproduce the failure using the Petstore generation command and route steps in the issue. Trace how route definitions reach that template, then verify that static routes are emitted before conflicting placeholder routes. Done means the generated Slim server starts without FastRoute\BadRouteException and the static login/logout routes remain reachable.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100