Skip to content

Commit

Permalink
AB-289-optimised-structure-and fixed-test-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushShri committed Dec 17, 2024
1 parent 5ec3f70 commit fdc0ca2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
11 changes: 6 additions & 5 deletions libV2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -228,7 +229,7 @@ module.exports = {
data: collection
}],
analytics: this.analytics || {},
extractedTypes: finalExtractedTypesList || []
extractedTypes: finalExtractedTypesObject || []
});
}
return cb(null, {
Expand Down
8 changes: 3 additions & 5 deletions libV2/schemaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand All @@ -2643,7 +2641,7 @@ module.exports = {
})
},
collectionVariables,
extractedTypesList
extractedTypesObject
};
},

Expand Down
48 changes: 24 additions & 24 deletions test/unit/convertV2WithTypes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
});
Expand All @@ -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;
}
});
});

Expand All @@ -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');
Expand All @@ -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();
});
}
);
});
});

0 comments on commit fdc0ca2

Please sign in to comment.