diff --git a/samtranslator/plugins/api/implicit_http_api_plugin.py b/samtranslator/plugins/api/implicit_http_api_plugin.py index 372742632..5012ac11f 100644 --- a/samtranslator/plugins/api/implicit_http_api_plugin.py +++ b/samtranslator/plugins/api/implicit_http_api_plugin.py @@ -87,9 +87,11 @@ def _process_api_events( key = "Path" if not isinstance(path, six.string_types) else "Method" raise InvalidEventException(logicalId, "Api Event must have a String specified for '{}'.".format(key)) - # !Ref is resolved by this time. If it is still a dict, we can't parse/use this Api. - if isinstance(api_id, dict): - raise InvalidEventException(logicalId, "Api Event must reference an Api in the same template.") + # !Ref is resolved by this time. If it is not a string, we can't parse/use this Api. + if api_id and not isinstance(api_id, six.string_types): + raise InvalidEventException( + logicalId, "Api Event's ApiId must be a string referencing an Api in the same template." + ) api_dict_condition = self.api_conditions.setdefault(api_id, {}) method_conditions = api_dict_condition.setdefault(path, {}) diff --git a/samtranslator/plugins/api/implicit_rest_api_plugin.py b/samtranslator/plugins/api/implicit_rest_api_plugin.py index 3e94309f1..5636abbf2 100644 --- a/samtranslator/plugins/api/implicit_rest_api_plugin.py +++ b/samtranslator/plugins/api/implicit_rest_api_plugin.py @@ -85,9 +85,11 @@ def _process_api_events( if not isinstance(method, six.string_types): raise InvalidEventException(logicalId, "Api Event must have a String specified for 'Method'.") - # !Ref is resolved by this time. If it is still a dict, we can't parse/use this Api. - if isinstance(api_id, dict): - raise InvalidEventException(logicalId, "Api Event must reference an Api in the same template.") + # !Ref is resolved by this time. If it is not a string, we can't parse/use this Api. + if api_id and not isinstance(api_id, six.string_types): + raise InvalidEventException( + logicalId, "Api Event's RestApiId must be a string referencing an Api in the same template." + ) api_dict_condition = self.api_conditions.setdefault(api_id, {}) method_conditions = api_dict_condition.setdefault(path, {}) diff --git a/tests/translator/input/error_function_invalid_event_api_ref.yaml b/tests/translator/input/error_function_invalid_event_api_ref.yaml new file mode 100644 index 000000000..ac7052f52 --- /dev/null +++ b/tests/translator/input/error_function_invalid_event_api_ref.yaml @@ -0,0 +1,16 @@ +Resources: + FunctionApiRestApiRefError: + Type: 'AWS::Serverless::Function' + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + Events: + ApiEvent: + Type: Api + Properties: + Method: get + Path: / + # RestApiId should not be a list + RestApiId: + - Fn::Sub: ServerlessRestApi diff --git a/tests/translator/input/error_function_invalid_event_http_api_ref.yaml b/tests/translator/input/error_function_invalid_event_http_api_ref.yaml new file mode 100644 index 000000000..fb05dc323 --- /dev/null +++ b/tests/translator/input/error_function_invalid_event_http_api_ref.yaml @@ -0,0 +1,17 @@ +Resources: + FunctionApiHttpApiRefError: + Type: 'AWS::Serverless::Function' + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + Events: + ApiEvent: + Type: HttpApi + Properties: + Method: get + Path: / + # ApiId should not be a list + ApiId: + - Fn::Sub: ServerlessHttpApi + diff --git a/tests/translator/output/error_api_event_import_vaule_reference.json b/tests/translator/output/error_api_event_import_vaule_reference.json index fa85df16b..83b444b46 100644 --- a/tests/translator/output/error_api_event_import_vaule_reference.json +++ b/tests/translator/output/error_api_event_import_vaule_reference.json @@ -1 +1 @@ -{"errorMessage":"Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [Function] is invalid. Event with id [GetHtml] is invalid. Api Event must reference an Api in the same template."} \ No newline at end of file +{"errorMessage":"Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [Function] is invalid. Event with id [GetHtml] is invalid. Api Event's RestApiId must be a string referencing an Api in the same template."} \ No newline at end of file diff --git a/tests/translator/output/error_api_swagger_integration_with_condition_intrinsic_api_id.json b/tests/translator/output/error_api_swagger_integration_with_condition_intrinsic_api_id.json index ffefd8463..fd1abe8a3 100644 --- a/tests/translator/output/error_api_swagger_integration_with_condition_intrinsic_api_id.json +++ b/tests/translator/output/error_api_swagger_integration_with_condition_intrinsic_api_id.json @@ -1,4 +1,4 @@ { - "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event must reference an Api in the same template." + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event's RestApiId must be a string referencing an Api in the same template." } \ No newline at end of file diff --git a/tests/translator/output/error_api_swagger_integration_with_find_in_map_intrinsic_api_id.json b/tests/translator/output/error_api_swagger_integration_with_find_in_map_intrinsic_api_id.json index ffefd8463..fd1abe8a3 100644 --- a/tests/translator/output/error_api_swagger_integration_with_find_in_map_intrinsic_api_id.json +++ b/tests/translator/output/error_api_swagger_integration_with_find_in_map_intrinsic_api_id.json @@ -1,4 +1,4 @@ { - "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event must reference an Api in the same template." + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event's RestApiId must be a string referencing an Api in the same template." } \ No newline at end of file diff --git a/tests/translator/output/error_api_swagger_integration_with_getatt_intrinsic_api_id.json b/tests/translator/output/error_api_swagger_integration_with_getatt_intrinsic_api_id.json index ffefd8463..fd1abe8a3 100644 --- a/tests/translator/output/error_api_swagger_integration_with_getatt_intrinsic_api_id.json +++ b/tests/translator/output/error_api_swagger_integration_with_getatt_intrinsic_api_id.json @@ -1,4 +1,4 @@ { - "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event must reference an Api in the same template." + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event's RestApiId must be a string referencing an Api in the same template." } \ No newline at end of file diff --git a/tests/translator/output/error_api_swagger_integration_with_join_intrinsic_api_id.json b/tests/translator/output/error_api_swagger_integration_with_join_intrinsic_api_id.json index ffefd8463..fd1abe8a3 100644 --- a/tests/translator/output/error_api_swagger_integration_with_join_intrinsic_api_id.json +++ b/tests/translator/output/error_api_swagger_integration_with_join_intrinsic_api_id.json @@ -1,4 +1,4 @@ { - "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event must reference an Api in the same template." + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event's RestApiId must be a string referencing an Api in the same template." } \ No newline at end of file diff --git a/tests/translator/output/error_api_swagger_integration_with_select_intrinsic_api_id.json b/tests/translator/output/error_api_swagger_integration_with_select_intrinsic_api_id.json index ffefd8463..fd1abe8a3 100644 --- a/tests/translator/output/error_api_swagger_integration_with_select_intrinsic_api_id.json +++ b/tests/translator/output/error_api_swagger_integration_with_select_intrinsic_api_id.json @@ -1,4 +1,4 @@ { - "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event must reference an Api in the same template." + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event's RestApiId must be a string referencing an Api in the same template." } \ No newline at end of file diff --git a/tests/translator/output/error_api_swagger_integration_with_sub_intrinsic_api_id.json b/tests/translator/output/error_api_swagger_integration_with_sub_intrinsic_api_id.json index ffefd8463..fd1abe8a3 100644 --- a/tests/translator/output/error_api_swagger_integration_with_sub_intrinsic_api_id.json +++ b/tests/translator/output/error_api_swagger_integration_with_sub_intrinsic_api_id.json @@ -1,4 +1,4 @@ { - "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event must reference an Api in the same template." + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event's RestApiId must be a string referencing an Api in the same template." } \ No newline at end of file diff --git a/tests/translator/output/error_api_swagger_integration_with_transform_intrinsic_api_id.json b/tests/translator/output/error_api_swagger_integration_with_transform_intrinsic_api_id.json index ffefd8463..fd1abe8a3 100644 --- a/tests/translator/output/error_api_swagger_integration_with_transform_intrinsic_api_id.json +++ b/tests/translator/output/error_api_swagger_integration_with_transform_intrinsic_api_id.json @@ -1,4 +1,4 @@ { - "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event must reference an Api in the same template." + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HtmlFunction] is invalid. Event with id [GetHtml] is invalid. Api Event's RestApiId must be a string referencing an Api in the same template." } \ No newline at end of file diff --git a/tests/translator/output/error_function_invalid_event_api_ref.json b/tests/translator/output/error_function_invalid_event_api_ref.json new file mode 100644 index 000000000..b02fdaa75 --- /dev/null +++ b/tests/translator/output/error_function_invalid_event_api_ref.json @@ -0,0 +1,8 @@ +{ + "errors": [ + { + "errorMessage": "Resource with id [FunctionApiRestApiRefError] is invalid. Event with id [ApiEvent] is invalid. Api Event's RestApiId must be a string referencing an Api in the same template." + } + ], + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [FunctionApiRestApiRefError] is invalid. Event with id [ApiEvent] is invalid. Api Event's RestApiId must be a string referencing an Api in the same template." +} diff --git a/tests/translator/output/error_function_invalid_event_http_api_ref.json b/tests/translator/output/error_function_invalid_event_http_api_ref.json new file mode 100644 index 000000000..466a4bf4f --- /dev/null +++ b/tests/translator/output/error_function_invalid_event_http_api_ref.json @@ -0,0 +1,8 @@ +{ + "errors": [ + { + "errorMessage": "Resource with id [FunctionApiHttpApiRefError] is invalid. Event with id [ApiEvent] is invalid. Api Event's ApiId must be a string referencing an Api in the same template." + } + ], + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [FunctionApiHttpApiRefError] is invalid. Event with id [ApiEvent] is invalid. Api Event's ApiId must be a string referencing an Api in the same template." +}