diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 4a9428f7b3..298c04c7fe 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -245,10 +245,8 @@ def get_openapi_operation_metadata( operation["description"] = route.description operation_id = route.operation_id or route.unique_id if operation_id in operation_ids: - message = ( - f"Duplicate Operation ID {operation_id} for function " - + f"{route.endpoint.__name__}" # ty: ignore[unresolved-attribute] - ) + endpoint_name = getattr(route.endpoint, "__name__", "") + message = f"Duplicate Operation ID {operation_id} for function {endpoint_name}" file_name = getattr(route.endpoint, "__globals__", {}).get("__file__") if file_name: message += f" at {file_name}"