Browse Source

🐛 Fix duplicate operationId when using multiple methods on same route

When a route is added with multiple HTTP methods (e.g., methods=["POST", "DELETE"]),
all methods were getting the same auto-generated operationId based only on the
first method, causing "Duplicate Operation ID" warnings.

The fix appends the method name to the operationId when:
1. No explicit operation_id is provided
2. The route has multiple methods

This ensures unique operationIds for each method while maintaining backward
compatibility for single-method routes and explicit operation_id usage.

Fixes #13175
pull/14827/head
Contributor 5 months ago
parent
commit
7856976026
  1. 4
      fastapi/openapi/utils.py

4
fastapi/openapi/utils.py

@ -243,6 +243,10 @@ def get_openapi_operation_metadata(
if route.description: if route.description:
operation["description"] = route.description operation["description"] = route.description
operation_id = route.operation_id or route.unique_id operation_id = route.operation_id or route.unique_id
# When a route has multiple methods and uses auto-generated unique_id,
# append the method to make the operation_id unique for each method
if not route.operation_id and len(route.methods) > 1:
operation_id = f"{operation_id}_{method.lower()}"
if operation_id in operation_ids: if operation_id in operation_ids:
message = ( message = (
f"Duplicate Operation ID {operation_id} for function " f"Duplicate Operation ID {operation_id} for function "

Loading…
Cancel
Save