Skip to content

Commit

Permalink
fix: add validation for API property Model (#2340)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonife authored Mar 22, 2022
1 parent 594c54f commit 02921bc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
10 changes: 9 additions & 1 deletion samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions tests/translator/input/error_api_with_models_of_invalid_type.yaml
Original file line number Diff line number Diff line change
@@ -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/'
Original file line number Diff line number Diff line change
@@ -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."
}

0 comments on commit 02921bc

Please sign in to comment.