diff --git a/libV2/index.js b/libV2/index.js index 73193626..4f406d47 100644 --- a/libV2/index.js +++ b/libV2/index.js @@ -33,7 +33,7 @@ module.exports = { let preOrderTraversal = GraphLib.alg.preorder(collectionTree, 'root:collection'); let collection = {}, - finalExtractedTypesList = []; + finalExtractedTypesObject = {}; /** * individually start generating the folder, request, collection @@ -93,17 +93,18 @@ module.exports = { let request = {}, collectionVariables = [], requestObject = {}, - extractedTypesList = []; + extractedTypesObject = {}; try { - ({ request, collectionVariables, extractedTypesList } = resolvePostmanRequest(context, + ({ request, collectionVariables, extractedTypesObject } = resolvePostmanRequest(context, context.openapi.paths[node.meta.path], node.meta.path, node.meta.method )); requestObject = generateRequestItemObject(request); - finalExtractedTypesList = finalExtractedTypesList.concat(extractedTypesList); + finalExtractedTypesObject = Object.assign({}, finalExtractedTypesObject, extractedTypesObject); + } catch (error) { console.error(error); @@ -228,7 +229,7 @@ module.exports = { data: collection }], analytics: this.analytics || {}, - extractedTypes: finalExtractedTypesList || [] + extractedTypes: finalExtractedTypesObject || [] }); } return cb(null, { diff --git a/libV2/schemaUtils.js b/libV2/schemaUtils.js index 32d0c0a1..de7b1673 100644 --- a/libV2/schemaUtils.js +++ b/libV2/schemaUtils.js @@ -2589,8 +2589,7 @@ module.exports = { { alwaysInheritAuthentication } = context.computedOptions, methodPath, requestBlock, - requestObj, - extractedTypesList = []; + extractedTypesObject = {}; context.resolvedSchemaTypes = null; headers.push(..._.get(requestBody, 'headers', [])); pathVariables.push(...baseUrlData.pathVariables); @@ -2627,8 +2626,7 @@ module.exports = { methodPath = method + path; requestBlock = { request: unifiedRequestTypes, response: resolvedExampleTypes }; - requestObj = { [methodPath]: requestBlock }; - extractedTypesList.push(requestObj); + Object.assign(extractedTypesObject, { [methodPath]: requestBlock }); // add accept header if found and not present already if (!_.isEmpty(acceptHeader)) { @@ -2643,7 +2641,7 @@ module.exports = { }) }, collectionVariables, - extractedTypesList + extractedTypesObject }; }, diff --git a/test/unit/convertV2WithTypes.test.js b/test/unit/convertV2WithTypes.test.js index c90a5c54..d0c5756c 100644 --- a/test/unit/convertV2WithTypes.test.js +++ b/test/unit/convertV2WithTypes.test.js @@ -83,7 +83,7 @@ describe('convertV2WithTypes', function() { expect(err).to.be.null; expect(conversionResult.result).to.equal(true); expect(conversionResult.extractedTypes).to.not.be.undefined; - expect(conversionResult.extractedTypes.length).to.not.equal(0); + expect(Object.keys(conversionResult.extractedTypes).length).to.not.equal(0); } ); }); @@ -97,24 +97,25 @@ describe('convertV2WithTypes', function() { { type: 'file', data: testSpec1 }, { requestNameSource: 'url' }, (err, conversionResult) => { expect(err).to.be.null; - expect(conversionResult.extractedTypes).to.be.an('array').that.is.not.empty; - const element = conversionResult.extractedTypes[0]; - - expect(element).to.be.an('object').that.includes.keys('request'); - expect(element).to.be.an('object').that.includes.keys('response'); - const { response } = element; - expect(response).to.be.an('object').that.is.not.empty; - const [key, value] = Object.entries(response)[1]; - expect(key).to.be.a('string'); - const schema = JSON.parse(value.body), - transformedSchema = transformSchema(schema), - validate = ajv.compile(transformedSchema), - valid = validate(example); - - expect(value).to.have.property('body').that.is.a('string'); - - - expect(valid, `Validation failed for key: ${key} with errors: ${JSON.stringify(validate.errors)}`).to.be.true; + expect(conversionResult.extractedTypes).to.be.an('object').that.is.not.empty; + for (const [path, element] of Object.entries(conversionResult.extractedTypes)) { + expect(element).to.be.an('object').that.includes.keys('request'); + expect(element).to.be.an('object').that.includes.keys('response'); + expect(path).to.be.a('string'); + + const { response } = element; + expect(response).to.be.an('object').that.is.not.empty; + const [key, value] = Object.entries(response)[1]; + expect(key).to.be.a('string'); + + const schema = JSON.parse(value.body), + transformedSchema = transformSchema(schema), + validate = ajv.compile(transformedSchema), + valid = validate(example); + + expect(value).to.have.property('body').that.is.a('string'); + expect(valid, `Validation failed for key: ${key} with errors: ${JSON.stringify(validate.errors)}`).to.be.true; + } }); }); @@ -137,12 +138,10 @@ describe('convertV2WithTypes', function() { Converter.convertV2WithTypes({ type: 'string', data: openapi }, options, (err, conversionResult) => { expect(err).to.be.null; - expect(conversionResult.extractedTypes).to.be.an('array').that.is.not.empty; + expect(conversionResult.extractedTypes).to.be.an('object').that.is.not.empty; - // Validate the first extracted type - const element = conversionResult.extractedTypes[0]; + const element = Object.values(conversionResult.extractedTypes)[0]; const { response } = element; - // Get the schema from the response const [key, value] = Object.entries(response)[0]; expect(value).to.have.property('body').that.is.a('string'); @@ -153,6 +152,7 @@ describe('convertV2WithTypes', function() { valid = validate(example); expect(valid, `Validation failed for key: ${key} with errors: ${JSON.stringify(validate.errors)}`).to.be.true; done(); - }); + } + ); }); });