diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 6ed0d22b8..910a56d55 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -246,11 +246,7 @@ def get_openapi_operation_metadata( operation_id = route.operation_id or route.unique_id # If the route has multiple methods and operation_id was not explicitly set, # append the method to make each operation ID unique - if ( - not route.operation_id - and route.methods - and len(route.methods) > 1 - ): + if not route.operation_id and route.methods and len(route.methods) > 1: # unique_id already includes a method suffix, so we need to replace it # with the actual method being processed # unique_id format: "{name}_{path}_{first_method}" diff --git a/tests/test_duplicate_operation_id.py b/tests/test_duplicate_operation_id.py index 1a984a771..23eecc19e 100644 --- a/tests/test_duplicate_operation_id.py +++ b/tests/test_duplicate_operation_id.py @@ -2,10 +2,10 @@ Test that routes with multiple methods get unique operation IDs. Related to issue #13175 """ + import warnings from fastapi import FastAPI -from fastapi.testclient import TestClient def test_multiple_methods_unique_operation_ids(): @@ -25,8 +25,7 @@ def test_multiple_methods_unique_operation_ids(): # Check that no duplicate operation ID warning was raised duplicate_warnings = [ - warning for warning in w - if "Duplicate Operation ID" in str(warning.message) + warning for warning in w if "Duplicate Operation ID" in str(warning.message) ] assert len(duplicate_warnings) == 0, ( f"Found {len(duplicate_warnings)} duplicate operation ID warnings: " @@ -46,8 +45,7 @@ def test_multiple_methods_unique_operation_ids(): delete_op_id = clear_path["delete"]["operationId"] assert post_op_id != delete_op_id, ( - f"Operation IDs should be different: " - f"POST={post_op_id}, DELETE={delete_op_id}" + f"Operation IDs should be different: POST={post_op_id}, DELETE={delete_op_id}" ) @@ -68,8 +66,7 @@ def test_multiple_routes_with_multiple_methods(): openapi_schema = app.openapi() duplicate_warnings = [ - warning for warning in w - if "Duplicate Operation ID" in str(warning.message) + warning for warning in w if "Duplicate Operation ID" in str(warning.message) ] assert len(duplicate_warnings) == 0 @@ -113,8 +110,7 @@ def test_add_api_route_with_multiple_methods(): openapi_schema = app.openapi() duplicate_warnings = [ - warning for warning in w - if "Duplicate Operation ID" in str(warning.message) + warning for warning in w if "Duplicate Operation ID" in str(warning.message) ] assert len(duplicate_warnings) == 0