Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are format attributes supported in API Gateway request validators?

I've added a request validator to my API Gateway swagger file (OAS 3.0). When I test the validation by passing in an invalid request body the error message I receive includes errors that I don't understand. Steps to reproduce are below.

  1. Create a new api gateway using the following swagger:
openapi: 3.0.0
info:
  version: "1"
  title: Request Validation Example
  description: |
    ## Request Validation
    Minimal swagger to reproduce request validation errors.

x-amazon-apigateway-request-validators: 
  all:
    validateRequestBody: true
    validateRequestParameters: true
x-amazon-apigateway-gateway-responses:
  BAD_REQUEST_BODY:
    statusCode: 400
    responseTemplates:
      application/json: |
        {
          message: $context.error.messageString
          errors: $context.error.validationErrorString
        }
paths:
  /employee:
    post:
      x-amazon-apigateway-request-validator: all
      summary: Create a new Employee
      operationId: CreateEmployee
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Employee"
        required: true
      responses:
        "201":
          description: Created
          $ref: "#/components/responses/200"
components:
  responses:
    "200":
      description: Success
  schemas:
    Employee:
      type: object
      properties:
        id:
          type: integer
          format: int32
        phoneNumbers:
          type: array
          items:
            $ref: "#/components/schemas/PhoneNumber"
        salary:
          type: number
          format: double
      required:
        - phoneNumbers
        - salary      
    PhoneNumber:
      type: object
      properties:
        number:
          type: string
      required:
        - number
  1. Set up the integration method for the newly created employee resource, choose the mock integration.

  2. Test the employee POST method using the following request body:

{
    "id": 1,
    "phoneNumbers": [
        {
            "number": "1234567890"
        }
    ],
    "salary": 45000
}

Request validation will succeed with this request body

  1. Test the employee POST method with the following request body:
{
    "id": "1",
    "phoneNumbers": [
        {
            "number": "1234567890"
        }
    ],
    "salary": 45000
}

You will now see the following request validation error:

{
  message:  "Invalid request body"
  errors: [instance type (string) does not match any allowed primitive type (allowed: [\"integer\"]), format attribute \"double\" not supported, format attribute \"int32\" not supported]
}

You can see that this message includes the correct error saying that the string id doesn't match the type integer. You will also see errors regarding format attributes double and int32 not being supported, these are the errors I don't understand. As far as I know double and int32 format attributes are supported by OAS 2.0 and 3.0. Do API Gateway request validators support the double and int32 format attributes? Is the request validator incorrectly configured in my swagger definition?

Edit: It appears that the int32 and double format attributes are known issues: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html#api-gateway-known-issues-rest-apis

However, I also experience these issues using a regex in the format attribute. This isn't specifically mentioned in the known issues so still looking for information on that.

like image 813
Ray Boyd Avatar asked Jan 03 '20 16:01

Ray Boyd


People also ask

Which format is supported for API definition files in API gateway?

Currently, API Gateway supports OpenAPI v2. 0 and OpenAPI v3. 0 definition files.

What are some of the valid methods that we can use with API gateway?

Closely associated with the proxy resource, API Gateway supports an HTTP method of ANY . This ANY method represents any HTTP method that is to be supplied at run time. It allows you to use a single API method setup for all of the supported HTTP methods of DELETE , GET , HEAD , OPTIONS , PATCH , POST , and PUT .

How do I add a request validator to AWS API gateway?

To enable a request validator on a methodCreate a new or choose an existing resource of the API. Create a new or choose an existing method the resource. Choose Method Request. Choose the pencil icon of Request Validator under Settings.


1 Answers

I think it's important to note that the models defined in a OAS document are supposed to be JSONSchema, not necessarily OpenAPI. They're validated at runtime as JSONSchema Draft 4, which does not include a format attribute in the specification.

What can be confusing at times is the import operation. When using OpenAPI to define your API and importing it, API Gateway ends up parsing the intersection of the OAS specification for models and JSONSchema draft 4.

If there is an attribute of JSONSchema that you need, which is not included in OpenAPI's Schema specification, (e.g. type: [..., null]) then creating or updating an API Gateway ::Model directly is a workaround.

like image 88
Jmoney38 Avatar answered Oct 19 '22 06:10

Jmoney38