Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise InvalidEventException when RestApiId/ApiId is not resolved to a string #2213

Merged
merged 1 commit into from
Nov 9, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions samtranslator/plugins/api/implicit_http_api_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})
Expand Down
8 changes: 5 additions & 3 deletions samtranslator/plugins/api/implicit_rest_api_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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."}
{"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."}
Original file line number Diff line number Diff line change
@@ -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."
}

Original file line number Diff line number Diff line change
@@ -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."
}

Original file line number Diff line number Diff line change
@@ -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."
}

Original file line number Diff line number Diff line change
@@ -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."
}

Original file line number Diff line number Diff line change
@@ -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."
}

Original file line number Diff line number Diff line change
@@ -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."
}

Original file line number Diff line number Diff line change
@@ -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."
}

Original file line number Diff line number Diff line change
@@ -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."
}
Original file line number Diff line number Diff line change
@@ -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."
}