Skip to content

Commit 6b3afda

Browse files
committed
Fix duplicate OPTIONS method error for paths with trailing slash
Address review feedback: use normalized_path instead of path when adding CORS OPTIONS method. This ensures consistent behavior regardless of which path (/datasets vs /datasets/) appears first in the template. Fixes #3816
1 parent b70d4e5 commit 6b3afda

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

samtranslator/model/api/api_generator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -983,20 +983,20 @@ def _add_cors(self) -> None:
983983
# Track normalized paths to avoid duplicate OPTIONS methods for paths that differ only by trailing slash
984984
# API Gateway treats /path and /path/ as the same resource, so we normalize before adding CORS
985985
normalized_paths_processed: Set[str] = set()
986-
986+
987987
for path in editor.iter_on_path():
988988
# Normalize path by removing trailing slash (except for root path "/")
989989
normalized_path = path.rstrip("/") if path != "/" else path
990-
990+
991991
# Skip if we've already processed this normalized path to avoid duplicate OPTIONS methods
992992
if normalized_path in normalized_paths_processed:
993993
continue
994-
994+
995995
normalized_paths_processed.add(normalized_path)
996-
996+
997997
try:
998998
editor.add_cors( # type: ignore[no-untyped-call]
999-
path,
999+
normalized_path,
10001000
properties.AllowOrigin,
10011001
properties.AllowHeaders,
10021002
properties.AllowMethods,

tests/model/api/test_api_generator.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_add_cors_with_trailing_slash_paths(self):
6262
"x-amazon-apigateway-integration": {
6363
"type": "aws_proxy",
6464
"httpMethod": "POST",
65-
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/func/invocations"
65+
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/func/invocations",
6666
}
6767
}
6868
},
@@ -71,13 +71,13 @@ def test_add_cors_with_trailing_slash_paths(self):
7171
"x-amazon-apigateway-integration": {
7272
"type": "aws_proxy",
7373
"httpMethod": "POST",
74-
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/func/invocations"
74+
"uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/func/invocations",
7575
}
7676
}
77-
}
78-
}
77+
},
78+
},
7979
}
80-
80+
8181
api_generator = ApiGenerator(
8282
logical_id="TestApi",
8383
cache_cluster_enabled=None,
@@ -112,19 +112,20 @@ def test_add_cors_with_trailing_slash_paths(self):
112112
api_key_source_type=None,
113113
disable_execute_api_endpoint=None,
114114
)
115-
115+
116116
# Call _add_cors which should normalize paths and avoid duplicates
117117
api_generator._add_cors()
118-
118+
119119
# Check that OPTIONS method is not added to both /datasets and /datasets/
120120
# It should only be added once to avoid the duplicate OPTIONS error
121121
paths_with_options = [
122-
path for path, methods in api_generator.definition_body["paths"].items()
122+
path
123+
for path, methods in api_generator.definition_body["paths"].items()
123124
if "options" in methods or "OPTIONS" in methods
124125
]
125-
126+
126127
# We should have only ONE path with OPTIONS method (the normalized one)
127128
# Both /datasets and /datasets/ normalize to /datasets
128-
self.assertEqual(len(paths_with_options), 1,
129-
"CORS should only add OPTIONS to one of the paths that differ by trailing slash")
130-
129+
self.assertEqual(
130+
len(paths_with_options), 1, "CORS should only add OPTIONS to one of the paths that differ by trailing slash"
131+
)

0 commit comments

Comments
 (0)