diff --git a/samtranslator/swagger/swagger.py b/samtranslator/swagger/swagger.py index 53ac00bee2..7c1f7877e9 100644 --- a/samtranslator/swagger/swagger.py +++ b/samtranslator/swagger/swagger.py @@ -531,7 +531,17 @@ def set_path_default_authorizer( if add_default_auth_to_preflight or normalized_method_name != "options": normalized_method_name = self._normalize_method_name(method_name) # It is possible that the method could have two definitions in a Fn::If block. - for method_definition in self.get_method_contents(self.get_path(path)[normalized_method_name]): + method_dict = self.get_path(path).get(normalized_method_name) + if method_dict is None: + raise InvalidDocumentException( + [ + InvalidTemplateException( + "Can't find method definition dictionary for method '{}'".format(normalized_method_name) + ) + ] + ) + + for method_definition in self.get_method_contents(method_dict): # If no integration given, then we don't need to process this definition (could be AWS::NoValue) if not isinstance(method_definition, dict): diff --git a/tests/translator/input/error_api_invalid_any_method.yaml b/tests/translator/input/error_api_invalid_any_method.yaml new file mode 100644 index 0000000000..bd6b304744 --- /dev/null +++ b/tests/translator/input/error_api_invalid_any_method.yaml @@ -0,0 +1,44 @@ +Resources: + HttpApiFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: s3://sam-demo-bucket/todo_list.zip + Handler: index.restapi + Runtime: python3.7 + Events: + SimpleCase: + Type: HttpApi + Properties: + ApiId: !Ref MyApi + BasePath: + Type: HttpApi + Properties: + ApiId: !Ref MyApi + Path: / + Method: get + + MyApi: + Type: AWS::Serverless::Api + Properties: + StageName: + Ref: Stage + Auth: + DefaultAuthorizer: "LambdaAuthorizer" + Authorizers: + LambdaAuthorizer: + FunctionPayloadType: "REQUEST" + Identity: + Headers: + - "Authorization" + DefinitionBody: + openapi: '3.0' + info: + title: !Sub ${AWS::StackName}-Api + paths: + /: + any: + x-amazon-apigateway-integration: + httpMethod: ANY + type: http_proxy + uri: https://www.alphavantage.co/ + payloadFormatVersion: '1.0' \ No newline at end of file diff --git a/tests/translator/output/error_api_invalid_any_method.json b/tests/translator/output/error_api_invalid_any_method.json new file mode 100644 index 0000000000..c17791ac68 --- /dev/null +++ b/tests/translator/output/error_api_invalid_any_method.json @@ -0,0 +1,3 @@ +{ + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. Can't find method definition dictionary for method 'x-amazon-apigateway-any-method'" +} \ No newline at end of file diff --git a/tests/translator/test_translator.py b/tests/translator/test_translator.py index fc89f799d3..a0e5be2f1c 100644 --- a/tests/translator/test_translator.py +++ b/tests/translator/test_translator.py @@ -667,6 +667,7 @@ def test_transform_success_no_side_effect(self, testcase, partition_with_region) "error_api_gateway_responses_nonnumeric_status_code", "error_api_gateway_responses_unknown_responseparameter", "error_api_gateway_responses_unknown_responseparameter_property", + "error_api_invalid_any_method", "error_api_invalid_auth", "error_api_invalid_path", "error_api_invalid_definitionuri",