Skip to content

Fix duplicate operationId for routes with multiple HTTP methods#15215

Open
fitratgulmamadov wants to merge 4 commits intofastapi:masterfrom
fitratgulmamadov:fix/issue-13175-duplicate-operation-id
Open

Fix duplicate operationId for routes with multiple HTTP methods#15215
fitratgulmamadov wants to merge 4 commits intofastapi:masterfrom
fitratgulmamadov:fix/issue-13175-duplicate-operation-id

Conversation

@fitratgulmamadov
Copy link
Copy Markdown

@fitratgulmamadov fitratgulmamadov commented Mar 24, 2026

Description

When a route is registered with multiple HTTP methods:

router.add_api_route("/items/", endpoint, methods=["POST", "DELETE"])

FastAPI generates the same operationId for all methods and emits a UserWarning: Duplicate Operation ID. This breaks OpenAPI clients and auto-generated SDKs.

Root cause: generate_unique_id() only takes the first method from the set (list(route.methods)[0]), so all methods on the same route share the same unique_id.

Fix: In get_openapi_operation_metadata() — which already receives the current method as a parameter — detect multi-method routes and build a per-method operationId using the same pattern as generate_unique_id.

Result:

POST /things/ → create_or_delete_things_things__post
DELETE /things/ → create_or_delete_things_things__delete
No duplicate warnings ✓
Fixes #13175

When a route is registered with multiple HTTP methods via
add_api_route(..., methods=["POST", "DELETE"]), generate_unique_id()
only uses the first method from the set, causing all methods to share
the same unique_id and producing duplicate operationIds in the OpenAPI
schema along with a UserWarning.

Fix by detecting multi-method routes in get_openapi_operation_metadata
and building a per-method operationId using the current method parameter
(already available in the function), following the same pattern as
generate_unique_id.

Fixes fastapi#13175
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 24, 2026

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing fitratgulmamadov:fix/issue-13175-duplicate-operation-id (4c1c2dd) with master (1f442c4)

Open in CodSpeed

@YuriiMotov
Copy link
Copy Markdown
Member

YuriiMotov commented Mar 25, 2026

@fitratgulmamadov, have you reviewed other PRs linked to that issue?
Please, describe why this approach is better

@fitratgulmamadov
Copy link
Copy Markdown
Author

@fitratgulmamadov, have you reviewed other PRs linked to that issue? Please, describe why this approach is better

Yes, I reviewed the linked PRs before submitting.

Why this approach differs from rejected PRs:

Known limitation: The body schema name (Body_do_something__put) is still generated at route compilation time and remains non-deterministic for multi-method routes. This would require a separate fix at the routing layer (as #10694 attempts). Our PR addresses only the operationId duplication.

Happy to adjust the approach based on your feedback.

@fitratgulmamadov
Copy link
Copy Markdown
Author

fitratgulmamadov commented Apr 6, 2026

Have it any chances to merge? @YuriiMotov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicated OperationID when adding route with multiple methods

2 participants