From 2d456d00fc8167404c705e8034b72ce8542d3be9 Mon Sep 17 00:00:00 2001 From: Matthew Litwin Date: Fri, 14 Apr 2017 13:41:18 -0700 Subject: [PATCH 1/2] Fix swagger-tools/issues/486 - validateValue must pass correct schema when body is array. 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. Without this, you get a bogus schema, which eventually leads to converValue thinnking it's got an object type (the default), and attempting to JSON.stringify the nice string into an object. --- middleware/swagger-validator.js | 4 ++- test/2.0/test-middleware-swagger-metadata.js | 32 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/middleware/swagger-validator.js b/middleware/swagger-validator.js index 514f1dfd5d..53f70f7d2c 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 + // etParameterType 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; From 41ecbcd72f8ce2dcae7ffb9cff9db7ba07fae3f0 Mon Sep 17 00:00:00 2001 From: Matthew Litwin Date: Fri, 14 Apr 2017 13:53:27 -0700 Subject: [PATCH 2/2] Clean up comment --- middleware/swagger-validator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/middleware/swagger-validator.js b/middleware/swagger-validator.js index 53f70f7d2c..e76df8708f 100644 --- a/middleware/swagger-validator.js +++ b/middleware/swagger-validator.js @@ -114,7 +114,7 @@ var validateValue = function (req, schema, path, val, location, callback) { var spec = cHelpers.getSpec(version); // In swagger 2, a parameter in body will have a .schema, rather than be a schema: http://swagger.io/specification/#parameterObject - // etParameterType is already savvy to this, so we do what it does for the schema. + // 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 {