Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated body parser to handle extended content types #1663

Merged
merged 9 commits into from May 27, 2018

Conversation

ifiok
Copy link
Contributor

@ifiok ifiok commented May 21, 2018

Issues

Closes:

Changes

Updates the validator check for content types to allow for extended types such as outlined in:
https://www.iana.org/assignments/media-types/media-types.xhtml.
It also covers for types such as: 'application/json; charset=utf-8'

Copy link
Member

@DonutEspresso DonutEspresso left a comment

Choose a reason for hiding this comment

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

Thanks for the first time contrib! 🎉 Left a few minor comments but very much appreciate the help!

case 'application/json':
var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json');

switch (true) {
Copy link
Member

Choose a reason for hiding this comment

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

Switching on a constant is kind of uncommon - I think I would prefer we do something like map any +json type into application/json (if we find a match) and keep the existing switch statement as is.

var type = req.contentType().toLowerCase();
//var type = req.contentType().toLowerCase();
var type = req
.contentType()
Copy link
Member

Choose a reason for hiding this comment

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

req.contentType() should already support handling ;, did you find out otherwise?

re: https://github.com/restify/node-restify/blob/master/lib/request.js#L210

@@ -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.

@@ -490,3 +490,947 @@ describe('JSON body parser', function() {
client.end();
});
});

describe('JSON body parser with content type application/hal+json', function() {
Copy link
Member

Choose a reason for hiding this comment

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

Wowza, this is a huge test! Thanks for adding all of these but I don't think we need to duplicate all of the existing tests. I'd just add a test or two to make sure that */*+json gets matched as application/json.

Copy link
Contributor Author

@ifiok ifiok left a comment

Choose a reason for hiding this comment

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

I will make these updates.

@ifiok
Copy link
Contributor Author

ifiok commented May 24, 2018

Hello @DonutEspresso, the changes have been made

@DonutEspresso
Copy link
Member

Thanks @ifiok ! I just realized I'm dumb and that jsonBodyParser can be used independent of bodyParser - I think that is why you added the regex test in jsonBodyParser directly too. :( Sorry about that!

Do you mind re-adding that check into jsonBodyParser? Once that's in I think we're ready to roll. Otherwise, this looks great!

@ifiok
Copy link
Contributor Author

ifiok commented May 27, 2018

@DonutEspresso I have added the check back to the jsonBodyParser

@DonutEspresso
Copy link
Member

Thanks @ifiok! Node 10 seems to be failing CI but that seems to be a problem on master. We can follow up on that elsewhere. Thanks for the contrib! 🎉

@DonutEspresso DonutEspresso merged commit 4537514 into restify:master May 27, 2018
@@ -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');

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants