-
-
Notifications
You must be signed in to change notification settings - Fork 6k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicated OperationID when adding route with multiple methods #4740
Comments
I am getting a similar problem. Am getting the following errors.
Upon further inspection of the code with the following lines: flat_models = get_flat_models_from_routes(routes)
model_name_map = get_model_name_map(flat_models)
definitions = get_model_definitions(
flat_models=flat_models, model_name_map=model_name_map
) The problem is that basically that in my case, the variables passed into So the problem in my case is that Once I remove code with my auth endpoints, openapi schema gets generated and docs do not crash, but I get many instances of the following errors.
My repo with the following problem is referenced above. |
Nevermind, found a solution. For future reference, the commit above fixed the issue. I was transferring some code from another project. Sorry if maybe not exactly relevant to the issue, but hope it helps someone in the future. |
@bruchar1 this solution above solve yout problem? |
No. @mbrav problem is unrelated to this. |
@mbrav I believe this issue can be closed then :) |
@jonatasoli I agree, but I am not the one who opened it :) |
@jonatasoli Why do you want to close this? The bug is still there, and completely unrelated to @mbrav issue. |
There are multiple issues here:
I'll create a quick MR to change the btw my current workaround is just splitting up the routes to different functions, so using |
The methods attribute accessed by the `generate_unique_id` function is modeled as a python set, which is not ordered and between runs might change. This results in the same fastapi generating different openapi specs if started multiple times. This also relates to the bigger issue tiangolo#4740 but is not a solution for that issue.
The methods attribute accessed by the `generate_unique_id` function is modeled as a python set, which is not ordered and between runs might change. This results in the same fastapi generating different openapi specs if started multiple times. This also relates to the bigger issue tiangolo#4740 but is not a solution for that issue.
Hello @bneijt, I confirm I hit the same issue with fastapi@0.79.1. I've decorated a method like this: @router.api_route(
"/example",
methods=["POST", "PUT"],
status_code=status.HTTP_201_CREATED,
)
async def save_example(...)
pass |
health_system.pyrouter = APIRouter(prefix="/system", route_class=ContextLogerRoute) @router.get('/health', tags=['默认开启的健康检查']) ================ =================== How to solve this problem? |
@462548187 I suppose the warning comes from the /system route. You need to specify a particular method (a.k.a HTTP request method type). It has to work with the warning though. |
Similar issue: UserWarning: Duplicate Operation IDI've been reducing my example until all I have is this: Freezefastapi==0.88.0
|
@ricardo-reis-1970 do you get these warnings only when running tests or also when running the app itself? Because for me it's only when running pytest. I haven't session-scoped my |
Just a thought: Can you or anyone facing the issue, confirm if you have included the router (with all routes) only once in the parent router or the fastapi app? |
@KRC1997 Yeah you're right (in a way). I only add the router once, but the problem occurred when I started using asgi-lifespan to include the startup- and shutdown routines (the app lifecycle) of my app when running tests. I had my The following workaround fixed the problem for me and also fixed another issue I had anyway: I now still initialize the database client in the startup routine (and close it in the shutdown routine) but everything that belongs to initializing the actual app instance (e.g. registering routers), I moved to a def pre_startup_routine(app: FastAPI) -> None:
# add middlewares
...
# register routers
...
async def startup_routine() -> None:
# init database client
...
async def shutdown_routine() -> None:
# close database client and cleanup
...
# create FastAPI app instance
app = FastAPI(
# ...
on_startup=[startup_routine],
on_shutdown=[shutdown_routine],
)
pre_startup_routine(app) This has the additional advantage that it works well with my test setup. |
@bkis yeah, i do believe it has something to do with the lifecycle of the |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First Check
Commit to Help
Example Code
Description
Seems to be caused by #4650.
The new
generate_unique_id()
function useslist(route.methods)[0].lower()
as suffix for theoperation_id
. Therefore, in my example, both post and delete endpoints get_post
suffix for operation_id, causing it to no longer be unique.It then issues a "UserWarning: Duplicate Operation ID"
Operating System
Windows
Operating System Details
No response
FastAPI Version
0.75.0
Python Version
3.10.2
Additional Context
No response
The text was updated successfully, but these errors were encountered: