Skip to content
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

Removed unnecessary options merging with each util function. #712

Merged
merged 4 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 13 additions & 21 deletions lib/schemaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/sc
openApiErr = require('./error.js'),
ajvValidationError = require('./ajValidation/ajvValidationError'),
utils = require('./utils.js'),
defaultOptions = require('../lib/options.js').getOptions('use'),
{ Node, Trie } = require('./trie.js'),
{ validateSchema } = require('./ajValidation/ajvValidation'),
inputValidation = require('./30XUtils/inputValidation'),
Expand Down Expand Up @@ -523,7 +522,6 @@ module.exports = {
* @returns {*} combined requestParams from operation and path params.
*/
getRequestParams: function(operationParam, pathParam, components, options) {
options = _.merge({}, defaultOptions, options);
if (!Array.isArray(operationParam)) {
operationParam = [];
}
Expand Down Expand Up @@ -579,7 +577,6 @@ module.exports = {
* @returns {Object} - The final object consists of the tree structure
*/
generateTrieFromPaths: function (spec, options, fromWebhooks = false) {
options = _.merge({}, defaultOptions, options);
let concreteUtils = getConcreteSchemaUtils({ type: 'json', data: spec }),
specComponentsAndUtils = {
concreteUtils
Expand Down Expand Up @@ -981,8 +978,6 @@ module.exports = {
* @returns {Array<object>} returns an array of sdk.Variable
*/
convertPathVariables: function(type, providedPathVars, commonPathVars, components, options, schemaCache) {
options = _.merge({}, defaultOptions, options);

var variables = [];
// converting the base uri path variables, if any
// commonPathVars is an object for type = root/method
Expand Down Expand Up @@ -1036,7 +1031,6 @@ module.exports = {
*/
convertChildToItemGroup: function (openapi, child, components, options,
schemaCache, variableStore, fromWebhooks = false) {
options = _.merge({}, defaultOptions, options);

var resource = child,
itemGroup,
Expand Down Expand Up @@ -1398,8 +1392,6 @@ module.exports = {
* @return {object} responseBody, contentType header needed
*/
convertToPmResponseBody: function(contentObj, components, options, schemaCache) {
options = _.merge({}, defaultOptions, options);

var responseBody, cTypeHeader, hasComputedType, cTypes,
isJsonLike = false;
if (!contentObj) {
Expand Down Expand Up @@ -1512,8 +1504,6 @@ module.exports = {
* @returns {*} first example in the input map type
*/
getExampleData: function(exampleObj, components, options) {
options = _.merge({}, defaultOptions, options);

var example,
exampleKey;

Expand Down Expand Up @@ -1556,7 +1546,6 @@ module.exports = {
convertToPmBodyData: function(bodyObj, requestType, contentType, parameterSourceOption,
indentCharacter, components, options, schemaCache) {

options = _.merge({}, defaultOptions, options);
var bodyData = '',
schemaFormat = SCHEMA_FORMATS.DEFAULT,
schemaType,
Expand Down Expand Up @@ -1672,7 +1661,6 @@ module.exports = {
* @returns {array} converted queryparam
*/
convertToPmQueryParameters: function(param, requestType, components, options, schemaCache) {
options = _.merge({}, defaultOptions, options);
var pmParams = [],
paramValue,
resolveTo = this.resolveToExampleOrSchema(requestType, options.requestParametersResolution,
Expand Down Expand Up @@ -1848,8 +1836,6 @@ module.exports = {
* @returns {Object} instance of a Postman SDK Header
*/
convertToPmHeader: function(header, requestType, parameterSource, components, options, schemaCache) {
options = _.merge({}, defaultOptions, options);

var fakeData,
convertedHeader,
reqHeader,
Expand Down Expand Up @@ -1891,7 +1877,6 @@ module.exports = {
* @returns {Object} - Postman requestBody and Content-Type Header
*/
convertToPmBody: function(requestBody, requestType, components, options, schemaCache) {
options = _.merge({}, defaultOptions, options);
var contentObj, // content is required
bodyData,
param,
Expand Down Expand Up @@ -2175,7 +2160,6 @@ module.exports = {
* @returns {Object} postman response
*/
convertToPmResponse: function(response, code, originalRequest, components, options, schemaCache) {
options = _.merge({}, defaultOptions, options);
var responseHeaders = [],
previewLanguage = 'text',
responseBodyWrapper,
Expand Down Expand Up @@ -2287,17 +2271,21 @@ module.exports = {
* @param {object} components - components defined in the OAS spec. These are used to
* resolve references while generating params.
* @param {object} options - a standard list of options that's globally passed around. Check options.js for more.
* @param {*} seenRef - References that are repeated. Used to identify circular references.
* @returns {Object} reference object from the saved components
* @no-unit-tests
*/
getRefObject: function($ref, components, options) {
options = _.merge({}, defaultOptions, options);
getRefObject: function($ref, components, options, seenRef = {}) {
var refObj, savedSchema;

if (typeof $ref !== 'string') {
return { value: `Invalid $ref: ${$ref} was found` };
}

if (seenRef[$ref]) {
return { value: `<Error: "${$ref}" contains circular references in it.` };
VShingala marked this conversation as resolved.
Show resolved Hide resolved
}

savedSchema = $ref.split('/').slice(1).map((elem) => {
// https://swagger.io/docs/specification/using-ref#escape
// since / is the default delimiter, slashes are escaped with ~1
Expand Down Expand Up @@ -2328,10 +2316,16 @@ module.exports = {
return { value: `reference ${$ref} not found in the given specification` };
}

// Mark current $ref as seen while processing it further
seenRef[$ref] = true;

if (refObj.$ref) {
return this.getRefObject(refObj.$ref, components, options);
return this.getRefObject(refObj.$ref, components, options, seenRef);
}

// Unmark current $ref once resolved
seenRef[$ref] = false;

return refObj;
},

Expand Down Expand Up @@ -2461,7 +2455,6 @@ module.exports = {
*/
convertRequestToItem: function(openapi, operationItem, components,
options, schemaCache, variableStore, fromWebhooks = false) {
options = _.merge({}, defaultOptions, options);
var reqName,
pathVariables = openapi.baseUrlVariables,
operation = operationItem.properties,
Expand Down Expand Up @@ -2825,7 +2818,6 @@ module.exports = {
* @returns {*} array of all query params
*/
convertToPmQueryArray: function(reqParams, requestType, components, options, schemaCache) {
options = _.merge({}, defaultOptions, options);
let requestQueryParams = [];
_.forEach(reqParams.query, (queryParam) => {
this.convertToPmQueryParameters(queryParam, requestType, components, options, schemaCache).forEach((pmParam) => {
Expand Down
35 changes: 35 additions & 0 deletions test/data/valid_openapi/recursiveRefComponents.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- $ref: "#/components/parameters/rec-param"
responses:
"401":
$ref: "#/components/responses/Unauthorized"
default:
$ref: "#/components/responses/paramA"
components:
parameters:
rec-param:
$ref: "#/components/parameters/rec-param"
responses:
Unauthorized:
$ref: "#/components/responses/Unauthorized"
paramA:
$ref: "#/components/responses/paramB"
paramB:
$ref: "#/components/responses/paramC"
paramC:
$ref: "#/components/responses/paramA"
33 changes: 32 additions & 1 deletion test/unit/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ describe('CONVERT FUNCTION TESTS ', function() {
specWithNullParams =
path.join(__dirname, VALID_OPENAPI_PATH, '/specWithNullParams.yaml'),
acceptHeaderExample =
path.join(__dirname, VALID_OPENAPI_PATH, '/acceptHeaderExample.json');
path.join(__dirname, VALID_OPENAPI_PATH, '/acceptHeaderExample.json'),
recursiveRefComponents =
path.join(__dirname, VALID_OPENAPI_PATH, '/recursiveRefComponents.yaml');


it('Should add collection level auth with type as `bearer`' +
Expand Down Expand Up @@ -1888,6 +1890,35 @@ describe('CONVERT FUNCTION TESTS ', function() {
done();
});
});

it('Should handle recursive references for non-schema $refs correctly', function(done) {
var openapi = fs.readFileSync(recursiveRefComponents, 'utf8');
Converter.convert({ type: 'string', data: openapi }, {},
(err, conversionResult) => {
expect(err).to.be.null;
expect(conversionResult.result).to.equal(true);
expect(conversionResult.output.length).to.equal(1);
expect(conversionResult.output[0].type).to.equal('collection');
expect(conversionResult.output[0].data).to.have.property('info');
expect(conversionResult.output[0].data).to.have.property('item');
expect(conversionResult.output[0].data.item.length).to.equal(1);

const item = conversionResult.output[0].data.item[0];

expect(item.request.header).to.be.undefined;
expect(item.request.url.query).to.be.empty;
expect(item.response.length).to.eql(2);
expect(item.response[0].header.length).to.eql(1);
expect(item.response[0].header[0].key).to.eql('Content-Type');
expect(item.response[0].header[0].value).to.eql('text/plain');
expect(item.response[0].body).to.be.empty;
expect(item.response[1].header.length).to.eql(1);
expect(item.response[1].header[0].key).to.eql('Content-Type');
expect(item.response[1].header[0].value).to.eql('text/plain');
expect(item.response[1].body).to.be.empty;
done();
});
});
});

describe('Converting swagger 2.0 files', function() {
Expand Down
29 changes: 28 additions & 1 deletion test/unit/convertV2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@
specWithNullParams =
path.join(__dirname, VALID_OPENAPI_PATH, '/specWithNullParams.yaml'),
acceptHeaderExample =
path.join(__dirname, VALID_OPENAPI_PATH, '/acceptHeaderExample.json');
path.join(__dirname, VALID_OPENAPI_PATH, '/acceptHeaderExample.json'),
recursiveRefComponents =
path.join(__dirname, VALID_OPENAPI_PATH, '/recursiveRefComponents.yaml');


describe('The convert v2 Function', function() {
Expand Down Expand Up @@ -256,7 +258,7 @@
});

// Need to handle collaping of folders
it.skip('Should generate collection with collapsing unnecessary folders ' +

Check warning on line 261 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests

Unexpected skipped mocha test
multipleFoldersSpec, function(done) {
var openapi = fs.readFileSync(multipleFoldersSpec, 'utf8');
Converter.convertV2({ type: 'string', data: openapi }, {}, (err, conversionResult) => {
Expand All @@ -268,7 +270,7 @@
done();
});
});
it.skip('Should collapse child and parent folder when parent has only one child' +

Check warning on line 273 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests

Unexpected skipped mocha test
multipleFoldersSpec1, function(done) {
var openapi = fs.readFileSync(multipleFoldersSpec1, 'utf8');
Converter.convertV2({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
Expand All @@ -282,7 +284,7 @@
done();
});
});
it.skip('Should generate collection without creating folders and having only one request' +

Check warning on line 287 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests

Unexpected skipped mocha test
multipleFoldersSpec2, function(done) {
var openapi = fs.readFileSync(multipleFoldersSpec2, 'utf8');
Converter.convertV2({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
Expand Down Expand Up @@ -947,7 +949,7 @@
});

// Handle optional parameters to be made disabled
it.skip('[Github #31] & [GitHub #337] - should set optional params as disabled', function(done) {

Check warning on line 952 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests

Unexpected skipped mocha test
let options = { schemaFaker: true, enableOptionalParameters: false };
Converter.convertV2({ type: 'file', data: requiredInParams }, options, (err, conversionResult) => {
expect(err).to.be.null;
Expand Down Expand Up @@ -2179,4 +2181,29 @@
done();
});
});

it('Should handle recursive references for non-schema $refs correctly', function(done) {
var openapi = fs.readFileSync(recursiveRefComponents, 'utf8');
Converter.convertV2({ type: 'string', data: openapi }, {},
(err, conversionResult) => {
expect(err).to.be.null;
expect(conversionResult.result).to.equal(true);
expect(conversionResult.output.length).to.equal(1);
expect(conversionResult.output[0].type).to.equal('collection');
expect(conversionResult.output[0].data).to.have.property('info');
expect(conversionResult.output[0].data).to.have.property('item');
expect(conversionResult.output[0].data.item.length).to.equal(1);

const item = conversionResult.output[0].data.item[0].item[0];

expect(item.request.header).to.be.undefined;
expect(item.request.url.query).to.be.empty;
expect(item.response.length).to.eql(2);
expect(item.response[0].header).to.be.empty;
expect(item.response[0].body).to.be.undefined;
expect(item.response[1].header).to.be.empty;
expect(item.response[1].body).to.be.undefined;
done();
});
});
});
Loading
Loading