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