Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Fix Issue 486: For a POST with body taking an array of strings, elements of the array can be improperly coerced into numeric values. #499

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion middleware/swagger-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 32 additions & 0 deletions test/2.0/test-middleware-swagger-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down