Skip to content

Commit fdc0ca2

Browse files
committed
AB-289-optimised-structure-and fixed-test-cases
1 parent 5ec3f70 commit fdc0ca2

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

libV2/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = {
3333
let preOrderTraversal = GraphLib.alg.preorder(collectionTree, 'root:collection');
3434

3535
let collection = {},
36-
finalExtractedTypesList = [];
36+
finalExtractedTypesObject = {};
3737

3838
/**
3939
* individually start generating the folder, request, collection
@@ -93,17 +93,18 @@ module.exports = {
9393
let request = {},
9494
collectionVariables = [],
9595
requestObject = {},
96-
extractedTypesList = [];
96+
extractedTypesObject = {};
9797

9898
try {
99-
({ request, collectionVariables, extractedTypesList } = resolvePostmanRequest(context,
99+
({ request, collectionVariables, extractedTypesObject } = resolvePostmanRequest(context,
100100
context.openapi.paths[node.meta.path],
101101
node.meta.path,
102102
node.meta.method
103103
));
104104

105105
requestObject = generateRequestItemObject(request);
106-
finalExtractedTypesList = finalExtractedTypesList.concat(extractedTypesList);
106+
finalExtractedTypesObject = Object.assign({}, finalExtractedTypesObject, extractedTypesObject);
107+
107108
}
108109
catch (error) {
109110
console.error(error);
@@ -228,7 +229,7 @@ module.exports = {
228229
data: collection
229230
}],
230231
analytics: this.analytics || {},
231-
extractedTypes: finalExtractedTypesList || []
232+
extractedTypes: finalExtractedTypesObject || []
232233
});
233234
}
234235
return cb(null, {

libV2/schemaUtils.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,8 +2589,7 @@ module.exports = {
25892589
{ alwaysInheritAuthentication } = context.computedOptions,
25902590
methodPath,
25912591
requestBlock,
2592-
requestObj,
2593-
extractedTypesList = [];
2592+
extractedTypesObject = {};
25942593
context.resolvedSchemaTypes = null;
25952594
headers.push(..._.get(requestBody, 'headers', []));
25962595
pathVariables.push(...baseUrlData.pathVariables);
@@ -2627,8 +2626,7 @@ module.exports = {
26272626

26282627
methodPath = method + path;
26292628
requestBlock = { request: unifiedRequestTypes, response: resolvedExampleTypes };
2630-
requestObj = { [methodPath]: requestBlock };
2631-
extractedTypesList.push(requestObj);
2629+
Object.assign(extractedTypesObject, { [methodPath]: requestBlock });
26322630

26332631
// add accept header if found and not present already
26342632
if (!_.isEmpty(acceptHeader)) {
@@ -2643,7 +2641,7 @@ module.exports = {
26432641
})
26442642
},
26452643
collectionVariables,
2646-
extractedTypesList
2644+
extractedTypesObject
26472645
};
26482646
},
26492647

test/unit/convertV2WithTypes.test.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('convertV2WithTypes', function() {
8383
expect(err).to.be.null;
8484
expect(conversionResult.result).to.equal(true);
8585
expect(conversionResult.extractedTypes).to.not.be.undefined;
86-
expect(conversionResult.extractedTypes.length).to.not.equal(0);
86+
expect(Object.keys(conversionResult.extractedTypes).length).to.not.equal(0);
8787
}
8888
);
8989
});
@@ -97,24 +97,25 @@ describe('convertV2WithTypes', function() {
9797
{ type: 'file', data: testSpec1 }, { requestNameSource: 'url' }, (err, conversionResult) => {
9898

9999
expect(err).to.be.null;
100-
expect(conversionResult.extractedTypes).to.be.an('array').that.is.not.empty;
101-
const element = conversionResult.extractedTypes[0];
102-
103-
expect(element).to.be.an('object').that.includes.keys('request');
104-
expect(element).to.be.an('object').that.includes.keys('response');
105-
const { response } = element;
106-
expect(response).to.be.an('object').that.is.not.empty;
107-
const [key, value] = Object.entries(response)[1];
108-
expect(key).to.be.a('string');
109-
const schema = JSON.parse(value.body),
110-
transformedSchema = transformSchema(schema),
111-
validate = ajv.compile(transformedSchema),
112-
valid = validate(example);
113-
114-
expect(value).to.have.property('body').that.is.a('string');
115-
116-
117-
expect(valid, `Validation failed for key: ${key} with errors: ${JSON.stringify(validate.errors)}`).to.be.true;
100+
expect(conversionResult.extractedTypes).to.be.an('object').that.is.not.empty;
101+
for (const [path, element] of Object.entries(conversionResult.extractedTypes)) {
102+
expect(element).to.be.an('object').that.includes.keys('request');
103+
expect(element).to.be.an('object').that.includes.keys('response');
104+
expect(path).to.be.a('string');
105+
106+
const { response } = element;
107+
expect(response).to.be.an('object').that.is.not.empty;
108+
const [key, value] = Object.entries(response)[1];
109+
expect(key).to.be.a('string');
110+
111+
const schema = JSON.parse(value.body),
112+
transformedSchema = transformSchema(schema),
113+
validate = ajv.compile(transformedSchema),
114+
valid = validate(example);
115+
116+
expect(value).to.have.property('body').that.is.a('string');
117+
expect(valid, `Validation failed for key: ${key} with errors: ${JSON.stringify(validate.errors)}`).to.be.true;
118+
}
118119
});
119120
});
120121

@@ -137,12 +138,10 @@ describe('convertV2WithTypes', function() {
137138

138139
Converter.convertV2WithTypes({ type: 'string', data: openapi }, options, (err, conversionResult) => {
139140
expect(err).to.be.null;
140-
expect(conversionResult.extractedTypes).to.be.an('array').that.is.not.empty;
141+
expect(conversionResult.extractedTypes).to.be.an('object').that.is.not.empty;
141142

142-
// Validate the first extracted type
143-
const element = conversionResult.extractedTypes[0];
143+
const element = Object.values(conversionResult.extractedTypes)[0];
144144
const { response } = element;
145-
146145
// Get the schema from the response
147146
const [key, value] = Object.entries(response)[0];
148147
expect(value).to.have.property('body').that.is.a('string');
@@ -153,6 +152,7 @@ describe('convertV2WithTypes', function() {
153152
valid = validate(example);
154153
expect(valid, `Validation failed for key: ${key} with errors: ${JSON.stringify(validate.errors)}`).to.be.true;
155154
done();
156-
});
155+
}
156+
);
157157
});
158158
});

0 commit comments

Comments
 (0)