From 36bc8e54b37a6fedc6a4903469848acd575bf5f1 Mon Sep 17 00:00:00 2001 From: Grant Jenks Date: Tue, 20 Jan 2026 14:38:20 -0800 Subject: [PATCH] Use sorted() rather than list() for consistent operation_id naming The current code does list(methods)[0] where methods is a set and so picks one based on hash order (unreliable). The fix uses sorted(methods)[0] to consistently pick the first lexicographically. --- fastapi/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi/utils.py b/fastapi/utils.py index 78fdcbb5b..425e95b75 100644 --- a/fastapi/utils.py +++ b/fastapi/utils.py @@ -124,7 +124,7 @@ def generate_unique_id(route: "APIRoute") -> str: operation_id = f"{route.name}{route.path_format}" operation_id = re.sub(r"\W", "_", operation_id) assert route.methods - operation_id = f"{operation_id}_{list(route.methods)[0].lower()}" + operation_id = f"{operation_id}_{sorted(route.methods)[0].lower()}" return operation_id