apigee-127 / swagger-tools

A Node.js and browser module that provides tooling around Swagger.
MIT License
702 stars 373 forks source link

Validation incorrectly passes when array is expected but single item is sent #532

Open argos83 opened 7 years ago

argos83 commented 7 years ago

Hi, First of all thanks for this great library!

I'm came across a validation issue (unless I'm doing things wrong). Here it is:

Given this swagger specification:

swagger: '2.0'
info:
  version: '0.0.0'
  title: 'Test'

consumes:
  - 'application/json'

definitions:
  someObject:
    type: object
    properties:
      key:
        type: string
    required:
      - key

paths:
  /a-list:
    post:
      parameters:
        - name: 'body'
          in: 'body'
          required: true
          schema:
            type: array
            items:
              $ref: '#/definitions/someObject'
      responses:
        204:
          description: 'OK'

And this express app:

const express = require('express');
const swaggerTools = require('swagger-tools');
const yaml = require('yamljs');

const app = express();

const swaggerSpec = yaml.load('./swagger.yml');

swaggerTools.initializeMiddleware(swaggerSpec, (middleware) => {

    app.use(middleware.swaggerMetadata());
    app.use(middleware.swaggerValidator());

    app.post('/a-list', function (req, res) {
        res.status(204).end();
    });

    app.listen(3000, function () {
        console.log('Example app listening on port 3000!')
    });   
});

I'm getting the following validation results when POSTing the following payloads to /a-list:

This is tested on:

argos83 commented 7 years ago

Might be related to this https://github.com/apigee-127/swagger-tools/issues/438