Skip to content

Changes to enable importing multiple response examples. #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 111 additions & 84 deletions lib/schemaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1220,17 +1220,21 @@ module.exports = {
* resolve references while generating params.
* @param {object} options - a standard list of options that's globally passed around. Check options.js for more.
* @param {object} schemaCache - object storing schemaFaker and schmeResolution caches
* @return {object} responseBody, contentType header needed
* @return {Array[Object]} Array of responseBody, contentType and headers needed
*/
convertToPmResponseBody: function(contentObj, components, options, schemaCache) {
options = _.merge({}, defaultOptions, options);

var responseBody, cTypeHeader, hasComputedType, cTypes;
var cTypeHeader,
hasComputedType,
pmBodies = [],
responseBodies;

if (!contentObj) {
return {
return [{
contentTypeHeader: null,
responseBody: ''
};
}];
}
let headers = Object.keys(contentObj);

Expand All @@ -1247,32 +1251,42 @@ module.exports = {

// if no JSON or XML, take whatever we have
if (!hasComputedType) {
cTypes = Object.keys(contentObj);
if (cTypes.length > 0) {
cTypeHeader = cTypes[0];
if (headers.length > 0) {
cTypeHeader = headers[0];
hasComputedType = true;
}
else {
// just an empty object - can't convert anything
return {
return [{
contentTypeHeader: null,
responseBody: ''
};
}];
}
}
responseBody = this.convertToPmBodyData(contentObj[cTypeHeader], REQUEST_TYPE.EXAMPLE, cTypeHeader,
responseBodies = this.convertToPmBodyData(contentObj[cTypeHeader], REQUEST_TYPE.EXAMPLE, cTypeHeader,
PARAMETER_SOURCE.RESPONSE, options.indentCharacter, components, options, schemaCache);
if (this.getHeaderFamily(cTypeHeader) === HEADER_TYPE.JSON) {
responseBody = JSON.stringify(responseBody, null, options.indentCharacter);
}
else if (typeof responseBody !== 'string') {
// since the collection v2 schema only supports body being a string
responseBody = '';

responseBodies.forEach((responseBody) => {
if (this.getHeaderFamily(cTypeHeader) === HEADER_TYPE.JSON) {
responseBody = JSON.stringify(responseBody, null, options.indentCharacter);
}
else if (typeof responseBody !== 'string') {
// since the collection v2 schema only supports body being a string
responseBody = '';
}
pmBodies.push({
contentTypeHeader: cTypeHeader,
responseBody: responseBody
});
});

if (_.isEmpty(responseBodies)) {
pmBodies.push({
contentTypeHeader: cTypeHeader,
responseBody: ''
});
}
return {
contentTypeHeader: cTypeHeader,
responseBody: responseBody
};
return pmBodies;
},

/**
Expand Down Expand Up @@ -1313,23 +1327,18 @@ module.exports = {
getExampleData: function(exampleObj, components, options) {
options = _.merge({}, defaultOptions, options);

var example,
exampleKey;

var example;
if (typeof exampleObj !== 'object') {
return '';
}

exampleKey = Object.keys(exampleObj)[0];
example = exampleObj[exampleKey];
// return example value if present else example is returned

if (example.hasOwnProperty('$ref')) {
if (exampleObj.hasOwnProperty('$ref')) {
example = this.getRefObject(example.$ref, components, options);
}

if (example.hasOwnProperty('value')) {
example = example.value;
if (exampleObj.hasOwnProperty('value')) {
example = exampleObj.value;
}

return example;
Expand All @@ -1356,9 +1365,10 @@ module.exports = {
indentCharacter, components, options, schemaCache) {

options = _.merge({}, defaultOptions, options);
var bodyData = '',
var bodyData = [],
schemaFormat = SCHEMA_FORMATS.DEFAULT,
schemaType,
example_names,
resolveTo = this.resolveToExampleOrSchema(requestType, options.requestParametersResolution,
options.exampleParametersResolution);

Expand All @@ -1376,15 +1386,21 @@ module.exports = {
catch (e) {}
}
}
bodyData = bodyObj.example;
// return example value if present else example is returned
if (bodyData.hasOwnProperty('value')) {
bodyData = bodyData.value;
if (bodyObj.example.hasOwnProperty('value')) {
bodyData.push(bodyObj.example.value);
}
else {
bodyData.push(bodyObj.example);
}
}

else if (!_.isEmpty(bodyObj.examples) && (resolveTo === 'example' || !bodyObj.schema)) {
// take one of the examples as the body and not all
bodyData = this.getExampleData(bodyObj.examples, components, options);
example_names = Object.keys(bodyObj.examples);
example_names.forEach((example_name) => {
const example = this.getExampleData(bodyObj.examples[example_name], components, options);
bodyData.push(example);
});
}
else if (bodyObj.schema) {
if (bodyObj.schema.hasOwnProperty('$ref')) {
Expand All @@ -1398,24 +1414,24 @@ module.exports = {
if (options.complexityScore === 10) {
schemaType = bodyObj.schema.type;
if (schemaType === 'object') {
return {
return [{
value: '<Error: Spec size too large, skipping faking of schemas>'
};
}];
}
if (schemaType === 'array') {
return [
return [[
'<Error: Spec size too large, skipping faking of schemas>'
];
]];
}
return '<Error: Spec size too large, skipping faking of schemas>';
return ['<Error: Spec size too large, skipping faking of schemas>'];
}
// Do not fake the bodyData if the complexity is 10.
bodyData = safeSchemaFaker(bodyObj.schema || {}, resolveTo, PROCESSING_TYPE.CONVERSION, parameterSourceOption,
components, schemaFormat, indentCharacter, schemaCache, options.stackLimit);
bodyData.push(safeSchemaFaker(bodyObj.schema || {}, resolveTo, PROCESSING_TYPE.CONVERSION,
parameterSourceOption, components, schemaFormat, indentCharacter, schemaCache, options.stackLimit));
}
else {
// do not fake if the option is false
bodyData = '';
bodyData = [];
}
}
return bodyData;
Expand Down Expand Up @@ -1691,8 +1707,10 @@ module.exports = {
if (contentObj[URLENCODED].hasOwnProperty('schema') && contentObj[URLENCODED].schema.hasOwnProperty('$ref')) {
contentObj[URLENCODED].schema = this.getRefObject(contentObj[URLENCODED].schema.$ref, components, options);
}

// There is going to be a max of one requestBody
bodyData = this.convertToPmBodyData(contentObj[URLENCODED], requestType, URLENCODED,
PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache);
PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache)[0];
encoding = contentObj[URLENCODED].encoding ? contentObj[URLENCODED].encoding : {};
// create query parameters and add it to the request body object
_.forOwn(bodyData, (value, key) => {
Expand Down Expand Up @@ -1750,8 +1768,10 @@ module.exports = {
}
else if (contentObj.hasOwnProperty(FORM_DATA)) {
rDataMode = 'formdata';

// There is going to be a max of one requestBody
bodyData = this.convertToPmBodyData(contentObj[FORM_DATA], requestType, FORM_DATA,
PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache);
PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache)[0];
encoding = contentObj[FORM_DATA].encoding ? contentObj[FORM_DATA].encoding : {};
// create the form parameters and add it to the request body object
_.forOwn(bodyData, (value, key) => {
Expand Down Expand Up @@ -1832,8 +1852,9 @@ module.exports = {
}
}

// There is going to be a max of one requestBody
bodyData = this.convertToPmBodyData(contentObj[bodyType], requestType, bodyType,
PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache);
PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache)[0];

updateOptions = {
mode: rDataMode,
Expand Down Expand Up @@ -1863,18 +1884,19 @@ module.exports = {
* resolve references while generating params.
* @param {object} options - a standard list of options that's globally passed around. Check options.js for more.
* @param {object} schemaCache - object storing schemaFaker and schmeResolution caches
* @returns {Object} postman response
* @returns {Array[Object]} Array of postman response objects
*/
convertToPmResponse: function(response, code, originalRequest, components, options, schemaCache) {
options = _.merge({}, defaultOptions, options);
var responseHeaders = [],
previewLanguage = 'text',
responseBodyWrapper,
responseBodyWrappers,
header,
sdkResponse;
sdkResponse,
sdkResponses = [];

if (!response) {
return null;
return [];
}
_.forOwn(response.headers, (value, key) => {
if (_.toLower(key) !== 'content-type') {
Expand All @@ -1895,42 +1917,45 @@ module.exports = {
}
});

responseBodyWrapper = this.convertToPmResponseBody(response.content, components, options, schemaCache);

if (responseBodyWrapper.contentTypeHeader) {
// we could infer the content-type header from the body
responseHeaders.push({ key: 'Content-Type', value: responseBodyWrapper.contentTypeHeader });
if (this.getHeaderFamily(responseBodyWrapper.contentTypeHeader) === HEADER_TYPE.JSON) {
previewLanguage = PREVIEW_LANGUAGE.JSON;
}
else if (this.getHeaderFamily(responseBodyWrapper.contentTypeHeader) === HEADER_TYPE.XML) {
previewLanguage = PREVIEW_LANGUAGE.XML;
responseBodyWrappers = this.convertToPmResponseBody(response.content, components, options, schemaCache);
responseBodyWrappers.forEach((responseBodyWrapper) => {
if (responseBodyWrapper.contentTypeHeader) {
// we could infer the content-type header from the body
responseHeaders.push({ key: 'Content-Type', value: responseBodyWrapper.contentTypeHeader });
if (this.getHeaderFamily(responseBodyWrapper.contentTypeHeader) === HEADER_TYPE.JSON) {
previewLanguage = PREVIEW_LANGUAGE.JSON;
}
else if (this.getHeaderFamily(responseBodyWrapper.contentTypeHeader) === HEADER_TYPE.XML) {
previewLanguage = PREVIEW_LANGUAGE.XML;
}
}
}
else if (response.content && Object.keys(response.content).length > 0) {
responseHeaders.push({ key: 'Content-Type', value: Object.keys(response.content)[0] });
if (this.getHeaderFamily(Object.keys(response.content)[0]) === HEADER_TYPE.JSON) {
previewLanguage = PREVIEW_LANGUAGE.JSON;
else if (response.content && Object.keys(response.content).length > 0) {
responseHeaders.push({ key: 'Content-Type', value: Object.keys(response.content)[0] });
if (this.getHeaderFamily(Object.keys(response.content)[0]) === HEADER_TYPE.JSON) {
previewLanguage = PREVIEW_LANGUAGE.JSON;
}
else if (this.getHeaderFamily(Object.keys(response.content)[0]) === HEADER_TYPE.XML) {
previewLanguage = PREVIEW_LANGUAGE.XML;
}
}
else if (this.getHeaderFamily(Object.keys(response.content)[0]) === HEADER_TYPE.XML) {
previewLanguage = PREVIEW_LANGUAGE.XML;
else {
responseHeaders.push({ key: 'Content-Type', value: TEXT_PLAIN });
}
}
else {
responseHeaders.push({ key: 'Content-Type', value: TEXT_PLAIN });
}
code = code.replace(/X/g, '0');

sdkResponse = new sdk.Response({
name: response.description,
code: code === 'default' ? 500 : Number(code),
header: responseHeaders,
body: responseBodyWrapper.responseBody,
originalRequest: originalRequest
code = code.replace(/X/g, '0');

sdkResponse = new sdk.Response({
name: response.description,
code: code === 'default' ? 500 : Number(code),
header: responseHeaders,
body: responseBodyWrapper.responseBody,
originalRequest: originalRequest
});
sdkResponse._postman_previewlanguage = previewLanguage;

sdkResponses.push(sdkResponse);
});
sdkResponse._postman_previewlanguage = previewLanguage;

return sdkResponse;
return sdkResponses;
},

/**
Expand Down Expand Up @@ -2328,7 +2353,7 @@ module.exports = {
let thisOriginalRequest = {},
responseAuthHelper,
authQueryParams,
convertedResponse;
convertedResponses;

if (options.includeAuthInfoInExample) {
responseAuthHelper = this.getResponseAuthHelper(authHelper);
Expand Down Expand Up @@ -2389,9 +2414,11 @@ module.exports = {
// 'Exception:', e);
thisOriginalRequest.body = {};
}
convertedResponse = this.convertToPmResponse(swagResponse, code, thisOriginalRequest,
convertedResponses = this.convertToPmResponse(swagResponse, code, thisOriginalRequest,
components, options, schemaCache);
convertedResponse && item.responses.add(convertedResponse);
convertedResponses.forEach((convertedResponse) => {
item.responses.add(convertedResponse);
});
});
}

Expand Down
Loading