Skip to content

Commit 3d69abf

Browse files
committed
Added option to set preferred request body content-type
1 parent 35ece65 commit 3d69abf

File tree

2 files changed

+198
-11
lines changed

2 files changed

+198
-11
lines changed

libV2/schemaUtils.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,8 @@ let QUERYPARAM = 'query',
16451645
formDataRequestBody,
16461646
rawModeRequestBody;
16471647

1648-
const { preferredRequestBodyType } = context.computedOptions;
1648+
const { preferredRequestBodyType: optionRequestBodyType } = context.computedOptions,
1649+
preferredRequestBodyType = optionRequestBodyType || 'x-www-form-urlencoded';
16491650

16501651
if (!requestBody) {
16511652
return requestBody;
@@ -1667,12 +1668,21 @@ let QUERYPARAM = 'query',
16671668
for (const contentType in requestContent) {
16681669
if (contentType === URLENCODED) {
16691670
encodedRequestBody = resolveUrlEncodedRequestBodyForPostmanRequest(context, requestContent[contentType]);
1671+
if (preferredRequestBodyType === 'first-listed') {
1672+
return encodedRequestBody;
1673+
}
16701674
}
16711675
else if (contentType === FORM_DATA) {
16721676
formDataRequestBody = resolveFormDataRequestBodyForPostmanRequest(context, requestContent[contentType]);
1677+
if (preferredRequestBodyType === 'first-listed') {
1678+
return formDataRequestBody;
1679+
}
16731680
}
16741681
else {
16751682
rawModeRequestBody = resolveRawModeRequestBodyForPostmanRequest(context, requestContent);
1683+
if (preferredRequestBodyType === 'first-listed') {
1684+
return rawModeRequestBody;
1685+
}
16761686
}
16771687
}
16781688

@@ -1689,16 +1699,8 @@ let QUERYPARAM = 'query',
16891699
}
16901700
}
16911701

1692-
// If preferredRequestBodyType is not provided, return the first available request body
1693-
if (encodedRequestBody) {
1694-
return encodedRequestBody;
1695-
}
1696-
else if (formDataRequestBody) {
1697-
return formDataRequestBody;
1698-
}
1699-
else {
1700-
return rawModeRequestBody;
1701-
}
1702+
// Fallback
1703+
return rawModeRequestBody;
17021704
},
17031705

17041706
resolvePathItemParams = (context, operationParam, pathParam) => {
@@ -2267,6 +2269,7 @@ module.exports = {
22672269
},
22682270

22692271
resolveResponseForPostmanRequest,
2272+
resolveRequestBodyForPostmanRequest,
22702273
resolveRefFromSchema,
22712274
resolveSchema
22722275
};

