Support for multi file schema in schemapack

This commit is contained in:
umeshp7
2020-01-14 16:21:59 +05:30
parent f15d44dc8a
commit f9adc10459
2 changed files with 52 additions and 85 deletions

View File

@@ -1,14 +1,6 @@
'use strict';
const SchemaPack = require('./lib/schemapack.js').SchemaPack,
async = require('async'),
parse = require('./lib/parse.js'),
// options for oas-resolver
OasResolverOptions = {
resolve: true, // Resolve external references
jsonSchema: true // Treat $ref like JSON Schema and convert to OpenAPI Schema Objects
};
const SchemaPack = require('./lib/schemapack.js').SchemaPack;
module.exports = {
// Old API wrapping the new API
@@ -18,88 +10,19 @@ module.exports = {
if (schema.validated) {
return schema.convert(cb);
}
else if (input.type === 'folder') {
let filesPathArray = input.data,
convertedSpecs = [],
rootFiles = parse.getRootFiles(filesPathArray);
async.eachSeries(rootFiles, (rootFile, callback) => {
parse
// will merge all the files in the folder
.mergeFiles(rootFile, OasResolverOptions)
.then((spec) => {
converter.convert(spec, options, (err, result) => {
if (err) {
return callback(err);
}
convertedSpecs.push(result);
return callback(null);
});
})
.catch((err) => {
return callback(err);
});
}, (err) => {
if (err) {
return cb({
result: false,
reason: 'input type:' + input.type + ' is not valid'
});
}
var conversionResult = false,
convertedCollections = [],
reasonForFail;
_.forEach(convertedSpecs, (convertedSpec) => {
if (convertedSpec.result) {
let result;
conversionResult = conversionResult || convertedSpec.result;
function returnCollection (resultSpec) {
if (resultSpec.type === 'collection') {
return true;
}
}
// filtering out the collections from the convertedSpec
result = convertedSpec.output.filter(returnCollection);
convertedCollections.push(result[0]);
}
else {
conversionResult = conversionResult || convertedSpec.result;
reasonForFail = convertedSpec.reason;
}
});
if (conversionResult) {
return cb(null, {
result: true,
output: convertedCollections
});
}
return cb(null, {
result: false,
reason: reasonForFail
});
});
}
// eslint-disable-next-line no-else-return
else {
return cb(null, {
result: false,
reason: 'input type:' + input.type + ' is not valid'
});
}
return cb(null, schema.validationResult);
},
validate: function(input) {
validate: function (input) {
var schema = new SchemaPack(input);
return schema.validationResult;
},
mergeAndValidate: function (input, cb) {
var schema = new SchemaPack(input);
schema.mergeAndValidate(cb);
},
getOptions: function() {
return SchemaPack.getOptions();
},

View File

@@ -6,11 +6,17 @@ const COLLECTION_NAME = 'Converted from OpenAPI',
async = require('async'),
sdk = require('postman-collection'),
schemaUtils = require('./schemaUtils.js'),
OasResolverOptions = {
resolve: true, // Resolve external references
jsonSchema: true // Treat $ref like JSON Schema and convert to OpenAPI Schema Objects
},
parse = require('./parse.js'),
getOptions = require('./options').getOptions,
transactionSchema = require('../assets/validationRequestListSchema.json'),
utils = require('./utils.js'),
_ = require('lodash'),
fs = require('fs'),
// options for oas-resolver
// This provides the base class for
// errors with the input OpenAPI spec
@@ -93,6 +99,14 @@ class SchemaPack {
return this.validationResult;
}
}
else if (input.type === 'folder') {
this.validationResult = {
result: false,
reason: 'Input data not validated, please call mergeAndValidate() for input.type \'folder\''
};
return this.validationResult;
}
else {
// invalid input type
this.validationResult = {
@@ -122,6 +136,36 @@ class SchemaPack {
return this.validationResult;
}
mergeAndValidate (cb) {
let input = this.input,
validationResult,
rootFiles;
rootFiles = parse.getRootFiles(input.data);
if (rootFiles.length > 1) {
this.validationResult = {
result: false,
reason: 'More than one root file not supported.'
};
return cb(null, this.validationResult);
}
if (rootFiles.length) {
parse.mergeFiles(rootFiles[0], OasResolverOptions)
.then((spec) => {
this.input = {
type: 'json',
data: spec
};
validationResult = this.validate();
validationResult.openapi = this.openapi;
return cb(null, validationResult);
})
.catch((err) => {
return cb(err);
});
}
}
// convert method, this is called when you want to convert a schema that you've already loaded
// in the constructor
convert (callback) {