Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions samtranslator/plugins/api/implicit_api_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ def _process_api_events(self, function, api_events, template, condition=None):
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.")

api_dict = self.api_conditions.setdefault(api_id, {})
method_conditions = api_dict.setdefault(path, {})
method_conditions[method] = condition
Expand Down
25 changes: 25 additions & 0 deletions tests/plugins/api/test_implicit_api_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,31 @@ def test_must_verify_method_is_string(self):
with self.assertRaises(InvalidEventException) as context:
self.plugin._process_api_events(function, api_events, template)

def test_must_verify_rest_api_id_is_string(self):
api_events = {
"Api1": {
"Type": "Api",
"Properties": {
"Path": "/",
"Method": ["POST"],
"RestApiId": {"Fn::ImportValue": {"Fn::Sub": {"ApiName"}}}
}
}
}

template = Mock()
function_events_mock = Mock()
function = SamResource({
"Type": SamResourceType.Function.value,
"Properties": {
"Events": function_events_mock
}
})
function_events_mock.update = Mock()

with self.assertRaises(InvalidEventException) as context:
self.plugin._process_api_events(function, api_events, template)

def test_must_verify_path_is_string(self):
api_events = {
"Api1": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Parameters:
Api:
Type: String
Default: MyApi

Resources:
Function:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://sam-demo-bucket/member_portal.zip
Handler: index.gethtml
Runtime: nodejs4.3
Events:
GetHtml:
Type: Api
Properties:
Path: /
Method: get
RestApiId:
Fn::ImportValue:
Fn::Sub: ${Api}
Original file line number Diff line number Diff line change
@@ -0,0 +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."}
3 changes: 2 additions & 1 deletion tests/translator/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,8 @@ def _generate_new_deployment_hash(self, logical_id, dict_to_hash, rest_api_to_sw
'error_invalid_document_empty_semantic_version',
'error_api_with_invalid_open_api_version_type',
'error_api_with_custom_domains_invalid',
'error_api_with_custom_domains_route53_invalid'
'error_api_with_custom_domains_route53_invalid',
'error_api_event_import_vaule_reference'
])
@patch('boto3.session.Session.region_name', 'ap-southeast-1')
@patch('samtranslator.plugins.application.serverless_app_plugin.ServerlessAppPlugin._sar_service_call', mock_sar_service_call)
Expand Down