test/unit/schemaUtilsV2.test.js

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
const {
2+
resolveRequestBodyForPostmanRequest
3+
} = require('../../libV2/schemaUtils.js'),
4+
concreteUtils = require('../../lib/30XUtils/schemaUtils30X'),
5+
expect = require('chai').expect,
6+
7+
// Example operationItem
8+
operationItem = {
9+
put: {
10+
'tags': [
11+
'Administration: Users'
12+
],
13+
'summary': 'Create or Update User',
14+
'operationId': 'User',
15+
'requestBody': {
16+
'content': {
17+
'application/json': {
18+
'schema': {
19+
'type': 'object',
20+
'properties': {
21+
'foo': {
22+
'type': 'string'
23+
}
24+
}
25+
}
26+
},
27+
'text/json': {
28+
'schema': {
29+
'type': 'object',
30+
'properties': {
31+
'foo': {
32+
'type': 'string'
33+
}
34+
}
35+
}
36+
},
37+
'application/xml': {
38+
'schema': {
39+
'type': 'object',
40+
'properties': {
41+
'foo': {
42+
'type': 'string'
43+
}
44+
}
45+
}
46+
},
47+
'multipart/form-data': {
48+
'schema': {
49+
'type': 'object',
50+
'properties': {
51+
'foo': {
52+
'type': 'string'
53+
}
54+
}
55+
}
56+
},
57+
'application/x-www-form-urlencoded': {
58+
'schema': {
59+
'type': 'object',
60+
'properties': {
61+
'foo': {
62+
'type': 'string'
63+
}
64+
}
65+
}
66+
}
67+
},
68+
'description': 'The User request.',
69+
'required': true
70+
},
71+
'responses': {
72+
'200': {
73+
'description': 'OK',
74+
'content': {
75+
'application/json': {
76+
'schema': {
77+
'type': 'object',
78+
'properties': {}
79+
}
80+
}
81+
}
82+
}
83+
}
84+
}
85+
};
86+
87+
describe('resolveRequestBodyForPostmanRequest function', function () {
88+
89+
it('should return encoded request body when preferredRequestBodyType is not set', function () {
90+
const contextTest = {
91+
concreteUtils,
92+
schemaCache: {},
93+
schemaFakerCache: {},
94+
computedOptions: {
95+
parametersResolution: 'schema'
96+
}
97+
},
98+
operationItemTest = operationItem.put,
99+
result = resolveRequestBodyForPostmanRequest(contextTest, operationItemTest);
100+
101+
expect(result.body.mode).to.equal('urlencoded');
102+
});
103+
104+
it('should return raw request body as fallback when preferredRequestBodyType is not a valid option', function () {
105+
const contextTest = {
106+
concreteUtils,
107+
schemaCache: {},
108+
schemaFakerCache: {},
109+
computedOptions: {
110+
parametersResolution: 'schema',
111+
preferredRequestBodyType: 'foo-bar'
112+
}
113+
},
114+
operationItemTest = operationItem.put,
115+
result = resolveRequestBodyForPostmanRequest(contextTest, operationItemTest);
116+
117+
expect(result.body.mode).to.equal('raw');
118+
});
119+
120+
it('should return encoded request body when preferredRequestBodyType is x-www-form-urlencoded', function () {
121+
const contextTest = {
122+
concreteUtils,
123+
schemaCache: {},
124+
schemaFakerCache: {},
125+
computedOptions: {
126+
parametersResolution: 'schema',
127+
preferredRequestBodyType: 'x-www-form-urlencoded'
128+
}
129+
},
130+
operationItemTest = operationItem.put,
131+
result = resolveRequestBodyForPostmanRequest(contextTest, operationItemTest);
132+
133+
expect(result.body.mode).to.equal('urlencoded');
134+
});
135+
136+
it('should return form data request body when preferredRequestBodyType is form-data', function () {
137+
const contextTest = {
138+
concreteUtils,
139+
schemaCache: {},
140+
schemaFakerCache: {},
141+
computedOptions: {
142+
parametersResolution: 'schema',
143+
preferredRequestBodyType: 'form-data'
144+
}
145+
},
146+
operationItemTest = operationItem.put,
147+
result = resolveRequestBodyForPostmanRequest(contextTest, operationItemTest);
148+
149+
expect(result.body.mode).to.equal('formdata');
150+
});
151+
152+
it('should return raw request body when preferredRequestBodyType is raw', function () {
153+
const contextTest = {
154+
concreteUtils,
155+
schemaCache: {},
156+
schemaFakerCache: {},
157+
computedOptions: {
158+
parametersResolution: 'schema',
159+
preferredRequestBodyType: 'raw'
160+
}
161+
},
162+
operationItemTest = operationItem.put,
163+
result = resolveRequestBodyForPostmanRequest(contextTest, operationItemTest);
164+
165+
expect(result.body.mode).to.equal('raw');
166+
});
167+
168+
it('should return raw request body when preferredRequestBodyType is first-listed', function () {
169+
const contextTest = {
170+
concreteUtils,
171+
schemaCache: {},
172+
schemaFakerCache: {},
173+
computedOptions: {
174+
parametersResolution: 'schema',
175+
preferredRequestBodyType: 'first-listed'
176+
}
177+
},
178+
operationItemTest = operationItem.put,
179+
result = resolveRequestBodyForPostmanRequest(contextTest, operationItemTest);
180+
181+
expect(result.body.mode).to.equal('raw');
182+
});
183+
184+
});

0 commit comments

Comments
 (0)