Skip to content

Commit 35dcf31

Browse files
author
Vishal Shingala
committed
Fixed issue where for some of request body with JSON family headers were not correctly generated
1 parent 4786d4d commit 35dcf31

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

lib/schemaUtils.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,26 +2015,28 @@ module.exports = {
20152015
}
20162016
else {
20172017
let getXmlVersionContent = (bodyContent) => {
2018-
const regExp = new RegExp('([<\\?xml]+[\\s{1,}]+[version="\\d.\\d"]+[\\sencoding="]+.{1,15}"\\?>)');
2019-
let xmlBody = bodyContent;
2018+
const regExp = new RegExp('([<\\?xml]+[\\s{1,}]+[version="\\d.\\d"]+[\\sencoding="]+.{1,15}"\\?>)');
2019+
let xmlBody = bodyContent;
20202020

2021-
if (!bodyContent.match(regExp)) {
2022-
const versionContent = '<?xml version="1.0" encoding="UTF-8"?>\n';
2023-
xmlBody = versionContent + xmlBody;
2024-
}
2025-
return xmlBody;
2026-
};
2021+
if (!bodyContent.match(regExp)) {
2022+
const versionContent = '<?xml version="1.0" encoding="UTF-8"?>\n';
2023+
xmlBody = versionContent + xmlBody;
2024+
}
2025+
return xmlBody;
2026+
},
2027+
headerFamily;
20272028

20282029
bodyData = this.convertToPmBodyData(contentObj[bodyType], requestType, bodyType,
20292030
PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache);
20302031

2031-
bodyData = (bodyType === TEXT_XML || bodyType === APP_XML) ?
2032+
headerFamily = this.getHeaderFamily(bodyType);
2033+
bodyData = (bodyType === TEXT_XML || bodyType === APP_XML || headerFamily === HEADER_TYPE.XML) ?
20322034
getXmlVersionContent(bodyData) :
20332035
bodyData;
20342036

20352037
updateOptions = {
20362038
mode: rDataMode,
2037-
raw: bodyType !== APP_JSON ?
2039+
raw: !_.isObject(bodyData) && _.isFunction(_.get(bodyData, 'toString')) ?
20382040
bodyData.toString() :
20392041
JSON.stringify(bodyData, null, options.indentCharacter)
20402042
};

test/unit/util.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,70 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
19281928
expect(result.body.mode).to.equal('file');
19291929
done();
19301930
});
1931+
it(' application/vnd.api+json (headers with different structure but still of JSON family)', function(done) {
1932+
var requestBody = {
1933+
description: 'body description',
1934+
content: {
1935+
'application/vnd.api+json': {
1936+
'schema': {
1937+
type: 'object',
1938+
required: [
1939+
'id',
1940+
'name'
1941+
],
1942+
properties: {
1943+
id: {
1944+
type: 'integer',
1945+
format: 'int64'
1946+
},
1947+
name: {
1948+
type: 'string'
1949+
},
1950+
neglect: { // this will be neglected since schemaFaker does not process
1951+
type: 'string',
1952+
format: 'binary'
1953+
}
1954+
}
1955+
}
1956+
}
1957+
}
1958+
},
1959+
result, resultBody;
1960+
result = SchemaUtils.convertToPmBody(requestBody);
1961+
resultBody = JSON.parse(result.body.raw);
1962+
expect(resultBody.id).to.equal('<long>');
1963+
expect(resultBody.name).to.equal('<string>');
1964+
expect(result.contentHeader).to.deep.include({ key: 'Content-Type', value: 'application/vnd.api+json' });
1965+
expect(result.body.options.raw.language).to.equal('json');
1966+
done();
1967+
});
1968+
it(' application/vnd.api+xml (headers with different structure but still of XML family)', function(done) {
1969+
var requestBody = {
1970+
description: 'body description',
1971+
content: {
1972+
'application/vnd.api+xml': {
1973+
examples: {
1974+
xml: {
1975+
summary: 'A list containing two items',
1976+
value: '<AnXMLObject>test</AnXMLObject>'
1977+
}
1978+
}
1979+
}
1980+
}
1981+
},
1982+
result, resultBody;
1983+
result = SchemaUtils.convertToPmBody(requestBody, 'ROOT', {}, {
1984+
requestParametersResolution: 'example'
1985+
});
1986+
resultBody = (result.body.raw);
1987+
expect(resultBody).to.equal(
1988+
'<?xml version="1.0" encoding="UTF-8"?>\n<AnXMLObject>test</AnXMLObject>'
1989+
);
1990+
expect(result.contentHeader).to.deep.include(
1991+
{ key: 'Content-Type', value: 'application/vnd.api+xml' });
1992+
expect(result.body.options.raw.language).to.equal('xml');
1993+
done();
1994+
});
19311995
// things remaining : application/xml
19321996
});
19331997
});

0 commit comments

Comments
 (0)