From d3e7017d357ca10318ab68212841e7c8cea6c1c9 Mon Sep 17 00:00:00 2001 From: sarthak_saxena_72 Date: Tue, 30 Mar 2021 16:40:27 +0530 Subject: [PATCH 01/10] Changes to handle multiple response examples. Functions convertToPmResponse, convertToPmResponseBody, convertToPmBodyData now return a list of bodies, changed getExampleData to handle one example at a time. --- lib/schemaUtils.js | 181 +++++++++++++++++++++++++-------------------- 1 file changed, 99 insertions(+), 82 deletions(-) diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index e203f1d35..650e17751 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -1225,7 +1225,7 @@ module.exports = { convertToPmResponseBody: function(contentObj, components, options, schemaCache) { options = _.merge({}, defaultOptions, options); - var responseBody, cTypeHeader, hasComputedType, cTypes; + var cTypeHeader, hasComputedType, cTypes, pmBodies = []; if (!contentObj) { return { contentTypeHeader: null, @@ -1260,19 +1260,23 @@ module.exports = { }; } } - responseBody = this.convertToPmBodyData(contentObj[cTypeHeader], REQUEST_TYPE.EXAMPLE, cTypeHeader, + var 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 = ''; - } - return { - contentTypeHeader: cTypeHeader, - responseBody: 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 + }); + }) + return pmBodies }, /** @@ -1313,23 +1317,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; @@ -1356,7 +1355,7 @@ module.exports = { indentCharacter, components, options, schemaCache) { options = _.merge({}, defaultOptions, options); - var bodyData = '', + var bodyData = [], schemaFormat = SCHEMA_FORMATS.DEFAULT, schemaType, resolveTo = this.resolveToExampleOrSchema(requestType, options.requestParametersResolution, @@ -1376,15 +1375,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')) { @@ -1410,12 +1415,12 @@ module.exports = { return ''; } // 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; @@ -1671,7 +1676,8 @@ module.exports = { description, required, enumValue, - formHeaders = []; + formHeaders = [], + pmBodies = []; // @TODO: how do we support multiple content types contentObj = requestBody.content; @@ -1692,7 +1698,7 @@ module.exports = { contentObj[URLENCODED].schema = this.getRefObject(contentObj[URLENCODED].schema.$ref, components, options); } 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) => { @@ -1751,7 +1757,7 @@ module.exports = { else if (contentObj.hasOwnProperty(FORM_DATA)) { rDataMode = 'formdata'; 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) => { @@ -1831,28 +1837,32 @@ module.exports = { } } } + + contentHeader = new sdk.Header({ + key: 'Content-Type', + value: bodyType + }); bodyData = this.convertToPmBodyData(contentObj[bodyType], requestType, bodyType, PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache); - updateOptions = { - mode: rDataMode, - raw: JSON.stringify(bodyData, null, 4) - }; + for(body in bodyData) { + updateOptions = { + mode: rDataMode, + raw: JSON.stringify(body, null, 4) + }; - contentHeader = new sdk.Header({ - key: 'Content-Type', - value: bodyType - }); + reqBody.update(updateOptions); - reqBody.update(updateOptions); + pmBodies.push({ + body: body, + contentHeader: contentHeader, + formHeaders: formHeaders + }); + } } - return { - body: reqBody, - contentHeader: contentHeader, - formHeaders: formHeaders - }; + return pmBodies }, /** @@ -1871,7 +1881,8 @@ module.exports = { previewLanguage = 'text', responseBodyWrapper, header, - sdkResponse; + sdkResponse, + sdkResponses = []; if (!response) { return null; @@ -1895,42 +1906,46 @@ module.exports = { } }); - responseBodyWrapper = this.convertToPmResponseBody(response.content, components, options, schemaCache); + responseBodyWrappers = 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.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 - }); - sdkResponse._postman_previewlanguage = previewLanguage; + 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; - return sdkResponse; + sdkResponses.push(sdkResponse); + }) + + return sdkResponses; }, /** @@ -2389,9 +2404,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); + }); }); } From 0feacb16c42ec37c45de4610f59e84658f486388 Mon Sep 17 00:00:00 2001 From: sarthak_saxena_72 Date: Tue, 30 Mar 2021 17:33:13 +0530 Subject: [PATCH 02/10] lint fix --- lib/schemaUtils.js | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index 650e17751..efee46f77 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -1225,7 +1225,12 @@ module.exports = { convertToPmResponseBody: function(contentObj, components, options, schemaCache) { options = _.merge({}, defaultOptions, options); - var cTypeHeader, hasComputedType, cTypes, pmBodies = []; + var cTypeHeader, + hasComputedType, + cTypes, + pmBodies = [], + responseBodies; + if (!contentObj) { return { contentTypeHeader: null, @@ -1260,10 +1265,10 @@ module.exports = { }; } } - var responseBodies = 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); - responseBodies.forEach(responseBody => { + responseBodies.forEach(responseBody => { if (this.getHeaderFamily(cTypeHeader) === HEADER_TYPE.JSON) { responseBody = JSON.stringify(responseBody, null, options.indentCharacter); } @@ -1275,8 +1280,8 @@ module.exports = { contentTypeHeader: cTypeHeader, responseBody: responseBody }); - }) - return pmBodies + }); + return pmBodies; }, /** @@ -1358,6 +1363,7 @@ module.exports = { var bodyData = [], schemaFormat = SCHEMA_FORMATS.DEFAULT, schemaType, + example_names, resolveTo = this.resolveToExampleOrSchema(requestType, options.requestParametersResolution, options.exampleParametersResolution); @@ -1384,12 +1390,13 @@ module.exports = { bodyData.push(bodyObj.example); } } + else if (!_.isEmpty(bodyObj.examples) && (resolveTo === 'example' || !bodyObj.schema)) { example_names = Object.keys(bodyObj.examples); - example_names.forEach(example_name => { + 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')) { @@ -1415,8 +1422,8 @@ module.exports = { return ''; } // Do not fake the bodyData if the complexity is 10. - bodyData.push(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 @@ -1677,7 +1684,8 @@ module.exports = { required, enumValue, formHeaders = [], - pmBodies = []; + pmBodies = [], + bodyData, // @TODO: how do we support multiple content types contentObj = requestBody.content; @@ -1837,7 +1845,6 @@ module.exports = { } } } - contentHeader = new sdk.Header({ key: 'Content-Type', value: bodyType @@ -1846,7 +1853,7 @@ module.exports = { bodyData = this.convertToPmBodyData(contentObj[bodyType], requestType, bodyType, PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache); - for(body in bodyData) { + for (body in bodyData) { updateOptions = { mode: rDataMode, raw: JSON.stringify(body, null, 4) @@ -1862,7 +1869,7 @@ module.exports = { } } - return pmBodies + return pmBodies; }, /** @@ -2343,7 +2350,7 @@ module.exports = { let thisOriginalRequest = {}, responseAuthHelper, authQueryParams, - convertedResponse; + convertedResponses; if (options.includeAuthInfoInExample) { responseAuthHelper = this.getResponseAuthHelper(authHelper); @@ -2406,7 +2413,7 @@ module.exports = { } convertedResponses = this.convertToPmResponse(swagResponse, code, thisOriginalRequest, components, options, schemaCache); - convertedResponses.forEach(convertedResponse => { + convertedResponses.forEach((convertedResponse) => { item.responses.add(convertedResponse); }); }); From a36ca9d5b8579be706e44a315b30edda12674ac9 Mon Sep 17 00:00:00 2001 From: sarthak_saxena_72 Date: Tue, 30 Mar 2021 17:48:38 +0530 Subject: [PATCH 03/10] lint fix --- lib/schemaUtils.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index efee46f77..c4fbf8686 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -1226,10 +1226,10 @@ module.exports = { options = _.merge({}, defaultOptions, options); var cTypeHeader, - hasComputedType, - cTypes, - pmBodies = [], - responseBodies; + hasComputedType, + cTypes, + pmBodies = [], + responseBodies; if (!contentObj) { return { @@ -1268,7 +1268,7 @@ module.exports = { responseBodies = this.convertToPmBodyData(contentObj[cTypeHeader], REQUEST_TYPE.EXAMPLE, cTypeHeader, PARAMETER_SOURCE.RESPONSE, options.indentCharacter, components, options, schemaCache); - responseBodies.forEach(responseBody => { + responseBodies.forEach((responseBody) => { if (this.getHeaderFamily(cTypeHeader) === HEADER_TYPE.JSON) { responseBody = JSON.stringify(responseBody, null, options.indentCharacter); } @@ -1381,7 +1381,6 @@ module.exports = { catch (e) {} } } - // return example value if present else example is returned if (bodyObj.example.hasOwnProperty('value')) { bodyData.push(bodyObj.example.value); @@ -1684,8 +1683,7 @@ module.exports = { required, enumValue, formHeaders = [], - pmBodies = [], - bodyData, + pmBodies = []; // @TODO: how do we support multiple content types contentObj = requestBody.content; @@ -1853,7 +1851,7 @@ module.exports = { bodyData = this.convertToPmBodyData(contentObj[bodyType], requestType, bodyType, PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache); - for (body in bodyData) { + bodyData.forEach((body) => { updateOptions = { mode: rDataMode, raw: JSON.stringify(body, null, 4) @@ -1866,7 +1864,7 @@ module.exports = { contentHeader: contentHeader, formHeaders: formHeaders }); - } + }); } return pmBodies; @@ -1886,7 +1884,7 @@ module.exports = { options = _.merge({}, defaultOptions, options); var responseHeaders = [], previewLanguage = 'text', - responseBodyWrapper, + responseBodyWrappers, header, sdkResponse, sdkResponses = []; @@ -1915,7 +1913,7 @@ module.exports = { responseBodyWrappers = this.convertToPmResponseBody(response.content, components, options, schemaCache); - responseBodyWrappers.forEach (responseBodyWrapper => { + responseBodyWrappers.forEach((responseBodyWrapper) => { if (responseBodyWrapper.contentTypeHeader) { // we could infer the content-type header from the body responseHeaders.push({ key: 'Content-Type', value: responseBodyWrapper.contentTypeHeader }); @@ -1950,7 +1948,7 @@ module.exports = { sdkResponse._postman_previewlanguage = previewLanguage; sdkResponses.push(sdkResponse); - }) + }); return sdkResponses; }, From fd5aac1b1e8fe643377b195328a98c11381f9495 Mon Sep 17 00:00:00 2001 From: sarthak_saxena_72 Date: Tue, 30 Mar 2021 17:54:03 +0530 Subject: [PATCH 04/10] lint fix --- lib/schemaUtils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index c4fbf8686..b88f418e1 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -1851,7 +1851,7 @@ module.exports = { bodyData = this.convertToPmBodyData(contentObj[bodyType], requestType, bodyType, PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache); - bodyData.forEach((body) => { + bodyData.forEach((body) => { updateOptions = { mode: rDataMode, raw: JSON.stringify(body, null, 4) From dc12a7db9cdf91ea72c17ecebb7bc5be1f510bf1 Mon Sep 17 00:00:00 2001 From: sarthak_saxena_72 Date: Tue, 30 Mar 2021 18:26:59 +0530 Subject: [PATCH 05/10] fixed a tc --- lib/schemaUtils.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index b88f418e1..8c46eb79f 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -1220,7 +1220,7 @@ 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); @@ -1232,10 +1232,10 @@ module.exports = { responseBodies; if (!contentObj) { - return { + return [{ contentTypeHeader: null, - responseBody: '' - }; + responseBody: [] + }]; } let headers = Object.keys(contentObj); @@ -1259,10 +1259,10 @@ module.exports = { } else { // just an empty object - can't convert anything - return { + return [{ contentTypeHeader: null, - responseBody: '' - }; + responseBody: [] + }]; } } responseBodies = this.convertToPmBodyData(contentObj[cTypeHeader], REQUEST_TYPE.EXAMPLE, cTypeHeader, @@ -1274,7 +1274,7 @@ module.exports = { } else if (typeof responseBody !== 'string') { // since the collection v2 schema only supports body being a string - responseBody = ''; + responseBody = []; } pmBodies.push({ contentTypeHeader: cTypeHeader, @@ -1878,7 +1878,7 @@ 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); @@ -1912,7 +1912,6 @@ module.exports = { }); responseBodyWrappers = this.convertToPmResponseBody(response.content, components, options, schemaCache); - responseBodyWrappers.forEach((responseBodyWrapper) => { if (responseBodyWrapper.contentTypeHeader) { // we could infer the content-type header from the body From 579660ae5c66366e9675a0ed23636914d74fec75 Mon Sep 17 00:00:00 2001 From: sarthak_saxena_72 Date: Tue, 30 Mar 2021 19:56:55 +0530 Subject: [PATCH 06/10] fixed some tcs --- lib/schemaUtils.js | 219 ++++++++++++++++++++++----------------------- 1 file changed, 108 insertions(+), 111 deletions(-) diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index 8c46eb79f..736283fe2 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -1234,7 +1234,7 @@ module.exports = { if (!contentObj) { return [{ contentTypeHeader: null, - responseBody: [] + responseBody: '' }]; } let headers = Object.keys(contentObj); @@ -1261,7 +1261,7 @@ module.exports = { // just an empty object - can't convert anything return [{ contentTypeHeader: null, - responseBody: [] + responseBody: '' }]; } } @@ -1274,7 +1274,7 @@ module.exports = { } else if (typeof responseBody !== 'string') { // since the collection v2 schema only supports body being a string - responseBody = []; + responseBody = ''; } pmBodies.push({ contentTypeHeader: cTypeHeader, @@ -1409,16 +1409,16 @@ module.exports = { if (options.complexityScore === 10) { schemaType = bodyObj.schema.type; if (schemaType === 'object') { - return { + return [{ value: '' - }; + }]; } if (schemaType === 'array') { - return [ + return [[ '' - ]; + ]]; } - return ''; + return ['']; } // Do not fake the bodyData if the complexity is 10. bodyData.push(safeSchemaFaker(bodyObj.schema || {}, resolveTo, PROCESSING_TYPE.CONVERSION, @@ -1669,7 +1669,7 @@ module.exports = { convertToPmBody: function(requestBody, requestType, components, options, schemaCache) { options = _.merge({}, defaultOptions, options); var contentObj, // content is required - bodyData, + bodyData = [], param, paramArray = [], updateOptions = {}, @@ -1704,129 +1704,126 @@ module.exports = { contentObj[URLENCODED].schema = this.getRefObject(contentObj[URLENCODED].schema.$ref, components, options); } bodyData = this.convertToPmBodyData(contentObj[URLENCODED], requestType, URLENCODED, - PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache)[0]; + PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache); encoding = contentObj[URLENCODED].encoding ? contentObj[URLENCODED].encoding : {}; // create query parameters and add it to the request body object - _.forOwn(bodyData, (value, key) => { - - if (_.get(contentObj[URLENCODED], 'schema.type') === 'object') { - description = _.get(contentObj[URLENCODED], ['schema', 'properties', key, 'description'], ''); - required = _.includes(_.get(contentObj[URLENCODED], ['schema', 'required']), key); - enumValue = _.get(contentObj[URLENCODED], ['schema', 'properties', key, 'enum']); - } + bodyData.forEach((body) => { + _.forOwn(body, (value, key) => { - !encoding[key] && (encoding[key] = {}); - encoding[key].name = key; - encoding[key].schema = { - type: typeof value - }; - // for urlencoded body serialisation is treated similar to query param - // reference https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#fixed-fields-13 - encoding[key].in = 'query'; - _.isBoolean(required) && (encoding[key].required = required); - encoding[key].description = description; - - params = this.convertParamsWithStyle(encoding[key], value, PARAMETER_SOURCE.REQUEST, components, - schemaCache, options); - // TODO: Show warning for incorrect schema if !params - params && params.forEach((element) => { - // Collection v2.1 schema allows urlencoded param value to be only string - if (typeof element.value !== 'string') { - try { - // convert other datatype to string (i.e. number, boolean etc) - element.value = JSON.stringify(element.value); - } - catch (e) { - // JSON.stringify can fail in few cases, suggest invalid type for such case - // eslint-disable-next-line max-len - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Exceptions - element.value = 'INVALID_URLENCODED_PARAM_TYPE'; - } + if (_.get(contentObj[URLENCODED], 'schema.type') === 'object') { + description = _.get(contentObj[URLENCODED], ['schema', 'properties', key, 'description'], ''); + required = _.includes(_.get(contentObj[URLENCODED], ['schema', 'required']), key); + enumValue = _.get(contentObj[URLENCODED], ['schema', 'properties', key, 'enum']); } + !encoding[key] && (encoding[key] = {}); + encoding[key].name = key; + encoding[key].schema = { + type: typeof value + }; + // for urlencoded body serialisation is treated similar to query param + // reference https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#fixed-fields-13 + encoding[key].in = 'query'; + _.isBoolean(required) && (encoding[key].required = required); + encoding[key].description = description; + params = this.convertParamsWithStyle(encoding[key], value, PARAMETER_SOURCE.REQUEST, components, + schemaCache, options); + // TODO: Show warning for incorrect schema if !params + params && params.forEach((element) => { + // Collection v2.1 schema allows urlencoded param value to be only string + if (typeof element.value !== 'string') { + try { + // convert other datatype to string (i.e. number, boolean etc) + element.value = JSON.stringify(element.value); + } + catch (e) { + // JSON.stringify can fail in few cases, suggest invalid type for such case + // eslint-disable-next-line max-len + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Exceptions + element.value = 'INVALID_URLENCODED_PARAM_TYPE'; + } + } + }); + paramArray.push(...params); }); - paramArray.push(...params); - }); - updateOptions = { - mode: rDataMode, - urlencoded: paramArray - }; - - // add a content type header for each media type for the request body - contentHeader = new sdk.Header({ - key: 'Content-Type', - value: URLENCODED + updateOptions = { + mode: rDataMode, + urlencoded: paramArray + }; + // add a content type header for each media type for the request body + contentHeader = new sdk.Header({ + key: 'Content-Type', + value: URLENCODED + }); + // update the request body with the options + reqBody.update(updateOptions); }); - - // update the request body with the options - reqBody.update(updateOptions); } else if (contentObj.hasOwnProperty(FORM_DATA)) { rDataMode = 'formdata'; bodyData = this.convertToPmBodyData(contentObj[FORM_DATA], requestType, FORM_DATA, - PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache)[0]; + PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache); 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) => { + bodyData.forEach((body) => { + _.forOwn(body, (value, key) => { - if (_.get(contentObj[FORM_DATA], 'schema.type') === 'object') { - description = _.get(contentObj[FORM_DATA], ['schema', 'properties', key, 'description'], ''); - required = _.includes(_.get(contentObj[FORM_DATA], ['schema', 'required']), key); - enumValue = _.get(contentObj[FORM_DATA], ['schema', 'properties', key, 'enum']); - } - description = (required ? '(Required) ' : '') + description + - (enumValue ? ' (This can only be one of ' + enumValue + ')' : ''); - - if (encoding.hasOwnProperty(key)) { - _.forOwn(encoding[key].headers, (value, key) => { - if (key !== 'Content-Type') { - if (encoding[key].headers[key].hasOwnProperty('$ref')) { - encoding[key].headers[key] = getRefObject(encoding[key].headers[key].$ref, components, options); + if (_.get(contentObj[FORM_DATA], 'schema.type') === 'object') { + description = _.get(contentObj[FORM_DATA], ['schema', 'properties', key, 'description'], ''); + required = _.includes(_.get(contentObj[FORM_DATA], ['schema', 'required']), key); + enumValue = _.get(contentObj[FORM_DATA], ['schema', 'properties', key, 'enum']); + } + description = (required ? '(Required) ' : '') + description + + (enumValue ? ' (This can only be one of ' + enumValue + ')' : ''); + if (encoding.hasOwnProperty(key)) { + _.forOwn(encoding[key].headers, (value, key) => { + if (key !== 'Content-Type') { + if (encoding[key].headers[key].hasOwnProperty('$ref')) { + encoding[key].headers[key] = getRefObject(encoding[key].headers[key].$ref, components, options); + } + encoding[key].headers[key].name = key; + // this is only for ROOT request because we are adding the headers for example request later + formHeaders.push(this.convertToPmHeader(encoding[key].headers[key], + REQUEST_TYPE.ROOT, PARAMETER_SOURCE.REQUEST, components, options, schemaCache)); } - encoding[key].headers[key].name = key; - // this is only for ROOT request because we are adding the headers for example request later - formHeaders.push(this.convertToPmHeader(encoding[key].headers[key], - REQUEST_TYPE.ROOT, PARAMETER_SOURCE.REQUEST, components, options, schemaCache)); - } - }); - } - // Collection v2.1 schema allows form param value to be only string - if (typeof value !== 'string') { - try { - // convert other datatype to string (i.e. number, boolean etc) - value = JSON.stringify(value); + }); } - catch (e) { - // JSON.stringify can fail in few cases, suggest invalid type for such case - // eslint-disable-next-line max-len - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Exceptions - value = 'INVALID_FORM_PARAM_TYPE'; + // Collection v2.1 schema allows form param value to be only string + if (typeof value !== 'string') { + try { + // convert other datatype to string (i.e. number, boolean etc) + value = JSON.stringify(value); + } + catch (e) { + // JSON.stringify can fail in few cases, suggest invalid type for such case + // eslint-disable-next-line max-len + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Exceptions + value = 'INVALID_FORM_PARAM_TYPE'; + } } - } - - param = new sdk.FormParam({ - key: key, - value: value, - type: 'text' + param = new sdk.FormParam({ + key: key, + value: value, + type: 'text' + }); + param.description = description; + paramArray.push(param); }); - param.description = description; - paramArray.push(param); - }); - updateOptions = { - mode: rDataMode, - formdata: paramArray - }; - // add a content type header for the pertaining media type - contentHeader = new sdk.Header({ - key: 'Content-Type', - value: FORM_DATA + updateOptions = { + mode: rDataMode, + formdata: paramArray + }; + // add a content type header for the pertaining media type + contentHeader = new sdk.Header({ + key: 'Content-Type', + value: FORM_DATA + }); + // update the request body + reqBody.update(updateOptions); }); - // update the request body - reqBody.update(updateOptions); } else { rDataMode = 'raw'; let bodyType; - // checking for all possible raw types if (contentObj.hasOwnProperty(APP_JS)) { bodyType = APP_JS; } else if (contentObj.hasOwnProperty(APP_JSON)) { bodyType = APP_JSON; } @@ -1890,7 +1887,7 @@ module.exports = { sdkResponses = []; if (!response) { - return null; + return []; } _.forOwn(response.headers, (value, key) => { if (_.toLower(key) !== 'content-type') { From 17ca3cf61540540e4f30e2e6a775c00d4b637eb7 Mon Sep 17 00:00:00 2001 From: sarthak_saxena_72 Date: Tue, 30 Mar 2021 22:15:38 +0530 Subject: [PATCH 07/10] fixed all tcs --- lib/schemaUtils.js | 255 +++++++++++++++++++++++---------------------- 1 file changed, 132 insertions(+), 123 deletions(-) diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index 736283fe2..a3fd027c1 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -1227,7 +1227,6 @@ module.exports = { var cTypeHeader, hasComputedType, - cTypes, pmBodies = [], responseBodies; @@ -1252,9 +1251,8 @@ 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 { @@ -1281,6 +1279,13 @@ module.exports = { responseBody: responseBody }); }); + + if (_.isEmpty(responseBodies)) { + pmBodies.push({ + contentTypeHeader: cTypeHeader, + responseBody: '' + }); + } return pmBodies; }, @@ -1669,7 +1674,7 @@ module.exports = { convertToPmBody: function(requestBody, requestType, components, options, schemaCache) { options = _.merge({}, defaultOptions, options); var contentObj, // content is required - bodyData = [], + bodyData, param, paramArray = [], updateOptions = {}, @@ -1682,8 +1687,7 @@ module.exports = { description, required, enumValue, - formHeaders = [], - pmBodies = []; + formHeaders = []; // @TODO: how do we support multiple content types contentObj = requestBody.content; @@ -1703,127 +1707,134 @@ 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 - bodyData.forEach((body) => { - _.forOwn(body, (value, key) => { + _.forOwn(bodyData, (value, key) => { - if (_.get(contentObj[URLENCODED], 'schema.type') === 'object') { - description = _.get(contentObj[URLENCODED], ['schema', 'properties', key, 'description'], ''); - required = _.includes(_.get(contentObj[URLENCODED], ['schema', 'required']), key); - enumValue = _.get(contentObj[URLENCODED], ['schema', 'properties', key, 'enum']); - } - !encoding[key] && (encoding[key] = {}); - encoding[key].name = key; - encoding[key].schema = { - type: typeof value - }; - // for urlencoded body serialisation is treated similar to query param - // reference https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#fixed-fields-13 - encoding[key].in = 'query'; - _.isBoolean(required) && (encoding[key].required = required); - encoding[key].description = description; - params = this.convertParamsWithStyle(encoding[key], value, PARAMETER_SOURCE.REQUEST, components, - schemaCache, options); - // TODO: Show warning for incorrect schema if !params - params && params.forEach((element) => { - // Collection v2.1 schema allows urlencoded param value to be only string - if (typeof element.value !== 'string') { - try { - // convert other datatype to string (i.e. number, boolean etc) - element.value = JSON.stringify(element.value); - } - catch (e) { - // JSON.stringify can fail in few cases, suggest invalid type for such case - // eslint-disable-next-line max-len - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Exceptions - element.value = 'INVALID_URLENCODED_PARAM_TYPE'; - } - } - }); - paramArray.push(...params); - }); - updateOptions = { - mode: rDataMode, - urlencoded: paramArray + if (_.get(contentObj[URLENCODED], 'schema.type') === 'object') { + description = _.get(contentObj[URLENCODED], ['schema', 'properties', key, 'description'], ''); + required = _.includes(_.get(contentObj[URLENCODED], ['schema', 'required']), key); + enumValue = _.get(contentObj[URLENCODED], ['schema', 'properties', key, 'enum']); + } + + !encoding[key] && (encoding[key] = {}); + encoding[key].name = key; + encoding[key].schema = { + type: typeof value }; - // add a content type header for each media type for the request body - contentHeader = new sdk.Header({ - key: 'Content-Type', - value: URLENCODED + // for urlencoded body serialisation is treated similar to query param + // reference https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#fixed-fields-13 + encoding[key].in = 'query'; + _.isBoolean(required) && (encoding[key].required = required); + encoding[key].description = description; + + params = this.convertParamsWithStyle(encoding[key], value, PARAMETER_SOURCE.REQUEST, components, + schemaCache, options); + // TODO: Show warning for incorrect schema if !params + params && params.forEach((element) => { + // Collection v2.1 schema allows urlencoded param value to be only string + if (typeof element.value !== 'string') { + try { + // convert other datatype to string (i.e. number, boolean etc) + element.value = JSON.stringify(element.value); + } + catch (e) { + // JSON.stringify can fail in few cases, suggest invalid type for such case + // eslint-disable-next-line max-len + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Exceptions + element.value = 'INVALID_URLENCODED_PARAM_TYPE'; + } + } }); - // update the request body with the options - reqBody.update(updateOptions); + paramArray.push(...params); }); + updateOptions = { + mode: rDataMode, + urlencoded: paramArray + }; + + // add a content type header for each media type for the request body + contentHeader = new sdk.Header({ + key: 'Content-Type', + value: URLENCODED + }); + + // update the request body with the options + reqBody.update(updateOptions); } 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 - bodyData.forEach((body) => { - _.forOwn(body, (value, key) => { + _.forOwn(bodyData, (value, key) => { - if (_.get(contentObj[FORM_DATA], 'schema.type') === 'object') { - description = _.get(contentObj[FORM_DATA], ['schema', 'properties', key, 'description'], ''); - required = _.includes(_.get(contentObj[FORM_DATA], ['schema', 'required']), key); - enumValue = _.get(contentObj[FORM_DATA], ['schema', 'properties', key, 'enum']); - } - description = (required ? '(Required) ' : '') + description + - (enumValue ? ' (This can only be one of ' + enumValue + ')' : ''); - if (encoding.hasOwnProperty(key)) { - _.forOwn(encoding[key].headers, (value, key) => { - if (key !== 'Content-Type') { - if (encoding[key].headers[key].hasOwnProperty('$ref')) { - encoding[key].headers[key] = getRefObject(encoding[key].headers[key].$ref, components, options); - } - encoding[key].headers[key].name = key; - // this is only for ROOT request because we are adding the headers for example request later - formHeaders.push(this.convertToPmHeader(encoding[key].headers[key], - REQUEST_TYPE.ROOT, PARAMETER_SOURCE.REQUEST, components, options, schemaCache)); + if (_.get(contentObj[FORM_DATA], 'schema.type') === 'object') { + description = _.get(contentObj[FORM_DATA], ['schema', 'properties', key, 'description'], ''); + required = _.includes(_.get(contentObj[FORM_DATA], ['schema', 'required']), key); + enumValue = _.get(contentObj[FORM_DATA], ['schema', 'properties', key, 'enum']); + } + description = (required ? '(Required) ' : '') + description + + (enumValue ? ' (This can only be one of ' + enumValue + ')' : ''); + + if (encoding.hasOwnProperty(key)) { + _.forOwn(encoding[key].headers, (value, key) => { + if (key !== 'Content-Type') { + if (encoding[key].headers[key].hasOwnProperty('$ref')) { + encoding[key].headers[key] = getRefObject(encoding[key].headers[key].$ref, components, options); } - }); - } - // Collection v2.1 schema allows form param value to be only string - if (typeof value !== 'string') { - try { - // convert other datatype to string (i.e. number, boolean etc) - value = JSON.stringify(value); - } - catch (e) { - // JSON.stringify can fail in few cases, suggest invalid type for such case - // eslint-disable-next-line max-len - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Exceptions - value = 'INVALID_FORM_PARAM_TYPE'; + encoding[key].headers[key].name = key; + // this is only for ROOT request because we are adding the headers for example request later + formHeaders.push(this.convertToPmHeader(encoding[key].headers[key], + REQUEST_TYPE.ROOT, PARAMETER_SOURCE.REQUEST, components, options, schemaCache)); } - } - param = new sdk.FormParam({ - key: key, - value: value, - type: 'text' }); - param.description = description; - paramArray.push(param); - }); - updateOptions = { - mode: rDataMode, - formdata: paramArray - }; - // add a content type header for the pertaining media type - contentHeader = new sdk.Header({ - key: 'Content-Type', - value: FORM_DATA + } + // Collection v2.1 schema allows form param value to be only string + if (typeof value !== 'string') { + try { + // convert other datatype to string (i.e. number, boolean etc) + value = JSON.stringify(value); + } + catch (e) { + // JSON.stringify can fail in few cases, suggest invalid type for such case + // eslint-disable-next-line max-len + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Exceptions + value = 'INVALID_FORM_PARAM_TYPE'; + } + } + + param = new sdk.FormParam({ + key: key, + value: value, + type: 'text' }); - // update the request body - reqBody.update(updateOptions); + param.description = description; + paramArray.push(param); + }); + updateOptions = { + mode: rDataMode, + formdata: paramArray + }; + // add a content type header for the pertaining media type + contentHeader = new sdk.Header({ + key: 'Content-Type', + value: FORM_DATA }); + // update the request body + reqBody.update(updateOptions); } else { rDataMode = 'raw'; let bodyType; + // checking for all possible raw types if (contentObj.hasOwnProperty(APP_JS)) { bodyType = APP_JS; } else if (contentObj.hasOwnProperty(APP_JSON)) { bodyType = APP_JSON; } @@ -1840,31 +1851,29 @@ module.exports = { } } } - contentHeader = new sdk.Header({ - key: 'Content-Type', - value: bodyType - }); + // 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); - - bodyData.forEach((body) => { - updateOptions = { - mode: rDataMode, - raw: JSON.stringify(body, null, 4) - }; + PARAMETER_SOURCE.REQUEST, options.indentCharacter, components, options, schemaCache)[0]; - reqBody.update(updateOptions); + updateOptions = { + mode: rDataMode, + raw: JSON.stringify(bodyData, null, 4) + }; - pmBodies.push({ - body: body, - contentHeader: contentHeader, - formHeaders: formHeaders - }); + contentHeader = new sdk.Header({ + key: 'Content-Type', + value: bodyType }); + + reqBody.update(updateOptions); } - return pmBodies; + return { + body: reqBody, + contentHeader: contentHeader, + formHeaders: formHeaders + }; }, /** From 4e21124ec676f6c11815c1b4eac50ddcd8c794ca Mon Sep 17 00:00:00 2001 From: sarthak_saxena_72 Date: Tue, 30 Mar 2021 22:41:43 +0530 Subject: [PATCH 08/10] fixed all tcs 2 --- test/unit/util.test.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/test/unit/util.test.js b/test/unit/util.test.js index 7c11546b2..c0c917ed8 100644 --- a/test/unit/util.test.js +++ b/test/unit/util.test.js @@ -653,7 +653,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { format: 'int32' } }, - retValSchema = SchemaUtils.convertToPmBodyData(bodyWithSchema, 'ROOT', 'application/json'); + retValSchema = SchemaUtils.convertToPmBodyData(bodyWithSchema, 'ROOT', 'application/json')[0]; expect(retValSchema).to.be.equal(''); }); @@ -664,7 +664,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { value: 'This is a sample value' } }, - retValExample = SchemaUtils.convertToPmBodyData(bodyWithExample, 'application/json'); + retValExample = SchemaUtils.convertToPmBodyData(bodyWithExample, 'application/json')[0]; expect(retValExample).to.equal('This is a sample value'); }); @@ -681,7 +681,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { } }, retValExamples = SchemaUtils.convertToPmBodyData(bodyWithExamples, 'ROOT', 'application/json', - 'request', ' ', null, { requestParametersResolution: 'example' }); + 'request', ' ', null, { requestParametersResolution: 'example' })[0]; expect(retValExamples.foo).to.equal(1); expect(retValExamples.bar).to.equal(2); }); @@ -704,7 +704,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { } } }, - { requestParametersResolution: 'example' }); + { requestParametersResolution: 'example' })[0]; expect(retValExample).to.equal('Hello'); }); @@ -725,7 +725,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { } } } - }, { requestParametersResolution: 'example' }); + }, { requestParametersResolution: 'example' })[0]; expect(retValExample.name).to.equal('Example'); }); }); @@ -1678,7 +1678,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { it('with undefined ContentObj', function() { var contentObj, pmResponseBody; - pmResponseBody = SchemaUtils.convertToPmResponseBody(contentObj).responseBody; + pmResponseBody = SchemaUtils.convertToPmResponseBody(contentObj)[0].responseBody; expect(pmResponseBody).to.equal(''); }); it('with Content-Type application/json', function() { @@ -1703,7 +1703,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { } }, pmResponseBody; - pmResponseBody = JSON.parse(SchemaUtils.convertToPmResponseBody(contentObj).responseBody); + pmResponseBody = JSON.parse(SchemaUtils.convertToPmResponseBody(contentObj)[0].responseBody); expect(pmResponseBody.id).to.equal(''); expect(pmResponseBody.name).to.equal(''); }); @@ -1729,7 +1729,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { } }, pmResponseBody; - pmResponseBody = JSON.parse(SchemaUtils.convertToPmResponseBody(contentObj).responseBody); + pmResponseBody = JSON.parse(SchemaUtils.convertToPmResponseBody(contentObj)[0].responseBody); expect(pmResponseBody.id).to.equal(''); expect(pmResponseBody.name).to.equal(''); }); @@ -1755,7 +1755,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { } }, pmResponseBody; - pmResponseBody = JSON.parse(SchemaUtils.convertToPmResponseBody(contentObj).responseBody); + pmResponseBody = JSON.parse(SchemaUtils.convertToPmResponseBody(contentObj)[0].responseBody); expect(pmResponseBody.id).to.equal(''); expect(pmResponseBody.name).to.equal(''); }); @@ -1775,7 +1775,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { pmResponseBody; pmResponseBody = SchemaUtils.convertToPmResponseBody(contentObj, {}, { indentCharacter: '\t' - }).responseBody; + })[0].responseBody; expect(pmResponseBody).to.equal('{\n\t"id": ""\n}'); }); it('with Content-Type text/plain', function() { @@ -1787,7 +1787,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { } }, pmResponseBody; - pmResponseBody = SchemaUtils.convertToPmResponseBody(contentObj).responseBody; + pmResponseBody = SchemaUtils.convertToPmResponseBody(contentObj)[0].responseBody; expect(typeof pmResponseBody).to.equal('string'); }); it('with Content-Type application/xml', function() { @@ -1815,7 +1815,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { pmResponseBody; pmResponseBody = SchemaUtils.convertToPmResponseBody(contentObj, {}, { indentCharacter: ' ' - }).responseBody; + })[0].responseBody; expect(pmResponseBody).to.equal( [ '', @@ -1835,7 +1835,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { } }, pmResponseBody; - pmResponseBody = SchemaUtils.convertToPmResponseBody(contentObj).responseBody; + pmResponseBody = SchemaUtils.convertToPmResponseBody(contentObj)[0].responseBody; expect(typeof pmResponseBody).to.equal('string'); }); it('with Content-Type unsupported', function() { @@ -1860,9 +1860,9 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { } }, pmResponseBody; - pmResponseBody = SchemaUtils.convertToPmResponseBody(contentObj).responseBody; + pmResponseBody = SchemaUtils.convertToPmResponseBody(contentObj)[0].responseBody; expect(pmResponseBody).to.equal(''); - }); + })[0]; // things remaining application/xml, application/javascript }); }); @@ -1895,7 +1895,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { code = '20X', pmResponse, responseBody; - pmResponse = SchemaUtils.convertToPmResponse(response, code).toJSON(); + pmResponse = SchemaUtils.convertToPmResponse(response, code)[0].toJSON(); responseBody = JSON.parse(pmResponse.body); expect(pmResponse.name).to.equal(response.description); expect(pmResponse.code).to.equal(200); @@ -1935,7 +1935,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { code = '20X', pmResponse; - pmResponse = SchemaUtils.convertToPmResponse(response, code).toJSON(); + pmResponse = SchemaUtils.convertToPmResponse(response, code)[0].toJSON(); expect(pmResponse.body).to.equal('\n (integer)\n (string)\n'); expect(pmResponse.name).to.equal(response.description); expect(pmResponse.code).to.equal(200); @@ -1953,7 +1953,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { code = '201', pmResponse; - pmResponse = SchemaUtils.convertToPmResponse(response, code).toJSON(); + pmResponse = SchemaUtils.convertToPmResponse(response, code)[0].toJSON(); expect(pmResponse.name).to.equal(response.description); expect(pmResponse.code).to.equal(201); expect(pmResponse.body).to.equal(''); @@ -2002,7 +2002,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { } } } - }); + })[0]; expect(pmResponse.headers.members[0].key).to.equal('Retry-After'); expect(pmResponse.headers.members[0].description).to.equal('Some description'); From 128e52a26fffc8a4741a25d7a672f62ea54f16cf Mon Sep 17 00:00:00 2001 From: sarthak_saxena_72 Date: Mon, 5 Apr 2021 01:20:26 +0530 Subject: [PATCH 09/10] added test for multiple examples --- test/data/valid_openapi/example-spec.yaml | 72 +++++++++++++++++++++++ test/unit/base.test.js | 11 ++++ 2 files changed, 83 insertions(+) create mode 100644 test/data/valid_openapi/example-spec.yaml diff --git a/test/data/valid_openapi/example-spec.yaml b/test/data/valid_openapi/example-spec.yaml new file mode 100644 index 000000000..874e32e4c --- /dev/null +++ b/test/data/valid_openapi/example-spec.yaml @@ -0,0 +1,72 @@ +openapi: "3.0.0" + +info: + title: ExampleApi + description: Example Api + version: 1.0.0 + contact: + name: support + email: support@example.com + +servers: + - url: http://example.com + description: example server + +paths: + /users: + post: + description: Create User + summary: Create User + security: + - {} + requestBody: + description: User + content: + application/json: + schema: + type: object + properties: + id: + type: integer + name: + type: string + examples: + John-Ok-Example: + summary: Success create of John User + description: Success create of John User + value: + id: 1 + name: John Doe + Jane-Ok-Example: + summary: Success create of Jane User + description: Success create of Jane User + value: + id: 2 + name: Jane Doe + + responses: + '201': + description: Return a User Object + content: + application/json: + schema: + type: object + properties: + id: + type: integer + name: + type: string + examples: + John-Ok-Example: + summary: Success create of John User + description: Success create of John User + value: + id: 1 + name: John Doe + Jane-Ok-Example: + summary: Success create of Jane User + description: Success create of Jane User + value: + id: 2 + name: Jane Doe + \ No newline at end of file diff --git a/test/unit/base.test.js b/test/unit/base.test.js index a66e12772..c78017ec3 100644 --- a/test/unit/base.test.js +++ b/test/unit/base.test.js @@ -967,6 +967,17 @@ describe('CONVERT FUNCTION TESTS ', function() { done(); }); }); + it('[GitHub #350] - The converter should resolve multiple examples', function (done) { + var emptyAuthSpec = path.join(__dirname, VALID_OPENAPI_PATH + '/example-spec.yaml'), + openapi = fs.readFileSync(emptyAuthSpec, 'utf8'); + Converter.convert({ type: 'string', data: openapi }, { requestNameSource: 'URL' }, + (err, conversionResult) => { + expect(err).to.be.null; + let request = conversionResult.output[0].data.item[0].response; + expect(request.length).to.equal(2); + done(); + }); + }); }); describe('requestNameSource option', function() { From 13e6b8131fafb7cc9b5391fd1618463cc060e3ea Mon Sep 17 00:00:00 2001 From: sarthak_saxena_72 Date: Mon, 12 Apr 2021 11:53:54 +0530 Subject: [PATCH 10/10] request -> response in test --- test/unit/base.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/base.test.js b/test/unit/base.test.js index c78017ec3..df236a319 100644 --- a/test/unit/base.test.js +++ b/test/unit/base.test.js @@ -973,8 +973,8 @@ describe('CONVERT FUNCTION TESTS ', function() { Converter.convert({ type: 'string', data: openapi }, { requestNameSource: 'URL' }, (err, conversionResult) => { expect(err).to.be.null; - let request = conversionResult.output[0].data.item[0].response; - expect(request.length).to.equal(2); + let response = conversionResult.output[0].data.item[0].response; + expect(response.length).to.equal(2); done(); }); });