diff --git a/middleware/swagger-validator.js b/middleware/swagger-validator.js index 514f1dfd5d..e76df8708f 100644 --- a/middleware/swagger-validator.js +++ b/middleware/swagger-validator.js @@ -113,7 +113,9 @@ var validateValue = function (req, schema, path, val, location, callback) { var isModel = mHelpers.isModelParameter(version, schema); var spec = cHelpers.getSpec(version); - val = mHelpers.convertValue(val, schema, mHelpers.getParameterType(schema), location); + // In swagger 2, a parameter in body will have a .schema, rather than be a schema: http://swagger.io/specification/#parameterObject + // getParameterType() is already savvy to this, so we do what it does for the schema. + val = mHelpers.convertValue(val, _.isUndefined(schema.schema) ? schema : schema.schema, mHelpers.getParameterType(schema), location); try { validators.validateSchemaConstraints(version, schema, path, val); diff --git a/test/2.0/test-middleware-swagger-metadata.js b/test/2.0/test-middleware-swagger-metadata.js index f688a260e1..792ea3bf91 100644 --- a/test/2.0/test-middleware-swagger-metadata.js +++ b/test/2.0/test-middleware-swagger-metadata.js @@ -1096,6 +1096,38 @@ describe('Swagger Metadata Middleware v2.0', function () { }); }); + it('should not coerce strings in array body responses (Issue 486)', function (done) { + var cPetStoreJson = _.cloneDeep(petStoreJson); + var petNames = ['0', 'just a string', '{"notreallyjson":true}']; + + cPetStoreJson.paths['/pets'].post.parameters[0].schema = { + type: 'array', + items: { + type: 'string' + } + }; + + helpers.createServer([cPetStoreJson], { + swaggerRouterOptions: { + controllers: { + createPet: function (req, res, next) { + var newPetNames = req.swagger.params.pet.value; + + res.end(JSON.stringify(newPetNames)); + + return next(); } + } + } + }, function(app) { + request(app) + .post('/api/pets') + .set('Accept', 'application/json') + .send(petNames) + .expect(200) + .end(helpers.expectContent(petNames, done)); + }); + }); + it('should not error with file parameter not being provided (Issue 350)', function (done) { var cPetStoreJson = _.cloneDeep(petStoreJson); var operation = _.cloneDeep(cPetStoreJson.paths['/pets']).post;