🐛 Fix duplicate operationId for routes registered with multiple HTTP methods#15398
Open
Pedrom2002 wants to merge 2 commits intofastapi:masterfrom
Open
🐛 Fix duplicate operationId for routes registered with multiple HTTP methods#15398Pedrom2002 wants to merge 2 commits intofastapi:masterfrom
operationId for routes registered with multiple HTTP methods#15398Pedrom2002 wants to merge 2 commits intofastapi:masterfrom
Conversation
…P methods When a route is registered with multiple methods via `add_api_route(..., methods=["POST", "DELETE"])` or `@app.api_route(..., methods=[...])`, `route.unique_id` is generated once using an arbitrary single method as suffix. As a result, every OpenAPI operation produced from that route shared the same `operationId`, triggering the duplicate operation warning and breaking client code generators. Fix `get_openapi_operation_metadata()` to regenerate a per-method `operationId` when the route has more than one method and no explicit `operation_id` is set. The route's own `generate_unique_id_function` is reused on a shallow copy of the route scoped to the current method, so both the default function and user-provided ones produce unique ids. `route.unique_id` itself is left untouched, which keeps response/stream model names (derived from `unique_id`) stable. Closes fastapi#13175
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 methods (e.g.
add_api_route(..., methods=["POST", "DELETE"])or@app.api_route(..., methods=[...])),route.unique_idis generated once with an arbitrary single method as suffix. Every OpenAPI operation produced from that route ends up sharing the sameoperationId, triggering the "Duplicate Operation ID" warning and breaking client code generators (openapi-generator, etc.).Fix
get_openapi_operation_metadata()now regenerates a per-methodoperationIdwhen the route has more than one method and no explicitoperation_idis set. It reuses the route's owngenerate_unique_id_functionon a shallow copy of the route scoped to the current method, so both the default function and user-provided ones produce unique ids.route.unique_iditself is left untouched, which keeps response / stream model names (derived fromunique_id) stable.Tests
Added
tests/test_multiple_methods_operation_id.pywith 9 tests:add_api_routewith multiple methods@api_routedecorator with multiple methodsoperation_idstill respected (and still warns on dup)generate_unique_id_functionhonoured per methodinclude_routerpath/openapi.jsongeneration end-to-endFull suite:
3130 passed, 32 skipped, 5 xfailed.Closes #13175.
Related: #10694, #15215.