From f9adc10459ab3fa8aaa1c852f18503f06bbe1273 Mon Sep 17 00:00:00 2001 From: umeshp7 <31915995+umeshp7@users.noreply.github.com> Date: Tue, 14 Jan 2020 16:21:59 +0530 Subject: [PATCH] Support for multi file schema in schemapack --- index.js | 93 ++++------------------------------------------- lib/schemapack.js | 44 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 85 deletions(-) diff --git a/index.js b/index.js index f34a4ae..c8a8bce 100644 --- a/index.js +++ b/index.js @@ -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(); }, diff --git a/lib/schemapack.js b/lib/schemapack.js index fbfd301..cc64212 100644 --- a/lib/schemapack.js +++ b/lib/schemapack.js @@ -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) {