Skip to content

Commit fb4d963

Browse files
authored
fix: Exclude Non-Method Path Sections (#2171)
* Added Exclusion for Non-Method Path Sections * Added Unit Tests
1 parent 11f9fef commit fb4d963

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

samtranslator/swagger/swagger.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class SwaggerEditor(object):
2828
_CACHE_KEY_PARAMETERS = "cacheKeyParameters"
2929
# https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
3030
_ALL_HTTP_METHODS = ["OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"]
31+
_EXCLUDED_PATHS_FIELDS = ["summary", "description", "parameters"]
3132
_POLICY_TYPE_IAM = "Iam"
3233
_POLICY_TYPE_IP = "Ip"
3334
_POLICY_TYPE_VPC = "Vpc"
@@ -527,8 +528,8 @@ def set_path_default_authorizer(
527528
for method_name, method in self.get_path(path).items():
528529
normalized_method_name = self._normalize_method_name(method_name)
529530

530-
# Excluding parameters section
531-
if normalized_method_name == "parameters":
531+
# Excluding non-method sections
532+
if normalized_method_name in SwaggerEditor._EXCLUDED_PATHS_FIELDS:
532533
continue
533534
if add_default_auth_to_preflight or normalized_method_name != "options":
534535
normalized_method_name = self._normalize_method_name(method_name)
@@ -623,8 +624,8 @@ def set_path_default_apikey_required(self, path):
623624
"""
624625

625626
for method_name, method in self.get_path(path).items():
626-
# Excluding parameters section
627-
if method_name == "parameters":
627+
# Excluding non-method sections
628+
if method_name in SwaggerEditor._EXCLUDED_PATHS_FIELDS:
628629
continue
629630

630631
# It is possible that the method could have two definitions in a Fn::If block.

tests/swagger/test_swagger.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,14 @@ def setUp(self):
949949
self.original_swagger = {
950950
"swagger": "2.0",
951951
"paths": {
952-
"/foo": {"get": {_X_INTEGRATION: {"a": "b"}}, "post": {_X_INTEGRATION: {"a": "b"}}},
953-
"/bar": {"get": {_X_INTEGRATION: {"a": "b"}}},
952+
"/foo": {
953+
"summary": "a",
954+
"description": "b",
955+
"parameters": {"a": "b"},
956+
"get": {_X_INTEGRATION: {"a": "b"}},
957+
"post": {_X_INTEGRATION: {"a": "b"}},
958+
},
959+
"/bar": {"summary": "a", "description": "b", "get": {_X_INTEGRATION: {"a": "b"}}},
954960
},
955961
}
956962

@@ -971,6 +977,8 @@ def test_must_add_default_apikey_to_all_paths(self):
971977
self.editor.set_path_default_apikey_required(path)
972978
methods = self.editor.swagger["paths"][path]
973979
for method in methods:
980+
if method in ["summary", "parameters", "description"]:
981+
continue
974982
self.assertEqual(expected, methods[method]["security"])
975983

976984
def test_add_default_apikey_to_all_paths_correctly_handles_method_level_settings(self):

0 commit comments

Comments
 (0)