Browse Source

fix: preserve backwards compatibility in generate_unique_id for route creation

pull/15327/head
Contributor 3 months ago
parent
commit
21f00fad88
  1. 10
      fastapi/openapi/utils.py
  2. 11
      fastapi/utils.py

10
fastapi/openapi/utils.py

@ -33,7 +33,6 @@ from fastapi.types import ModelNameMap
from fastapi.utils import (
deep_dict_update,
generate_operation_id_for_path,
get_openapi_operation_id,
is_body_allowed_for_status_code,
)
from pydantic import BaseModel
@ -243,7 +242,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 get_openapi_operation_id(route, method)
# Start with explicit operation_id or unique_id
operation_id = route.operation_id or route.unique_id
# For multi-method routes, append the method if the unique_id alone would be a duplicate
if operation_id in operation_ids:
operation_id = f"{route.unique_id}_{method.lower()}"
if operation_id in operation_ids:
endpoint_name = getattr(route.endpoint, "__name__", "<unnamed_endpoint>")
message = f"Duplicate Operation ID {operation_id} for function {endpoint_name}"

11
fastapi/utils.py

@ -102,17 +102,6 @@ def generate_unique_id(route: APIRoute) -> str:
return operation_id
def get_openapi_operation_id(route: APIRoute, method: str) -> str:
"""
Generate a unique operation ID for a specific HTTP method in the OpenAPI schema.
This ensures each HTTP method on a multi-method route gets a unique operation ID.
"""
operation_id = f"{route.name}{route.path_format}"
operation_id = re.sub(r"\W", "_", operation_id)
operation_id = f"{operation_id}_{method.lower()}"
return operation_id
def deep_dict_update(main_dict: dict[Any, Any], update_dict: dict[Any, Any]) -> None:
for key, value in update_dict.items():
if (

Loading…
Cancel
Save