serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

Models Validation doesn't check the implicitly created AWS::Serverless::Api resource

Open stevehogdahl opened this issue 3 years ago • 5 comments

Description:

The Models Validation doesn't check the implicitly created AWS::Serverless::Api resource. When creating a new model there's no way to set it to the implicitly AWS::Serverless::Api resource without sam validate failing. Models isn't a valid global parameter so it can't be set there. Creating a AWS::ApiGateway::Model resource setting RestApiId to ServerlessRestApi the following validation error is thrown ...the related API does not define any Models. This means the only way to add a Model is to explicitly create the Api resource, add the Models to it, and then set RestApiId on the other resources. This is a pain point for us because we have 75 Function resources in the template that now need to explicitly set RestApiId.

Steps to reproduce:

Account:
  Type: 'AWS::ApiGateway::Model'
  Properties:
    RestApiId: !Ref ServerlessRestApi
    ContentType: application/json
    Name: Account
    Schema:
      $schema: 'http://json-schema.org/draft-04/schema#'
      title: Account
      type: array
      items:
        type: object
        properties:
          name:
            type: string
          status:
            type: string
CreateAccountFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: controllers/accounts.create
      Events:
        CreateAccount:
          Type: Api
          Properties:
            Path: /accounts
            Method: post
            RequestModel:
              Model: Account
              Required: true
              ValidateBody: true
      Role: !GetAtt LambdaRole.Arn

Observed result:

Error: [InvalidResourceException('CreateAccountFunction', 'Event with id [CreateAccount] is invalid. Unable to set RequestModel [CreateAccountsModel] on API method [post] for path [/accounts] because the related API does not define any Models.')] ('CreateAccountFunction', 'Event with id [CreateAccount] is invalid. Unable to set RequestModel [CreateAccountsModel] on API method [post] for path [/accounts] because the related API does not define any Models.')

Expected result:

I would expect the validation to check if any models exist on the implicitly created Api resource.

stevehogdahl avatar Nov 05 '21 14:11 stevehogdahl

Thanks for reporting this in! Please allow us some time to investigate the feasibility of the request! Again thanks for the input!

qingchm avatar Apr 06 '22 16:04 qingchm

Facing the same issue

nirajmchauhan avatar Jun 08 '22 05:06 nirajmchauhan

Facing Same Issue, Any update on above request.

thimmareddykovvuri avatar Apr 13 '23 16:04 thimmareddykovvuri

Marking this as type/bug as users are having issues with defining API models for implicit API resource generated by SAM Function.

GavinZZ avatar Oct 20 '23 00:10 GavinZZ

I took a deep dive into this problem. Unfortunately I believe this is expected behaviour and we've clearly documented this behaviour here.

The workaround would have to define an explicit AWS::Serverless::Api resource that specifies the Models property. Explicitly set the RestApiId field. We apologize for any inconvenience. Leaving this issue open as a feature request.

GavinZZ avatar Oct 25 '23 19:10 GavinZZ