Skip to content

Commit 0a4393b

Browse files
authored
Merge pull request #600 from postmanlabs/fix/formatingBodyRawOutputWhenIsNotAnObject
Changing the format of the body.raw when the requestBody is not an ob…
2 parents 808eefc + 132987e commit 0a4393b

File tree

4 files changed

+86
-9
lines changed

4 files changed

+86
-9
lines changed

lib/schemaUtils.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/sc
2828
APP_JSON = 'application/json',
2929
APP_JS = 'application/javascript',
3030
TEXT_XML = 'text/xml',
31+
APP_XML = 'application/xml',
3132
TEXT_PLAIN = 'text/plain',
3233
TEXT_HTML = 'text/html',
3334
FORM_DATA = 'multipart/form-data',
@@ -1914,6 +1915,7 @@ module.exports = {
19141915
else if (contentObj.hasOwnProperty(APP_JSON)) { bodyType = APP_JSON; }
19151916
else if (contentObj.hasOwnProperty(TEXT_HTML)) { bodyType = TEXT_HTML; }
19161917
else if (contentObj.hasOwnProperty(TEXT_PLAIN)) { bodyType = TEXT_PLAIN; }
1918+
else if (contentObj.hasOwnProperty(APP_XML)) { bodyType = APP_XML; }
19171919
else if (contentObj.hasOwnProperty(TEXT_XML)) { bodyType = TEXT_XML; }
19181920
else {
19191921
// take the first property it has
@@ -1934,12 +1936,29 @@ module.exports = {
19341936
};
19351937
}
19361938
else {
1939+
let getXmlVersionContent = (bodyContent) => {
1940+
const regExp = new RegExp('([<\\?xml]+[\\s{1,}]+[version="\\d.\\d"]+[\\sencoding="]+.{1,15}"\\?>)');
1941+
let xmlBody = bodyContent;
1942+
1943+
if (!bodyContent.match(regExp)) {
1944+
const versionContent = '<?xml version="1.0" encoding="UTF-8"?>\n';
1945+
xmlBody = versionContent + xmlBody;
1946+
}
1947+
return xmlBody;
1948+
};
1949+
19371950
bodyData = this.convertToPmBodyData(contentObj[bodyType], requestType, bodyType,
19381951
PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache);
19391952

1953+
bodyData = (bodyType === TEXT_XML || bodyType === APP_XML) ?
1954+
getXmlVersionContent(bodyData) :
1955+
bodyData;
1956+
19401957
updateOptions = {
19411958
mode: rDataMode,
1942-
raw: JSON.stringify(bodyData, null, options.indentCharacter)
1959+
raw: bodyType !== APP_JSON ?
1960+
bodyData.toString() :
1961+
JSON.stringify(bodyData, null, options.indentCharacter)
19431962
};
19441963
}
19451964

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
openapi: 3.0.3
2+
info:
3+
title: My API
4+
version: 1.0.0
5+
contact: {}
6+
servers:
7+
- url: "https://api.server.test/v1"
8+
paths:
9+
/test:
10+
post:
11+
summary: /test
12+
description: /test
13+
operationId: test
14+
requestBody:
15+
content:
16+
text/xml:
17+
example:
18+
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope">
19+
<soap:Body>
20+
<NumberToWords xmlns="http://www.dataaccess.com/webservicesserver">
21+
<ubiNum>500</ubiNum>
22+
</NumberToWords>
23+
</soap:Body>
24+
</soap:Envelope>
25+
responses:
26+
"200":
27+
description: OK
28+
content:
29+
application/json:
30+
examples:
31+
OK:
32+
value:
33+
Data: Postman
34+
tags: []

test/unit/base.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ describe('CONVERT FUNCTION TESTS ', function() {
5151
deepObjectLengthProperty = path.join(__dirname, VALID_OPENAPI_PATH, '/deepObjectLengthProperty.yaml'),
5252
valuePropInExample = path.join(__dirname, VALID_OPENAPI_PATH, '/valuePropInExample.yaml'),
5353
petstoreParamExample = path.join(__dirname, VALID_OPENAPI_PATH, '/petstoreParamExample.yaml'),
54+
xmlrequestBody = path.join(__dirname, VALID_OPENAPI_PATH, '/xmlExample.yaml'),
5455
queryParamWithEnumResolveAsExample =
5556
path.join(__dirname, VALID_OPENAPI_PATH, '/query_param_with_enum_resolve_as_example.json'),
5657
formDataParamDescription = path.join(__dirname, VALID_OPENAPI_PATH, '/form_data_param_description.yaml'),
@@ -1146,6 +1147,25 @@ describe('CONVERT FUNCTION TESTS ', function() {
11461147
});
11471148
});
11481149

1150+
it('Should convert xml request body correctly', function(done) {
1151+
const openapi = fs.readFileSync(xmlrequestBody, 'utf8');
1152+
Converter.convert({ type: 'string', data: openapi },
1153+
{ schemaFaker: true }, (err, conversionResult) => {
1154+
expect(err).to.be.null;
1155+
expect(conversionResult.result).to.equal(true);
1156+
expect(conversionResult.output[0].data.item[0].request.body.raw)
1157+
.to.equal(
1158+
'<?xml version="1.0" encoding="UTF-8"?>\n' +
1159+
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope">' +
1160+
' <soap:Body> <NumberToWords ' +
1161+
'xmlns="http://www.dataaccess.com/webservicesserver"> ' +
1162+
'<ubiNum>500</ubiNum> </NumberToWords> </soap:Body> ' +
1163+
'</soap:Envelope>'
1164+
);
1165+
done();
1166+
});
1167+
});
1168+
11491169
it('[Github #518]- integer query params with enum values get default value of NaN' +
11501170
descriptionInBodyParams, function(done) {
11511171
var openapi = fs.readFileSync(queryParamWithEnumResolveAsExample, 'utf8');

test/unit/util.test.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
16641664
examples: {
16651665
xml: {
16661666
summary: 'A list containing two items',
1667-
value: 'text/plain description'
1667+
value: '<AnXMLObject>test</AnXMLObject>'
16681668
}
16691669
}
16701670
}
@@ -1675,7 +1675,9 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
16751675
exampleParametersResolution: 'example'
16761676
});
16771677
resultBody = (result.body.raw);
1678-
expect(resultBody).to.equal('"text/plain description"');
1678+
expect(resultBody).to.equal(
1679+
'<?xml version="1.0" encoding="UTF-8"?>\n<AnXMLObject>test</AnXMLObject>'
1680+
);
16791681
expect(result.contentHeader).to.deep.include(
16801682
{ key: 'Content-Type', value: 'text/xml' });
16811683
expect(result.body.options.raw.language).to.equal('xml');
@@ -1697,7 +1699,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
16971699
});
16981700
resultBody = result.body.raw;
16991701
expect(resultBody)
1700-
.to.equal('"text/plain description"');
1702+
.to.equal('text/plain description');
17011703
expect(result.contentHeader).to.deep.include(
17021704
{ key: 'Content-Type', value: 'text/plain' });
17031705
done();
@@ -1717,7 +1719,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
17171719
exampleParametersResolution: 'example'
17181720
});
17191721
resultBody = (result.body.raw);
1720-
expect(resultBody).to.equal('"<html><body><ul><li>item 1</li><li>item 2</li></ul></body></html>"');
1722+
expect(resultBody).to.equal('<html><body><ul><li>item 1</li><li>item 2</li></ul></body></html>');
17211723
expect(result.contentHeader).to.deep.include(
17221724
{ key: 'Content-Type', value: 'text/html' });
17231725
done();
@@ -1839,7 +1841,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
18391841
examples: {
18401842
xml: {
18411843
summary: 'A list containing two items',
1842-
value: 'text/plain description'
1844+
value: '<AnXMLObject>test</AnXMLObject>'
18431845
}
18441846
}
18451847
}
@@ -1850,7 +1852,9 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
18501852
requestParametersResolution: 'example'
18511853
});
18521854
resultBody = (result.body.raw);
1853-
expect(resultBody).to.equal('"text/plain description"');
1855+
expect(resultBody).to.equal(
1856+
'<?xml version="1.0" encoding="UTF-8"?>\n<AnXMLObject>test</AnXMLObject>'
1857+
);
18541858
expect(result.contentHeader).to.deep.include(
18551859
{ key: 'Content-Type', value: 'text/xml' });
18561860
expect(result.body.options.raw.language).to.equal('xml');
@@ -1868,7 +1872,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
18681872
result, resultBody;
18691873
result = SchemaUtils.convertToPmBody(requestBody);
18701874
resultBody = result.body.raw;
1871-
expect(resultBody).to.equal('"text/plain description"');
1875+
expect(resultBody).to.equal('text/plain description');
18721876
expect(result.contentHeader).to.deep.include(
18731877
{ key: 'Content-Type', value: 'text/plain' });
18741878
done();
@@ -1885,7 +1889,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
18851889
result, resultBody;
18861890
result = SchemaUtils.convertToPmBody(requestBody);
18871891
resultBody = (result.body.raw);
1888-
expect(resultBody).to.equal('"<html><body><ul><li>item 1</li><li>item 2</li></ul></body></html>"');
1892+
expect(resultBody).to.equal('<html><body><ul><li>item 1</li><li>item 2</li></ul></body></html>');
18891893
expect(result.contentHeader).to.deep.include(
18901894
{ key: 'Content-Type', value: 'text/html' });
18911895
done();

0 commit comments

Comments
 (0)