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

AWS::Serverless::Api Models only support object type

Open hurlenko opened this issue 5 years ago • 4 comments

Description:

Defining api model with type other than object (specifically without a properties attribute) results in validation error.

SAM CLI, version 0.43.0

Steps to reproduce the issue:

  1. Define Models property to include a model without the properties (for example type array) attribute like so:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Models:
        MyModel:
          items:
            type: integer
          type: array
          title: MyModel
  1. Run sam validate -t template.yaml

Observed result:

An exception is raised:

ValueError: Invalid input. Value for properties is required

Expected result:

Successful validation. Models support all types supported by json schema without requirement for properties.

hurlenko avatar Apr 14 '20 17:04 hurlenko

It looks like SAM enforces having properties in the model here. Looks like we could update this section to remove this check.

keetonian avatar Apr 17 '20 21:04 keetonian

So I tried removing these two lines and after that sam validate succeeded, however running sam deploy failed with this error.

Error: Failed to create changeset for the stack: model-test, ex: Waiter ChangeSetCreateComplete failed: 
Waiter encountered a terminal failure state Status: FAILED. 
Reason: Transform AWS::Serverless-2016-10-31 failed with: Internal transform failure.

Currently a workaround is to define a "dummy" properties attribute like this:

MyModel:
  properties:
    dummy: {}
  items:
    type: integer
  type: array
  title: MyModel

which produces this APIGW model:

{
  "title" : "MyModel",
  "properties" : { },
  "type" : "array",
  "items" : {
    "type" : "integer"
  }
}

The properties attribute is never used, as the type of the object is array, and APIGW validation works as expected.

hurlenko avatar Apr 18 '20 19:04 hurlenko

@hurlenko yeah it looks like the ValueError that is being thrown when you run this locally isn't caught when it's run in the cloud, so it's returning an internal transform failure.

You would have to run sam deploy on a transformed version of the template in order for this to work; local changes to SAM are not reflected in the cloud version of SAM when you deploy a template.

keetonian avatar Apr 21 '20 21:04 keetonian

Hi @keetonian, first time here ! :). I can work in this issue but need to ask something before: Why are we doing this post-validation step as we've just run it for the whole body ? Shouldn't we trust in open api's validation and remove both validations ?

gmcrocetti avatar Sep 12 '20 22:09 gmcrocetti