Skip to content
This repository was archived by the owner on May 15, 2023. It is now read-only.

Commit fbaafcb

Browse files
baynezynoahdietz
authored andcommitted
Make sure that the json property is set when the content is JSON (#140)
* Change all calls to request to use the body property instead of the json property * json property is now set if the produces or consumes is a JSON media type
1 parent 5ac4575 commit fbaafcb

File tree

36 files changed

+518
-82
lines changed

36 files changed

+518
-82
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ handlebars.registerHelper('length', helpers.length);
559559
handlebars.registerHelper('pathify', helpers.pathify);
560560
handlebars.registerHelper('printJSON', helpers.printJSON);
561561
handlebars.registerHelper('requestDataParamFormatter', helpers.requestDataParamFormatter);
562+
handlebars.registerHelper('isJsonRepresentation', helpers.isJsonRepresentation);
562563

563564

564565
module.exports = {

lib/helpers.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
var _ = require('lodash');
44
var strObj = require('string');
55
var url = require('url');
6-
var TYPE_JSON = 'application/json';
76
var len;
87

98
module.exports = {
@@ -15,7 +14,8 @@ module.exports = {
1514
printJSON: printJSON,
1615
len: len,
1716
setLen: setLen,
18-
requestDataParamFormatter : requestDataParamFormatter
17+
requestDataParamFormatter : requestDataParamFormatter,
18+
isJsonRepresentation : isJsonRepresentation
1919
};
2020

2121
function setLen(descriptionLength) {
@@ -60,13 +60,30 @@ function validateResponse(type, noSchema,
6060
'needs 2 parameters');
6161
}
6262

63-
if (!noSchema && type === TYPE_JSON) {
63+
if (!noSchema && isJsonMediaType(type)) {
6464
return options.fn(this);
6565
} else {
6666
return options.inverse(this);
6767
}
6868
}
6969

70+
/**
71+
* decides if this request/response has a JSON representation
72+
* @param {string} contentType the media type of the request
73+
* @param {string} returnType the media type of the response
74+
*/
75+
function isJsonRepresentation(contentType, returnType, options) {
76+
return (isJsonMediaType(contentType) || isJsonMediaType(returnType)) ? options.fn(this) : options.inverse(this);
77+
}
78+
79+
/**
80+
* determines if the mediatype is json
81+
* @param {string} type content type to be evaluated
82+
*/
83+
function isJsonMediaType(type) {
84+
return type.trim().toLowerCase().substr(type.length - 4) === 'json';
85+
}
86+
7087
/**
7188
* replaces path params with obvious indicator for filling values
7289
* (i.e. if any part of the path is surrounded in curly braces {})

templates/request/delete/delete.handlebars

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
{{/validateResponse}}
88
request({
99
url: '{{pathify path pathParams}}',
10+
{{#isJsonRepresentation contentType returnType}}
11+
json: true,
12+
{{/isJsonRepresentation}}
1013
{{#ifCond queryParameters queryApiKey}}
1114
qs: {
1215
{{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},

templates/request/get/get.handlebars

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
{{/validateResponse}}
88
request({
99
url: '{{pathify path pathParams}}',
10+
{{#isJsonRepresentation contentType returnType}}
11+
json: true,
12+
{{/isJsonRepresentation}}
1013
{{#ifCond queryParameters queryApiKey}}
1114
qs: {
1215
{{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},

templates/request/patch/patch.handlebars

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
{{/validateResponse}}
88
request({
99
url: '{{pathify path pathParams}}',
10+
{{#isJsonRepresentation contentType returnType}}
11+
json: true,
12+
{{/isJsonRepresentation}}
1013
{{#ifCond queryParameters queryApiKey}}
1114
qs: {
1215
{{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
@@ -20,10 +23,10 @@
2023
Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}}
2124
},
2225
{{#is contentType 'application/json'}}
23-
{{#if request}}json: {
26+
{{#if request}}body: {
2427
{{ request }}
2528
}
26-
{{else}}json: {
29+
{{else}}body: {
2730
{{#each bodyParameters}}
2831
{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}
2932
{{/each}}
@@ -122,7 +125,7 @@
122125
Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}}
123126
},
124127
{{#is contentType 'application/json'}}
125-
json: {
128+
body: {
126129
{{#each bodyParameters}}
127130
{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}
128131
{{/each}}

templates/request/post/post.handlebars

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
{{/validateResponse}}
88
request({
99
url: '{{pathify path pathParams}}',
10+
{{#isJsonRepresentation contentType returnType}}
11+
json: true,
12+
{{/isJsonRepresentation}}
1013
{{#ifCond queryParameters queryApiKey}}
1114
qs: {
1215
{{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
@@ -21,7 +24,7 @@
2124
Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}}
2225
},
2326
{{#is contentType 'application/json'}}
24-
{{#if request}}json: {{ request }}{{else}}json: {
27+
{{#if request}}body: {{ request }}{{else}}body: {
2528
{{#each bodyParameters}}
2629
{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}
2730
{{/each}}
@@ -120,7 +123,7 @@
120123
Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}}
121124
},
122125
{{#is contentType 'application/json'}}
123-
json: {
126+
body: {
124127
{{#each bodyParameters}}
125128
{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}
126129
{{/each}}

templates/request/put/put.handlebars

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
{{/validateResponse}}
88
request({
99
url: '{{pathify path pathParams}}',
10+
{{#isJsonRepresentation contentType returnType}}
11+
json: true,
12+
{{/isJsonRepresentation}}
1013
{{#ifCond queryParameters queryApiKey}}
1114
qs: {
1215
{{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}},
@@ -21,7 +24,7 @@
2124
Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}}
2225
},
2326
{{#is contentType 'application/json'}}
24-
{{#if request}}json: {{ request }}{{else}}json: {
27+
{{#if request}}body: {{ request }}{{else}}body: {
2528
{{#each bodyParameters}}
2629
{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}
2730
{{/each}}
@@ -120,7 +123,7 @@
120123
Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}}
121124
},
122125
{{#is contentType 'application/json'}}
123-
json: {
126+
body: {
124127
{{#each bodyParameters}}
125128
{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}
126129
{{/each}}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
var chai = require('chai');
3+
var request = require('request');
4+
var expect = chai.expect;
5+
6+
describe('/user', function() {
7+
describe('get', function() {
8+
it('should respond with 200 OK', function(done) {
9+
request({
10+
url: 'https://api.uber.com/user',
11+
json: true,
12+
method: 'GET',
13+
headers: {
14+
'Content-Type': 'application/json'
15+
}
16+
},
17+
function(error, res, body) {
18+
if (error) return done(error);
19+
20+
expect(res.statusCode).to.equal(200);
21+
22+
expect(body).to.equal(null); // non-json response or no schema
23+
done();
24+
});
25+
});
26+
27+
it('should respond with 400 NOT OK', function(done) {
28+
request({
29+
url: 'https://api.uber.com/user',
30+
json: true,
31+
method: 'GET',
32+
headers: {
33+
'Content-Type': 'application/json'
34+
}
35+
},
36+
function(error, res, body) {
37+
if (error) return done(error);
38+
39+
expect(res.statusCode).to.equal(400);
40+
41+
expect(body).to.equal(null); // non-json response or no schema
42+
done();
43+
});
44+
});
45+
46+
it('should respond with 500 SERVER ERROR', function(done) {
47+
request({
48+
url: 'https://api.uber.com/user',
49+
json: true,
50+
method: 'GET',
51+
headers: {
52+
'Content-Type': 'application/json'
53+
}
54+
},
55+
function(error, res, body) {
56+
if (error) return done(error);
57+
58+
expect(res.statusCode).to.equal(500);
59+
60+
expect(body).to.equal(null); // non-json response or no schema
61+
done();
62+
});
63+
});
64+
65+
});
66+
67+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
var chai = require('chai');
3+
var request = require('request');
4+
var expect = chai.expect;
5+
6+
describe('/user', function() {
7+
describe('post', function() {
8+
it('should respond with 200 OK', function(done) {
9+
request({
10+
url: 'https://api.uber.com/user',
11+
method: 'POST',
12+
headers: {
13+
'Content-Type': 'application/xml'
14+
},
15+
body: 'XML STRING GOES HERE'
16+
},
17+
function(error, res, body) {
18+
if (error) return done(error);
19+
20+
expect(res.statusCode).to.equal(200);
21+
22+
expect(body).to.equal(null); // non-json response or no schema
23+
done();
24+
});
25+
});
26+
27+
it('should respond with 400 NOT OK', function(done) {
28+
request({
29+
url: 'https://api.uber.com/user',
30+
method: 'POST',
31+
headers: {
32+
'Content-Type': 'application/xml'
33+
},
34+
body: 'XML STRING GOES HERE'
35+
},
36+
function(error, res, body) {
37+
if (error) return done(error);
38+
39+
expect(res.statusCode).to.equal(400);
40+
41+
expect(body).to.equal(null); // non-json response or no schema
42+
done();
43+
});
44+
});
45+
46+
it('should respond with 500 SERVER ERROR', function(done) {
47+
request({
48+
url: 'https://api.uber.com/user',
49+
method: 'POST',
50+
headers: {
51+
'Content-Type': 'application/xml'
52+
},
53+
body: 'XML STRING GOES HERE'
54+
},
55+
function(error, res, body) {
56+
if (error) return done(error);
57+
58+
expect(res.statusCode).to.equal(500);
59+
60+
expect(body).to.equal(null); // non-json response or no schema
61+
done();
62+
});
63+
});
64+
65+
});
66+
67+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"swagger": "2.0",
3+
"info": {
4+
"version": "0.0.0",
5+
"title": "Simple API"
6+
},
7+
"host": "api.uber.com",
8+
"schemes": [
9+
"https"
10+
],
11+
"paths": {
12+
"/user": {
13+
"get": {
14+
"responses": {
15+
"200": {
16+
"description": "OK"
17+
},
18+
"400": {
19+
"description": "NOT OK"
20+
},
21+
"500": {
22+
"description": "SERVER ERROR"
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)