PayU / openapi-validator-middleware

Input validation using Swagger (Open API) and ajv
Apache License 2.0
144 stars 50 forks source link

contentTypeValidation boolean not supported for Open API 3.0 #174

Closed NivLipetz closed 2 years ago

NivLipetz commented 2 years ago

According to Open API 3.0 documentation - https://swagger.io/docs/specification/describing-request-body/

The consumes array is replaced with the requestBody.content map which maps the media types to their schemas.

The current option parameter: contentTypeValidation supports only the consumes array and not the new content structure that defines the request body.

Example

Open API 3.0 Swagger

openapi: 3.0.1
info:
  title: Sample API
  description: API description in Markdown.
  version: 1.0.0
servers:
- url: https://api.example.com/v1
paths:
  /users:
    post:
      summary: Creates a new user.
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/User'
        required: false
      responses:
        200:
          description: OK
          content: {}
      x-codegen-request-body-name: user
components: {}

for the following request POST /users with a different content-type header than application/json

I expect to see the this error:

'headers content-type must be one of application/json'

But instead I receive an error on the request body missing required params:

body should have required property 'name'

When using Open API 2.0 with the consumes array, the correct error is thrown regarding the problematic content-type header.

Open API 2.0 Swagger:

swagger: "2.0"
info:
  title: Sample API
  description: API description in Markdown.
  version: 1.0.0
host: api.example.com
basePath: /v1
schemes:
  - https
paths:
  /users:
    post:
      consumes:
        - application/json
      summary: Creates a new user.
      parameters:
        - in: body
          name: user
          schema:
            $ref: '#/definitions/User'
      responses:
        200:
          description: OK