Fix duplicate operationId for routes with multiple HTTP methods#15215
Open
fitratgulmamadov wants to merge 4 commits intofastapi:masterfrom
Open
Fix duplicate operationId for routes with multiple HTTP methods#15215fitratgulmamadov wants to merge 4 commits intofastapi:masterfrom
fitratgulmamadov wants to merge 4 commits intofastapi:masterfrom
Conversation
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
Member
|
@fitratgulmamadov, have you reviewed other PRs linked to that issue? |
Author
Yes, I reviewed the linked PRs before submitting. Why this approach differs from rejected PRs:
Known limitation: The body schema name ( Happy to adjust the approach based on your feedback. |
Author
|
Have it any chances to merge? @YuriiMotov |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When a route is registered with multiple HTTP methods:
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