Skip to content

[BUG][python-fastapi] Reordering of path operations can cause issues when path parameters are involved #22163

@zhornback

Description

@zhornback
Description

The OpenAPI Generator reorders path operations while generating server code. According to https://fastapi.tiangolo.com/tutorial/path-params/?h=path#order-matters, the order of path declaration is important. This can lead to FastAPI incorrectly routing a call to one endpoint to a similar endpoint containing a path parameter.

openapi-generator version

Initially found with v7.12.0, but reproduced with v7.16.0 as well.

OpenAPI declaration file content or url

https://gist.github.com/zhornback/dfd8b93c90907c403bef3af6a5b38c67

Steps to reproduce
  • Download yaml file linked above.
  • Get most recent openapi-generator-cli.jar
  • Generate server using 'java -jar ./openapi-generator-cli.jar generate -i ./petstore/petstore.yaml -g python-fastapi -o ./petstore/python-fastapi'.
  • Open petstore/python-fastapi/src/openapi_server/apis/pet_api.py.
  • Notice that GET /pet/{petID} is declared prior to GET /pet/latest. If you ran the server, you'd find calls to GET /pet/latest will fail, because 'latest' isn't a valid petID.
Related issues/PRs

#19550 seems like a similar issue for another generator.

Suggest a fix

The configuration for FastAPI should set setSkipSortingOperations to True. I will put together a PR to that effect as soon as I can.

As a short-term workaround, the generator is alphabetically sorting based upon path operationIDs. If you appropriately name your path operationIDs, you can control how the paths are sorted and avoid the issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions