Skip to content

Commit

Permalink
normalize behavior of req.param('length) - (see #3738)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed Jun 2, 2016
1 parent dded667 commit d739b0b
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/hooks/http/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,35 @@ module.exports = function(sails) {
// This is also handled separately for virtual requests in `lib/router/`:
// (see https://github.com/balderdashy/sails/pull/3599#issuecomment-195665040)
req._sails = sails;

// Wrap `req.param()` in a shim that normalizes the behavior of `req.param('length')`.
// (see https://github.com/balderdashy/sails/issues/3738#issue-156095626)
var origReqParam = req.param;
req.param = function getValForParam (name){
if (name === 'length') {
// If `req.params.length` is a string, instead of a number, then we know this request
// must have matched a route address like `/foo/bar/:length/baz`, so in that case, we'll
// allow `req.param('length')` to return the runtime value of `length` as a string.
if (_.isString(req.params.length)) {
return req.params.length;
}
else if (!_.isArray(req.body) && _.isObject(req.body) && !_.isUndefined(req.body.length) && !_.isNull(req.body.length)) {
// > In future versions of Sails, this shim will likely be modified to allow the `null` literal to be received
// > as a value for a body parameter and accessed in `req.param()`.
// > (This is because `null` and `undefined` are distinct and lossless when serializing and deserializing to
// > and from JSON-- so it's really specifically for standard JSON response bodies.)
// >
// > However, this is a departure from the behavior of Express, and a breaking change- so it will
// > not happen until the release of Sails v1 (or possibly in a pre v1.0 minor version.)
return req.body.length;
}
else if (_.isObject(req.query) && !_.isUndefined(req.query.length) && !_.isNull(req.query.length)) {
return req.query.length;
}
else { return undefined; }
}
return origReqParam.apply(req, Array.prototype.slice.call(arguments));
};

return next();
});
Expand Down

0 comments on commit d739b0b

Please sign in to comment.