From 02921bce3579c4bba8c28866457dd664069ce3ff Mon Sep 17 00:00:00 2001 From: jonife <79116465+jonife@users.noreply.github.com> Date: Tue, 22 Mar 2022 12:52:55 -0500 Subject: [PATCH] fix: add validation for API property Model (#2340) --- samtranslator/model/eventsources/push.py | 10 ++++- ...error_api_with_models_of_invalid_type.yaml | 37 +++++++++++++++++++ ...error_api_with_models_of_invalid_type.json | 3 ++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/translator/input/error_api_with_models_of_invalid_type.yaml create mode 100644 tests/translator/output/error_api_with_models_of_invalid_type.json diff --git a/samtranslator/model/eventsources/push.py b/samtranslator/model/eventsources/push.py index b9a6f5f7b..cac05ba1a 100644 --- a/samtranslator/model/eventsources/push.py +++ b/samtranslator/model/eventsources/push.py @@ -5,7 +5,7 @@ from samtranslator.model import ResourceMacro, PropertyType from samtranslator.model.eventsources import FUNCTION_EVETSOURCE_METRIC_PREFIX from samtranslator.model.types import is_type, list_of, dict_of, one_of, is_str -from samtranslator.model.intrinsics import ref, fnGetAtt, fnSub, make_shorthand, make_conditional +from samtranslator.model.intrinsics import is_intrinsic, ref, fnGetAtt, fnSub, make_shorthand, make_conditional from samtranslator.model.tags.resource_tagging import get_tag_list from samtranslator.model.s3 import S3Bucket @@ -767,6 +767,14 @@ def _add_swagger_integration(self, api, function, intrinsics_resolver): model=method_model, method=self.Method, path=self.Path ), ) + if not is_intrinsic(api_models) and not isinstance(api_models, dict): + raise InvalidEventException( + self.relative_id, + "Unable to set RequestModel [{model}] on API method [{method}] for path [{path}] " + "because the related API Models defined is of invalid type.".format( + model=method_model, method=self.Method, path=self.Path + ), + ) if not isinstance(method_model, str): raise InvalidEventException( self.relative_id, diff --git a/tests/translator/input/error_api_with_models_of_invalid_type.yaml b/tests/translator/input/error_api_with_models_of_invalid_type.yaml new file mode 100644 index 000000000..0a085e0ca --- /dev/null +++ b/tests/translator/input/error_api_with_models_of_invalid_type.yaml @@ -0,0 +1,37 @@ +Resources: + MyApi: + Type: AWS::Serverless::Api + Properties: + StageName: Prod + Models: + - Invalid_value + + MyFunction: + Type: AWS::Serverless::Function + Properties: + Handler: index.handler + Runtime: nodejs12.x + InlineCode: | + exports.handler = async (event, context, callback) => { + return { + statusCode: 200, + body: 'Success' + } + } + Events: + None: + Type: Api + Properties: + RequestModel: + Model: User + Required: true + RestApiId: + Ref: MyApi + Method: get + Path: /none + +Outputs: + ApiUrl: + Description: "API endpoint URL for Prod environment" + Value: + Fn::Sub: 'https://${MyApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/Prod/' \ No newline at end of file diff --git a/tests/translator/output/error_api_with_models_of_invalid_type.json b/tests/translator/output/error_api_with_models_of_invalid_type.json new file mode 100644 index 000000000..1f00f5733 --- /dev/null +++ b/tests/translator/output/error_api_with_models_of_invalid_type.json @@ -0,0 +1,3 @@ +{ + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 2. Resource with id [MyApi] is invalid. Type of property 'Models' is invalid. Resource with id [MyFunction] is invalid. Event with id [None] is invalid. Unable to set RequestModel [User] on API method [get] for path [/none] because the related API Models defined is of invalid type." +} \ No newline at end of file