diff --git a/samtranslator/plugins/api/implicit_http_api_plugin.py b/samtranslator/plugins/api/implicit_http_api_plugin.py index 3f8849dbc..83c078ea1 100644 --- a/samtranslator/plugins/api/implicit_http_api_plugin.py +++ b/samtranslator/plugins/api/implicit_http_api_plugin.py @@ -57,6 +57,13 @@ def _process_api_events(self, function, api_events, template, condition=None): for logicalId, event in api_events.items(): # api_events only contains HttpApi events event_properties = event.get("Properties", {}) + + if event_properties and not isinstance(event_properties, dict): + raise InvalidEventException( + logicalId, + "Event 'Properties' must be an Object. If you're using YAML, this may be an indentation issue.", + ) + if not event_properties: event["Properties"] = event_properties self._add_implicit_api_id_if_necessary(event_properties) diff --git a/samtranslator/plugins/api/implicit_rest_api_plugin.py b/samtranslator/plugins/api/implicit_rest_api_plugin.py index 3a6461928..92ecf8d86 100644 --- a/samtranslator/plugins/api/implicit_rest_api_plugin.py +++ b/samtranslator/plugins/api/implicit_rest_api_plugin.py @@ -63,6 +63,12 @@ def _process_api_events(self, function, api_events, template, condition=None): if not event_properties: continue + if not isinstance(event_properties, dict): + raise InvalidEventException( + logicalId, + "Event 'Properties' must be an Object. If you're using YAML, this may be an indentation issue.", + ) + self._add_implicit_api_id_if_necessary(event_properties) api_id = self._get_api_id(event_properties) diff --git a/tests/translator/input/error_function_invalid_api_event.yaml b/tests/translator/input/error_function_invalid_api_event.yaml index 98b7aa5f0..03a98c2a0 100644 --- a/tests/translator/input/error_function_invalid_api_event.yaml +++ b/tests/translator/input/error_function_invalid_api_event.yaml @@ -1,4 +1,15 @@ Resources: + FunctionApiInvalidProperties: + Type: 'AWS::Serverless::Function' + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + Events: + ApiEvent: + Type: Api + Properties: '/hello' # NOTE: Should be an object no a string + FunctionApiNoMethod: Type: 'AWS::Serverless::Function' Properties: diff --git a/tests/translator/input/error_implicit_http_api_properties.yaml b/tests/translator/input/error_implicit_http_api_properties.yaml new file mode 100644 index 000000000..33770f00d --- /dev/null +++ b/tests/translator/input/error_implicit_http_api_properties.yaml @@ -0,0 +1,12 @@ +Resources: + HttpApiFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: s3://sam-demo-bucket/todo_list.zip + Handler: index.restapi + Runtime: nodejs12.x + Policies: AmazonDynamoDBFullAccess + Events: + Basic: + Type: HttpApi + Properties: /basic diff --git a/tests/translator/output/error_function_invalid_api_event.json b/tests/translator/output/error_function_invalid_api_event.json index 01b123ed8..48027216a 100644 --- a/tests/translator/output/error_function_invalid_api_event.json +++ b/tests/translator/output/error_function_invalid_api_event.json @@ -4,5 +4,5 @@ "errorMessage": "Resource with id [FunctionApiNoMethod] is invalid. Event with id [ApiEvent] is invalid. Event is missing key 'Path'." } ], - "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 4. Resource with id [FunctionApiMethodArray] is invalid. Event with id [ApiEvent] is invalid. Api Event must have a String specified for 'Method'. Resource with id [FunctionApiNoMethod] is invalid. Event with id [ApiEvent] is invalid. Event is missing key 'Method'. Resource with id [FunctionApiNoPath] is invalid. Event with id [ApiEvent] is invalid. Event is missing key 'Path'. Resource with id [FunctionApiPathArray] is invalid. Event with id [ApiEvent] is invalid. Api Event must have a String specified for 'Path'." -} \ No newline at end of file + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 5. Resource with id [FunctionApiInvalidProperties] is invalid. Event with id [ApiEvent] is invalid. Event 'Properties' must be an Object. If you're using YAML, this may be an indentation issue. Resource with id [FunctionApiMethodArray] is invalid. Event with id [ApiEvent] is invalid. Api Event must have a String specified for 'Method'. Resource with id [FunctionApiNoMethod] is invalid. Event with id [ApiEvent] is invalid. Event is missing key 'Method'. Resource with id [FunctionApiNoPath] is invalid. Event with id [ApiEvent] is invalid. Event is missing key 'Path'. Resource with id [FunctionApiPathArray] is invalid. Event with id [ApiEvent] is invalid. Api Event must have a String specified for 'Path'." +} diff --git a/tests/translator/output/error_implicit_http_api_properties.json b/tests/translator/output/error_implicit_http_api_properties.json new file mode 100644 index 000000000..833d80e03 --- /dev/null +++ b/tests/translator/output/error_implicit_http_api_properties.json @@ -0,0 +1,8 @@ +{ + "errors": [ + { + "errorMessage": "Resource with id [HttpApiFunction] is invalid. Event with id [Basic] is invalid. Event 'Properties' must be an Object. If you're using YAML, this may be an indentation issue." + } + ], + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HttpApiFunction] is invalid. Event with id [Basic] is invalid. Event 'Properties' must be an Object. If you're using YAML, this may be an indentation issue." +} diff --git a/tests/translator/test_translator.py b/tests/translator/test_translator.py index 07f201602..541fbaf62 100644 --- a/tests/translator/test_translator.py +++ b/tests/translator/test_translator.py @@ -675,6 +675,7 @@ def _generate_new_deployment_hash(self, logical_id, dict_to_hash, rest_api_to_sw "error_http_api_invalid_openapi", "error_http_api_tags", "error_http_api_tags_def_uri", + "error_implicit_http_api_properties", "error_implicit_http_api_method", "error_implicit_http_api_path", "error_http_api_event_multiple_same_path",