Describe the bug
Using openapi-generator to generate clients from FastAPI projects results in redundant and repetitive function call names
To Reproduce
Steps to reproduce the behaviour:
Expected behaviour
The function names should not be repetitive and redundant, and should only be based on the action. For example, the DELETE method against /Claims/{claim_id} should follow the Python function name "delete_claim", and therefore show up as "deleteClaim" when converted to camelCase by openapi-generator.
Environment:
Recommended Fix
Generation of the "operationId" when creating the "openapi.json" file should be strictly the function name.
@christopherthompson81
I think having this as the default behavior is reasonable because it ensures that there are never conflicts in the names that don't stem from conflicts in routes, and that the defaults still include the function name. (I believe the default operation_id includes the full route path, the method, and the function name, which is why it looks so redundant.)
However, this is very easy to override manually. I use the following function to clean up the function names in my generated clients (and just take the responsibility on myself to avoid naming conflicts):
from fastapi.routing import APIRoute, FastAPI
def setup_openapi(app: FastAPI) -> None:
"""
Simplify operation IDs so that generated clients have simpler api function names.
Should be called on a FastAPI instance only after all routes have been added.
"""
for route in app.routes:
if isinstance(route, APIRoute):
route.operation_id = route.name
That does work. Thank-you! I will note that the necessary import is:
from fastapi.routing import APIRoute
and that the function to redefine the operation_id should be called after setting up the routes.
I also think this might be important information to include in the tutorial related to extending OpenAPI.
@christopherthompson81 updated in the example above. I think a PR adding a note about this to the docs would be well-received.
Thanks @dmontagu for the help!
@SKalt added it to the docs in #642
So I think this is solved, right?
Also, for what it's worth, I've added this to fastapi-utils in case you'd rather rely on someone else to maintain its implementation and not copy/paste it into each project.
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.
Most helpful comment
@christopherthompson81
I think having this as the default behavior is reasonable because it ensures that there are never conflicts in the names that don't stem from conflicts in routes, and that the defaults still include the function name. (I believe the default
operation_idincludes the full route path, the method, and the function name, which is why it looks so redundant.)However, this is very easy to override manually. I use the following function to clean up the function names in my generated clients (and just take the responsibility on myself to avoid naming conflicts):