Skip to content

Commit 64cc599

Browse files
authored
Merge pull request #698 from postmanlabs/feature/formdata-content-type
Add support for contentType field for Formdata request bodies
2 parents a97d6f8 + 881f19b commit 64cc599

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

lib/schemaUtils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,7 @@ module.exports = {
18961896
updateOptions = {},
18971897
reqBody = new sdk.RequestBody(),
18981898
contentHeader,
1899+
contentTypes = {},
18991900
rDataMode,
19001901
params,
19011902
encoding,
@@ -2018,6 +2019,10 @@ module.exports = {
20182019
REQUEST_TYPE.ROOT, PARAMETER_SOURCE.REQUEST, components, options, schemaCache));
20192020
}
20202021
});
2022+
2023+
if (typeof _.get(encoding, `[${key}].contentType`) === 'string') {
2024+
contentTypes[key] = encoding[key].contentType;
2025+
}
20212026
}
20222027
// Collection v2.1 schema allows form param value to be only string
20232028
if (typeof value !== 'string') {
@@ -2055,6 +2060,9 @@ module.exports = {
20552060
});
20562061
}
20572062
param.description = description;
2063+
if (contentTypes[key]) {
2064+
param.contentType = contentTypes[key];
2065+
}
20582066
paramArray.push(param);
20592067
});
20602068
updateOptions = {

libV2/schemaUtils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,7 @@ let QUERYPARAM = 'query',
12341234
resolveFormDataRequestBodyForPostmanRequest = (context, requestBodyContent) => {
12351235
let bodyData = '',
12361236
formDataParams = [],
1237+
encoding = {},
12371238
requestBodyData = {
12381239
mode: 'formdata',
12391240
formdata: formDataParams
@@ -1244,9 +1245,11 @@ let QUERYPARAM = 'query',
12441245
}
12451246

12461247
bodyData = resolveRequestBodyData(context, requestBodyContent.schema);
1248+
encoding = _.get(requestBodyContent, 'encoding', {});
12471249

12481250
_.forOwn(bodyData, (value, key) => {
12491251
let requestBodySchema,
1252+
contentType = null,
12501253
paramSchema,
12511254
description,
12521255
param;
@@ -1263,6 +1266,10 @@ let QUERYPARAM = 'query',
12631266
_.indexOf(requestBodySchema.required, key) !== -1;
12641267
description = getParameterDescription(paramSchema);
12651268

1269+
if (typeof _.get(encoding, `[${key}].contentType`) === 'string') {
1270+
contentType = encoding[key].contentType;
1271+
}
1272+
12661273
// TODO: Add handling for headers from encoding
12671274

12681275
if (paramSchema && paramSchema.type === 'binary') {
@@ -1281,6 +1288,9 @@ let QUERYPARAM = 'query',
12811288
}
12821289

12831290
param.description = description;
1291+
if (contentType) {
1292+
param.contentType = contentType;
1293+
}
12841294

12851295
formDataParams.push(param);
12861296
});

test/data/valid_openapi/required_in_parameters.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@
9898
"type": "string"
9999
}
100100
}
101+
},
102+
"encoding": {
103+
"formParam1": {
104+
"contentType": "application/xml"
105+
},
106+
"formParam2": {
107+
"contentType": "application/js"
108+
}
101109
}
102110
}
103111
}

test/unit/convertV2.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,8 @@ describe('The convert v2 Function', function() {
760760
request = requests[2].request;
761761
expect(request.body.formdata[0].description.content).to.equal('(Required) Description of formParam1');
762762
expect(request.body.formdata[1].description.content).to.equal('Description of formParam2');
763+
expect(request.body.formdata[0].contentType).to.equal('application/xml');
764+
expect(request.body.formdata[1].contentType).to.equal('application/js');
763765

764766
// POST /pets
765767
// RequestBody: application/x-www-form-urlencoded

test/unit/util.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,6 +1899,11 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
18991899
}
19001900
},
19011901
required: ['array']
1902+
},
1903+
encoding: {
1904+
file: {
1905+
contentType: 'application/binary'
1906+
}
19021907
}
19031908
}
19041909
}
@@ -1909,6 +1914,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
19091914
expect(resultBody[0].key).to.equal('array');
19101915
expect(resultBody[1].key).to.equal('file');
19111916
expect(resultBody[1].type).to.equal('file');
1917+
expect(resultBody[1].contentType).to.equal('application/binary');
19121918
expect(result.contentHeader).to.deep.include(
19131919
{ key: 'Content-Type', value: 'multipart/form-data' });
19141920
done();

0 commit comments

Comments
 (0)