Skip to content

Commit 3725f00

Browse files
keetonianShreya
authored andcommitted
fix: throw better error for improper api reference (#1224)
1 parent f006f0f commit 3725f00

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

samtranslator/plugins/api/implicit_api_plugin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ def _process_api_events(self, function, api_events, template, condition=None):
150150
raise InvalidEventException(logicalId,
151151
"Api Event must have a String specified for 'Method'.")
152152

153+
# !Ref is resolved by this time. If it is still a dict, we can't parse/use this Api.
154+
if (isinstance(api_id, dict)):
155+
raise InvalidEventException(logicalId,
156+
"Api Event must reference an Api in the same template.")
157+
153158
api_dict = self.api_conditions.setdefault(api_id, {})
154159
method_conditions = api_dict.setdefault(path, {})
155160
method_conditions[method] = condition

tests/plugins/api/test_implicit_api_plugin.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,31 @@ def test_must_verify_method_is_string(self):
447447
with self.assertRaises(InvalidEventException) as context:
448448
self.plugin._process_api_events(function, api_events, template)
449449

450+
def test_must_verify_rest_api_id_is_string(self):
451+
api_events = {
452+
"Api1": {
453+
"Type": "Api",
454+
"Properties": {
455+
"Path": "/",
456+
"Method": ["POST"],
457+
"RestApiId": {"Fn::ImportValue": {"Fn::Sub": {"ApiName"}}}
458+
}
459+
}
460+
}
461+
462+
template = Mock()
463+
function_events_mock = Mock()
464+
function = SamResource({
465+
"Type": SamResourceType.Function.value,
466+
"Properties": {
467+
"Events": function_events_mock
468+
}
469+
})
470+
function_events_mock.update = Mock()
471+
472+
with self.assertRaises(InvalidEventException) as context:
473+
self.plugin._process_api_events(function, api_events, template)
474+
450475
def test_must_verify_path_is_string(self):
451476
api_events = {
452477
"Api1": {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Parameters:
2+
Api:
3+
Type: String
4+
Default: MyApi
5+
6+
Resources:
7+
Function:
8+
Type: AWS::Serverless::Function
9+
Properties:
10+
CodeUri: s3://sam-demo-bucket/member_portal.zip
11+
Handler: index.gethtml
12+
Runtime: nodejs4.3
13+
Events:
14+
GetHtml:
15+
Type: Api
16+
Properties:
17+
Path: /
18+
Method: get
19+
RestApiId:
20+
Fn::ImportValue:
21+
Fn::Sub: ${Api}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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."}

tests/translator/test_translator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,8 @@ def _generate_new_deployment_hash(self, logical_id, dict_to_hash, rest_api_to_sw
548548
'error_invalid_document_empty_semantic_version',
549549
'error_api_with_invalid_open_api_version_type',
550550
'error_api_with_custom_domains_invalid',
551-
'error_api_with_custom_domains_route53_invalid'
551+
'error_api_with_custom_domains_route53_invalid',
552+
'error_api_event_import_vaule_reference'
552553
])
553554
@patch('boto3.session.Session.region_name', 'ap-southeast-1')
554555
@patch('samtranslator.plugins.application.serverless_app_plugin.ServerlessAppPlugin._sar_service_call', mock_sar_service_call)

0 commit comments

Comments
 (0)