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

Feature/add bundlev2 #746

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ module.exports = {
return schema.bundle();
},

bundleV2: function(input) {
var schema = new SchemaPack(input, _.has(input, 'options') ? input.options : {});
return schema.bundleV2();
},

// new API
SchemaPack
};
69 changes: 68 additions & 1 deletion lib/schemaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/sc
{ getConcreteSchemaUtils, isSwagger, validateSupportedVersion } = require('./common/versionUtils.js'),
async = require('async'),
sdk = require('postman-collection'),
$RefParser = require('@apidevtools/json-schema-ref-parser'),
schemaFaker = require('../assets/json-schema-faker.js'),
deref = require('./deref.js'),
_ = require('lodash'),
Expand Down Expand Up @@ -5362,5 +5363,71 @@ module.exports = {
},
MULTI_FILE_API_TYPE_ALLOWED_VALUE,

verifyDeprecatedProperties: verifyDeprecatedProperties
verifyDeprecatedProperties: verifyDeprecatedProperties,

/**
*
* @description Dereferences all ref paths and returns a single JSON schema string
*
* @param {{
* rootFiles: { path: string, content: string }[],
* data: { fileName: string, content: string }[]
* }} input - Multi file data
* @param {object} options - a standard list of options that's globally passed around. Check options.js for more.
*
* @returns {Promise<object>} Resolved schema string
*/
dereference: async (input, options = {}) => {
// Pre-process array data into map to optimize search later
const fileMap = new Map(),
parser = new $RefParser(),
version = input.specificationVersion ? input.specificationVersion : '3.0';

input.data.forEach((file) => {
fileMap.set(file.fileName, file.content);
});

// eslint-disable-next-line one-var
const promises = input.rootFiles.map(async (rootFile) => {
const bundledContent = await parser.dereference(rootFile.path, {
resolve: {
external: true,
file: {
canRead: (file) => {
// TODO: tweak the condition further to identify Postman-only URLs here
// for getting relative file data
const fileName = file.url.split('/').pop();
return fileMap.has(fileName);
},
read: async (file) => {
const fileName = file.url.split('/').pop();
return fileMap.get(fileName);
}
}
}
}),
result = { bundledContent };

if (options.includeReferenceMap) {
result.referenceMap = parser.$refs.values();
}

return result;
});

// eslint-disable-next-line one-var
const data = await Promise.all(promises);

return {
result: true,
output: {
type: 'bundledContent',
specification: {
type: 'OpenAPI',
version
},
data
}
};
}
};
4 changes: 2 additions & 2 deletions lib/schemapack.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ class SchemaPack {
}
});
input.rootFiles = inputContent;
return schemaUtils.processRelatedFiles(input, true, this.computedOptions);
return schemaUtils.dereference(input, this.computedOptions);
}
}
let adaptedRootFiles = input.rootFiles.map((rootFile) => {
Expand All @@ -823,7 +823,7 @@ class SchemaPack {
throw new Error('Root file content not found in data array');
}
input.rootFiles = adaptedRootFiles;
return schemaUtils.processRelatedFiles(input, true, this.computedOptions);
return schemaUtils.dereference(input, this.computedOptions);
}
}

Expand Down
74 changes: 74 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@
"postman-collection": "4.1.5",
"swagger2openapi": "7.0.8",
"traverse": "0.6.6",
"yaml": "1.10.2"
"yaml": "1.10.2",
"@apidevtools/json-schema-ref-parser": "9.1.2"
},
"author": "Postman Labs <help@getpostman.com>",
"license": "Apache-2.0",
Expand Down
Loading