Skip to content
Merged
6 changes: 6 additions & 0 deletions lib/plugins/bodyParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ function bodyParser(options) {
var parser;
var type = req.contentType().toLowerCase();

var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json');
// map any +json to application/json
if (jsonPatternMatcher.test(type)) {
type = 'application/json';
}

switch (type) {
case 'application/json':
parser = parseJson[0];
Expand Down
9 changes: 7 additions & 2 deletions lib/plugins/jsonBodyParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ function jsonBodyParser(options) {
// save original body on req.rawBody and req._body
req.rawBody = req._body = req.body;

if (req.getContentType() !== 'application/json' || !req.body) {
return next();
var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're already doing this match here before we call into it jsonBodyParser, we can remove this content type check entirely.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Regexp is used in two different places. Perhaps make it a const and share?

var contentType = req.getContentType();

if (contentType !== 'application/json' || !req.body) {
if (!jsonPatternMatcher.test(contentType)) {
return next();
}
}

var params;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"Falco Nogatz",
"Gergely Nemeth",
"Guillaume Chauvet",
"Ifiok Idiang",
"Isaac Schlueter",
"Jacob Quatier",
"James O'Cull",
Expand Down
29 changes: 29 additions & 0 deletions test/plugins/jsonBodyParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,33 @@ describe('JSON body parser', function() {
client.write('{"malformedJsonWithPercentSign":30%}');
client.end();
});

it('should handle application/*+json as application/json', function(done) {
SERVER.use(restify.plugins.bodyParser({ maxBodySize: 1024 }));

SERVER.post('/', function(req, res, next) {
res.send(200, { length: req.body.length });
next();
});

var opts = {
hostname: '127.0.0.1',
port: PORT,
path: '/',
method: 'POST',
agent: false,
headers: {
accept: 'application/json',
'content-type': 'application/hal+json',
'transfer-encoding': 'chunked'
}
};
var client = http.request(opts, function(res) {
assert.equal(res.statusCode, 413);
res.once('end', done);
res.resume();
});
client.write('{"a":[' + new Array(512).join('1,') + '0]}');
client.end();
});
});