aws / aws/serverless-application-model

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

Open
#2,209 5 comments 1 reaction 0 assignees View on GitHub
type/feature
Dominant language
Python
Stars
9.6k
Forks
2.5k
Avg merge
1d 11h
Merged PRs (30d)
7

Description

### 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.