swagger-tools icon indicating copy to clipboard operation
swagger-tools copied to clipboard

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

Open argos83 opened this issue 7 years ago • 1 comments

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:

  • [] -> valid as expected
  • [{"key": "a"}] -> valid as expected
  • [{"key": "a"}, {"key": "b"}] -> valid as expected
  • [{"missingKey": "a"}] -> invalid as expected
  • {} -> invalid as expected
  • {"key": "a"} -> should be invalid however validation passes

This is tested on:

  • node v6.11.2
  • swagger-tools v0.10.1
  • express v4.15.4

argos83 avatar Sep 20 '17 23:09 argos83

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

argos83 avatar Sep 21 '17 00:09 argos83