diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 8f1852b0cc..ed6ab0ef2f 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -1,6 +1,7 @@ import copy import http.client import inspect +import re import warnings from collections.abc import Sequence from typing import Any, Literal, cast @@ -242,7 +243,14 @@ def get_openapi_operation_metadata( operation["summary"] = generate_operation_summary(route=route, method=method) if route.description: operation["description"] = route.description - operation_id = route.operation_id or route.unique_id + if route.operation_id: + operation_id = route.operation_id + elif len(route.methods) > 1: + base_id = f"{route.name}{route.path_format}" + base_id = re.sub(r"\W", "_", base_id) + operation_id = f"{base_id}_{method.lower()}" + else: + operation_id = route.unique_id if operation_id in operation_ids: endpoint_name = getattr(route.endpoint, "__name__", "") message = f"Duplicate Operation ID {operation_id} for function {endpoint_name}"