From a3a6f2f5ee3db5609738b03fb6fcf32f40f50833 Mon Sep 17 00:00:00 2001 From: "S. M. Mohiuddin Khan Shiam" Date: Thu, 3 Jul 2025 16:46:24 +0600 Subject: [PATCH] Update utils.py --- fastapi/utils.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/fastapi/utils.py b/fastapi/utils.py index 4c7350fea..9f1a6b451 100644 --- a/fastapi/utils.py +++ b/fastapi/utils.py @@ -179,8 +179,15 @@ def generate_operation_id_for_path( def generate_unique_id(route: "APIRoute") -> str: operation_id = f"{route.name}{route.path_format}" operation_id = re.sub(r"\W", "_", operation_id) + # Ensure deterministic generation of the operation_id + # Sets are unordered, so converting directly to a list could give + # different results between runs, causing unstable OpenAPI specs. + # Sort the HTTP methods to guarantee a consistent order and pick the first + # one. This keeps the original behaviour (choosing one method) while + # making the result predictable. assert route.methods - operation_id = f"{operation_id}_{list(route.methods)[0].lower()}" + method = sorted(route.methods)[0].lower() + operation_id = f"{operation_id}_{method}" return operation_id