Skip to content

Commit b402d96

Browse files
committed
Adding xml version data to body if not present
- When content is text/xml and the provided body or example does not contain the version data it will be added at the beginning.
1 parent 3cce44a commit b402d96

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

lib/schemaUtils.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,18 @@ module.exports = {
19321932
else {
19331933
bodyData = this.convertToPmBodyData(contentObj[bodyType], requestType, bodyType,
19341934
PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache);
1935-
1935+
let getXmlVersionContent = (bodyContent) => {
1936+
const regExp = new RegExp('([<\\?xml]+[\\s{1,}]+[version="\\d.\\d"]+[\\sencoding="]+.{1,15}"\\?>)');
1937+
let xmlBody = bodyContent;
1938+
if (!bodyContent.match(regExp)) {
1939+
const versionContent = '<?xml version="1.0" encoding="utf-8"?>';
1940+
xmlBody = versionContent + xmlBody;
1941+
}
1942+
return xmlBody;
1943+
};
1944+
bodyData = bodyType === TEXT_XML ?
1945+
getXmlVersionContent(bodyData) :
1946+
bodyData;
19361947
updateOptions = {
19371948
mode: rDataMode,
19381949
raw: bodyType !== APP_JSON ?

test/unit/base.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,9 +1152,13 @@ describe('CONVERT FUNCTION TESTS ', function() {
11521152
expect(conversionResult.result).to.equal(true);
11531153
expect(conversionResult.output[0].data.item[0].request.body.raw)
11541154
.to.equal(
1155+
'<?xml version="1.0" encoding="utf-8"?>' +
11551156
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope">' +
1156-
' <soap:Body> <NumberToWords xmlns="http://www.dataaccess.com/webservicesserver">' +
1157-
' <ubiNum>500</ubiNum> </NumberToWords> </soap:Body> </soap:Envelope>');
1157+
' <soap:Body> <NumberToWords ' +
1158+
'xmlns="http://www.dataaccess.com/webservicesserver"> ' +
1159+
'<ubiNum>500</ubiNum> </NumberToWords> </soap:Body> ' +
1160+
'</soap:Envelope>'
1161+
);
11581162
done();
11591163
});
11601164
});

test/unit/util.test.js

Lines changed: 8 additions & 4 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"?><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');
@@ -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"?><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');

0 commit comments

Comments
 (0)