简体   繁体   中英

How can I let a null values pass in a sam template if it is in an API Gateway DefinitionBody as a response example

I have an OpenAPI 3.0.1 file and conforming to the standard it contains some examples responses for endpoints. Now since some of the parameters in the response are set to be nullable there are some examples which look as follows:

examples:
  account_not_verified:
    value:
      eamil: 'test@example.com'
      verification: '0'
      name: null

Since the name parameters is set up like this:

name:
    type: string
    nullable: true

I would assume that having a null value in the example response is valid by the open api standard. When I put this into the Swagger Editor is does not show any errors or warnings. But as soon as I include the OpenAPI file into my sam template to be used to configure an HTTP API Gateway like so:

ApiGateway:
  Type: AWS::Serverless::HttpApi
  Properties:
    StageName: !Ref Stage
    FailOnWarnings: true
    DefinitionBody:
      Fn::Transform:
        Name: AWS::Include
        Parameters:
          Location: ./oapi.yml

Cloud formation gives me the following error:

[/Resources/ApiGateway/Type/Body/paths/***/content/application/json/examples/ch_account_not_verified/value/name] 'null' values are not allowed in templates

Is there any way to allow this null value to exist in the example response or do I have to use some different method to indicate the null value in the example?

So with some trial and error, I was able to define it to allow strings and nulls, at least for a SAM template RequestBody. I am assuming API gateway uses JSON validation under the sheets, so if you define this in JSONSchema, for something like AWS power-tools validation, for example:

  "properties": {
    "spouse_first_name": {
    "$id": "#/properties/spouse_first_name",
    "type": ["string", "null"],
    "title": "The spouse_first_name",
    "maxLength": 100
    }
  }

Then the equivalent syntax in SAM template YAML would be:

   MyRequestModel:
     type: object
     properties:
       spouse_first_name:
         type: string, null
         maxLength: 100

I just tested this and it seems to allow both strings and nulls.

  • In the case of body validation, Don't forget to add 'ValidateBody: true' in your SAM template to force API gateway to validate your payload:

     RequestModel: Model: MyRequestModel ValidateBody: true
  • I realize this is not an apples to apples answer, but the take away is that you should be able to define it in comma separated values in YAML

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM