kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
892 stars 235 forks source link

openapi-request-validator doesn't validate JSON objects given in headers or query params #854

Open cguy opened 1 year ago

cguy commented 1 year ago

Hello,

I have an issue regarding the request validator. It doesn't seem to validate objects (content > application/json) given in header or query params.

Stackblitz to reproduce : https://stackblitz.com/edit/node-bfjjnw?embed=1&file=index.js,my-api.yml

---
openapi: 3.0.3
info:
  title: API name
  description: API description
  version: 0.0.1
paths:
  "/test":
    get:
      parameters:
        - in: header
          name: x-my-header
          required: true
          type: string
          pattern: '(test1|test2)'
        - in: query
          name: filter
          required: true
          content:
            application/json:
              schema:
                type: object
                required:
                  - "color"
                properties:
                  type:
                    type: string
                  color:
                    type: string
                    pattern: "(black|white)"
      responses:
        "204":
          description: OK

Calling the validator

// run `node index.js` in the terminal
const SwaggerParser = require('@apidevtools/swagger-parser');
const OpenAPIRequestValidator = require('openapi-request-validator');

console.log(`Hello Node.js v${process.versions.node}!`);

(async () => {
  let api = await SwaggerParser.bundle('./my-api.yml');
  const validator = new OpenAPIRequestValidator.default({
    ...api.paths['/test'].get,
  });

  const validation = validator.validateRequest({
    body: {},
    headers: { 'x-my-header': 'test1' },
    query: { filter: '{"type": "test"}' },
  });
  console.log('\n\nshould contains a validation error about query', validation);
})();

I should have some errors color is required (and have pattern to respect). Is there anything I am doing wrong?

sshahar1 commented 1 year ago

I think I'm experiencing the same issue