Skip to content

Commit bb5f6ff

Browse files
authored
Merge pull request #409 from postmanlabs/issue-8635
Use Json.parse for all json like application types
2 parents a061f37 + bcf00d6 commit bb5f6ff

File tree

14 files changed

+268
-7
lines changed

14 files changed

+268
-7
lines changed

codegens/js-fetch/lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ function parseFormData (body, trim) {
6565
*/
6666
function parseRawBody (body, trim, contentType) {
6767
var bodySnippet = 'var raw = ';
68-
if (contentType === 'application/json') {
68+
// Match any application type whose underlying structure is json
69+
// For example application/vnd.api+json
70+
// All of them have +json as suffix
71+
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
6972
try {
7073
let jsonBody = JSON.parse(body);
7174
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody)});\n`;

codegens/js-fetch/test/unit/convert.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,40 @@ describe('js-fetch convert function for test collection', function () {
4949
});
5050
});
5151

52+
it('should use JSON.parse if the content-type is application/vnd.api+json', function () {
53+
request = new sdk.Request({
54+
'method': 'POST',
55+
'header': [
56+
{
57+
'key': 'Content-Type',
58+
'value': 'application/vnd.api+json'
59+
}
60+
],
61+
'body': {
62+
'mode': 'raw',
63+
'raw': '{"data": {"hello": "world"} }'
64+
},
65+
'url': {
66+
'raw': 'https://postman-echo.com/get',
67+
'protocol': 'https',
68+
'host': [
69+
'postman-echo',
70+
'com'
71+
],
72+
'path': [
73+
'get'
74+
]
75+
}
76+
});
77+
convert(request, {}, function (error, snippet) {
78+
if (error) {
79+
expect.fail(null, null, error);
80+
}
81+
expect(snippet).to.be.a('string');
82+
expect(snippet).to.contain('var raw = JSON.stringify({"data":{"hello":"world"}});');
83+
});
84+
});
85+
5286
it('should return snippet with redirect property set to manual for ' +
5387
'no follow redirect', function () {
5488
request = new sdk.Request(mainCollection.item[0].request);

codegens/js-jquery/lib/util/parseBody.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ module.exports = function (request, trimRequestBody, indentation, contentType) {
2020
switch (request.body.mode) {
2121
case 'raw':
2222
if (!_.isEmpty(request.body[request.body.mode])) {
23-
if (contentType === 'application/json') {
23+
// Match any application type whose underlying structure is json
24+
// For example application/vnd.api+json
25+
// All of them have +json as suffix
26+
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
2427
// eslint-disable-next-line max-depth
2528
try {
2629
let jsonBody = JSON.parse(request.body[request.body.mode]);

codegens/js-jquery/test/unit/converter.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,40 @@ describe('jQuery converter', function () {
7373
});
7474
});
7575

76+
it('should use JSON.parse if the content-type is application/vnd.api+json', function () {
77+
let request = new sdk.Request({
78+
'method': 'POST',
79+
'header': [
80+
{
81+
'key': 'Content-Type',
82+
'value': 'application/vnd.api+json'
83+
}
84+
],
85+
'body': {
86+
'mode': 'raw',
87+
'raw': '{"data": {"hello": "world"} }'
88+
},
89+
'url': {
90+
'raw': 'https://postman-echo.com/get',
91+
'protocol': 'https',
92+
'host': [
93+
'postman-echo',
94+
'com'
95+
],
96+
'path': [
97+
'get'
98+
]
99+
}
100+
});
101+
convert(request, {}, function (error, snippet) {
102+
if (error) {
103+
expect.fail(null, null, error);
104+
}
105+
expect(snippet).to.be.a('string');
106+
expect(snippet).to.contain('"data": JSON.stringify({"data":{"hello":"world"}})');
107+
});
108+
});
109+
76110
it('should trim header keys and not trim header values', function () {
77111
var request = new sdk.Request({
78112
'method': 'GET',

codegens/js-xhr/lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ function parseURLEncodedBody (body) {
3030
*/
3131
function parseRawBody (body, trim, contentType) {
3232
var bodySnippet = 'var data = ';
33-
if (contentType === 'application/json') {
33+
// Match any application type whose underlying structure is json
34+
// For example application/vnd.api+json
35+
// All of them have +json as suffix
36+
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
3437
try {
3538
let jsonBody = JSON.parse(body);
3639
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody)});\n`;

codegens/js-xhr/test/unit/convert.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('js-xhr convert function', function () {
3434
'" value_containing_whitespaces ")');
3535
});
3636
});
37+
3738
it('should include JSON.stringify in the snippet for raw json bodies', function () {
3839
var request = new sdk.Request({
3940
'method': 'POST',
@@ -67,6 +68,41 @@ describe('js-xhr convert function', function () {
6768
expect(snippet).to.include('var data = JSON.stringify({"json":"Test-Test"})');
6869
});
6970
});
71+
72+
it('should use JSON.parse if the content-type is application/vnd.api+json', function () {
73+
let request = new sdk.Request({
74+
'method': 'POST',
75+
'header': [
76+
{
77+
'key': 'Content-Type',
78+
'value': 'application/vnd.api+json'
79+
}
80+
],
81+
'body': {
82+
'mode': 'raw',
83+
'raw': '{"data": {"hello": "world"} }'
84+
},
85+
'url': {
86+
'raw': 'https://postman-echo.com/get',
87+
'protocol': 'https',
88+
'host': [
89+
'postman-echo',
90+
'com'
91+
],
92+
'path': [
93+
'get'
94+
]
95+
}
96+
});
97+
convert(request, {}, function (error, snippet) {
98+
if (error) {
99+
expect.fail(null, null, error);
100+
}
101+
expect(snippet).to.be.a('string');
102+
expect(snippet).to.contain('var data = JSON.stringify({"data":{"hello":"world"}});');
103+
});
104+
});
105+
70106
it('should generate snippets for no files in form data', function () {
71107
var request = new sdk.Request({
72108
'method': 'POST',

codegens/nodejs-axios/lib/parseRequest.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ function parseFormData (body, trim, ES6_enabled) {
8080
function parseRawBody (body, trim, contentType, ES6_enabled) {
8181
var varDeclare = ES6_enabled ? 'let' : 'var',
8282
bodySnippet = varDeclare + ' data = ';
83-
if (contentType === 'application/json') {
83+
// Match any application type whose underlying structure is json
84+
// For example application/vnd.api+json
85+
// All of them have +json as suffix
86+
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
8487
try {
8588
let jsonBody = JSON.parse(body);
8689
bodySnippet += `JSON.stringify(${JSON.stringify(jsonBody)});\n`;

codegens/nodejs-axios/test/unit/snippet.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,40 @@ describe('nodejs-axios convert function', function () {
5050
});
5151
});
5252

53+
it('should use JSON.parse if the content-type is application/vnd.api+json', function () {
54+
request = new sdk.Request({
55+
'method': 'POST',
56+
'header': [
57+
{
58+
'key': 'Content-Type',
59+
'value': 'application/vnd.api+json'
60+
}
61+
],
62+
'body': {
63+
'mode': 'raw',
64+
'raw': '{"data": {"hello": "world"} }'
65+
},
66+
'url': {
67+
'raw': 'https://postman-echo.com/get',
68+
'protocol': 'https',
69+
'host': [
70+
'postman-echo',
71+
'com'
72+
],
73+
'path': [
74+
'get'
75+
]
76+
}
77+
});
78+
convert(request, {}, function (error, snippet) {
79+
if (error) {
80+
expect.fail(null, null, error);
81+
}
82+
expect(snippet).to.be.a('string');
83+
expect(snippet).to.contain('var data = JSON.stringify({"data":{"hello":"world"}});');
84+
});
85+
});
86+
5387
it('should return snippet with maxRedirects property set to ' +
5488
'0 for no follow redirect', function () {
5589
request = new sdk.Request(mainCollection.item[0].request);

codegens/nodejs-native/lib/parseRequest.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ function parseBody (requestbody, indentString, trimBody, contentType) {
8585
if (requestbody) {
8686
switch (requestbody.mode) {
8787
case 'raw':
88-
if (contentType === 'application/json') {
88+
// Match any application type whose underlying structure is json
89+
// For example application/vnd.api+json
90+
// All of them have +json as suffix
91+
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) {
8992
try {
9093
let jsonBody = JSON.parse(requestbody[requestbody.mode]);
9194
return `JSON.stringify(${JSON.stringify(jsonBody)})`;

codegens/nodejs-native/test/unit/snippet.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,40 @@ describe('nodejs-native convert function', function () {
9797
});
9898
});
9999

100+
it('should use JSON.parse if the content-type is application/vnd.api+json', function () {
101+
let request = new sdk.Request({
102+
'method': 'POST',
103+
'header': [
104+
{
105+
'key': 'Content-Type',
106+
'value': 'application/vnd.api+json'
107+
}
108+
],
109+
'body': {
110+
'mode': 'raw',
111+
'raw': '{"data": {"hello": "world"} }'
112+
},
113+
'url': {
114+
'raw': 'https://postman-echo.com/get',
115+
'protocol': 'https',
116+
'host': [
117+
'postman-echo',
118+
'com'
119+
],
120+
'path': [
121+
'get'
122+
]
123+
}
124+
});
125+
convert(request, {}, function (error, snippet) {
126+
if (error) {
127+
expect.fail(null, null, error);
128+
}
129+
expect(snippet).to.be.a('string');
130+
expect(snippet).to.contain('var postData = JSON.stringify({"data":{"hello":"world"}});');
131+
});
132+
});
133+
100134
it('should trim header keys and not trim header values', function () {
101135
var request = new sdk.Request({
102136
'method': 'GET',

0 commit comments

Comments
 (0)