Skip to content

🐛 Fix duplicate operationId for routes registered with multiple HTTP methods#15398

Open
Pedrom2002 wants to merge 2 commits intofastapi:masterfrom
Pedrom2002:fix/duplicate-operation-id-multiple-methods
Open

🐛 Fix duplicate operationId for routes registered with multiple HTTP methods#15398
Pedrom2002 wants to merge 2 commits intofastapi:masterfrom
Pedrom2002:fix/duplicate-operation-id-multiple-methods

Conversation

@Pedrom2002
Copy link
Copy Markdown

Description

When a route is registered with multiple methods (e.g. add_api_route(..., methods=["POST", "DELETE"]) or @app.api_route(..., methods=[...])), route.unique_id is generated once with an arbitrary single method as suffix. Every OpenAPI operation produced from that route ends up sharing the same operationId, triggering the "Duplicate Operation ID" warning and breaking client code generators (openapi-generator, etc.).

Fix

get_openapi_operation_metadata() now regenerates a per-method operationId when the route has more than one method and no explicit operation_id is set. It reuses the route's own generate_unique_id_function 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.

Tests

Added tests/test_multiple_methods_operation_id.py with 9 tests:

  • add_api_route with multiple methods
  • @api_route decorator with multiple methods
  • Explicit operation_id still respected (and still warns on dup)
  • Custom generate_unique_id_function honoured per method
  • Single-method route unchanged (regression guard)
  • include_router path
  • /openapi.json generation end-to-end
  • Parametrized across 2 and 5 method combos

Full suite: 3130 passed, 32 skipped, 5 xfailed.

Closes #13175.
Related: #10694, #15215.

…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
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 21, 2026

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing Pedrom2002:fix/duplicate-operation-id-multiple-methods (c18d86a) with master (13eade2)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (88021c3) during the generation of this report, so 13eade2 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

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

1 participant