Adding swagger support in convert

This commit is contained in:
Erik Mendoza
2022-04-13 10:48:37 -05:00
parent b9bb50db5d
commit 888bd70565
8 changed files with 461 additions and 4601 deletions

View File

@@ -145,8 +145,22 @@ function filterOptionsByVersion(options, version) {
return options;
}
/**
* Calculates if thew current input is using swagger 2.0 spec
* @param {string} version The current spec version
* @returns {boolean} True if the current spec is using swagger 2.0 spec
*/
function isSwagger(version) {
let isSwagger = false;
if (version === VERSION_20.version) {
isSwagger = true;
}
return isSwagger;
}
module.exports = {
getSpecVersion,
getConcreteSchemaUtils,
filterOptionsByVersion
filterOptionsByVersion,
isSwagger
};

View File

@@ -5,7 +5,7 @@ const _ = require('lodash'),
VERSION30 = '3.0',
VERSION31 = '3.1',
VERSION20 = '2.0',
SUPPORTED_VERSIONS = [VERSION30, VERSION31];
SUPPORTED_VERSIONS = [VERSION20, VERSION30, VERSION31];
/**
* Takes a list of arguments and resolve them acording its content

View File

@@ -1,8 +1,10 @@
'use strict';
const { convertSwaggerToOpenapi } = require('./swaggerUtils/swaggerToOpenapi.js');
// This is the default collection name if one can't be inferred from the OpenAPI spec
const COLLECTION_NAME = 'Imported from OpenAPI 3.0',
{ getConcreteSchemaUtils } = require('./common/versionUtils.js'),
{ getConcreteSchemaUtils, isSwagger } = require('./common/versionUtils.js'),
BROWSER = 'browser',
Ajv = require('ajv'),
addFormats = require('ajv-formats'),
@@ -235,7 +237,7 @@ class SchemaPack {
// convert method, this is called when you want to convert a schema that you've already loaded
// in the constructor
convert (callback) {
async convert (callback) {
let openapi,
options = this.computedOptions,
analysis,
@@ -256,6 +258,16 @@ class SchemaPack {
specComponentsAndUtils = { concreteUtils };
Object.assign(specComponentsAndUtils, concreteUtils.getRequiredData(this.openapi));
if (isSwagger(concreteUtils.version)) {
try {
let result = await convertSwaggerToOpenapi(this.openapi);
this.openapi = result.openapi;
}
catch (error) {
return callback(error);
}
}
// create and sanitize basic spec
openapi = this.openapi;
openapi.servers = _.isEmpty(openapi.servers) ? [{ url: '/' }] : openapi.servers;

View File

@@ -13,5 +13,17 @@ module.exports = {
*/
parseSpec: function (openApiSpec) {
return schemaUtilsCommon.parseSpec(openApiSpec, inputValidationSwagger);
},
/**
* Get the required elements for conversion from spec parsed data
* @param {object} spec openapi parsed value
* @returns {object} required elements to convert
*/
getRequiredData: function(spec) {
return {
info: spec.info,
paths: spec.paths
};
}
};

View File

@@ -0,0 +1,22 @@
const Swagger2OpenAPI = require('swagger2openapi'),
_ = require('lodash');
module.exports = {
convertSwaggerToOpenapi: function(parsedSwagger) {
try {
return Swagger2OpenAPI.convertObj(
parsedSwagger,
{
fatal: false,
patch: true,
anchors: true,
warnOnly: true
}
);
}
catch (error) {
return error;
}
}
};

4978
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -126,6 +126,7 @@
"oas-resolver-browser": "2.5.2",
"path-browserify": "1.0.1",
"postman-collection": "4.0.0",
"swagger2openapi": "7.0.8",
"yaml": "1.10.2"
},
"author": "Postman Labs <help@getpostman.com>",

View File

@@ -42,3 +42,18 @@ describe('getMetaData method', function() {
});
});
});
describe('Convert method', function() {
it('Should convert a basic example', function() {
const fileSource = path.join(__dirname, SWAGGER_20_FOLDER + '/sampleswagger.json'),
fileData = fs.readFileSync(fileSource, 'utf8'),
input = {
type: 'string',
data: fileData
},
schemapack = new SchemaPack(input);
schemapack.convert((error, result) => {
expect(error).to.be.null;
});
});
});