Fix conversion process for openapi 3.1, adding schemaUtils31X and schemaUtils30X, adding unit tests

This commit is contained in:
Erik Mendoza
2021-12-07 21:52:51 -06:00
parent 7c265fab19
commit 34f31e9931
19 changed files with 897 additions and 92 deletions

View File

@@ -2,6 +2,7 @@
// 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'),
BROWSER = 'browser',
Ajv = require('ajv'),
async = require('async'),
@@ -22,30 +23,27 @@ const COLLECTION_NAME = 'Imported from OpenAPI 3.0',
// This provides the base class for
// errors with the input OpenAPI spec
OpenApiErr = require('./error.js'),
DEFAULT_SPEC_VERSION = '3.0',
{
getSpecVersion
} = require('./common/versionUtils.js');
schemaUtils = require('./schemaUtils');
let path = require('path'),
schemaUtils,
concreteUtils,
pathBrowserify = require('path-browserify');
/**
*
* @param {string} specVersion - the OAS specification version
* @returns {NodeRequire} the schema utils according to version
*/
function getConcreteSchemaUtils(specVersion) {
let schemaUtils = {};
if (specVersion === DEFAULT_SPEC_VERSION) {
schemaUtils = require('./schemaUtils');
}
else {
schemaUtils = require('./schemaUtils31X');
}
return schemaUtils;
}
// /**
// *
// * @param {string} specVersion - the OAS specification version
// * @returns {NodeRequire} the schema utils according to version
// */
// function getConcreteSchemaUtils(specVersion) {
// let concreteUtils = {};
// if (specVersion === DEFAULT_SPEC_VERSION) {
// concreteUtils = require('./30XUtils/schemaUtils30X');
// }
// else {
// concreteUtils = require('./31XUtils/schemaUtils31X');
// }
// return concreteUtils;
// }
class SchemaPack {
constructor (input, options = {}) {
@@ -66,10 +64,9 @@ class SchemaPack {
);
// hardcoding this option - not exposed to users yet
this.computedOptions.schemaFaker = true;
let indentCharacter = this.computedOptions.indentCharacter,
specVersion = getSpecVersion(input);
let indentCharacter = this.computedOptions.indentCharacter;
this.computedOptions.indentCharacter = indentCharacter === 'tab' ? '\t' : ' ';
schemaUtils = getConcreteSchemaUtils(specVersion);
concreteUtils = getConcreteSchemaUtils(input);
this.validate();
}
@@ -125,7 +122,7 @@ class SchemaPack {
return this.validationResult;
}
specParseResult = schemaUtils.parseSpec(json);
specParseResult = concreteUtils.parseSpec(json);
if (!specParseResult.result) {
// validation failed
@@ -258,7 +255,7 @@ class SchemaPack {
analysis,
generatedStore = {},
collectionJSON,
componentsAndPaths,
specComponentsAndUtils,
authHelper,
schemaCache = {
schemaResolutionCache: this.schemaResolutionCache,
@@ -270,10 +267,12 @@ class SchemaPack {
}
// this cannot be attempted before validation
componentsAndPaths = {
components: this.openapi.components,
paths: this.openapi.paths
};
specComponentsAndUtils = { concreteUtils };
Object.assign(specComponentsAndUtils, concreteUtils.getRequiredData(this.openapi));
// {
// components: this.openapi.components,
// paths: this.openapi.paths
// };
// create and sanitize basic spec
openapi = this.openapi;
@@ -330,10 +329,24 @@ class SchemaPack {
// For paths, All operations are grouped based on corresponding paths
try {
if (options.folderStrategy === 'tags') {
schemaUtils.addCollectionItemsUsingTags(openapi, generatedStore, componentsAndPaths, options, schemaCache);
schemaUtils.addCollectionItemsUsingTags(
openapi,
generatedStore,
specComponentsAndUtils,
options,
schemaCache,
concreteUtils
);
}
else {
schemaUtils.addCollectionItemsUsingPaths(openapi, generatedStore, componentsAndPaths, options, schemaCache);
schemaUtils.addCollectionItemsUsingPaths(
openapi,
generatedStore,
specComponentsAndUtils,
options,
schemaCache,
concreteUtils
);
}
}
catch (e) {