added options for schemaFaker & reqName

This commit is contained in:
pavanteja-potnuru
2018-09-10 16:03:17 +05:30
parent a5dc21d81d
commit 2ee7be66f1
3 changed files with 19 additions and 12 deletions

View File

@@ -7,17 +7,17 @@ module.exports = {
convert: function(input, options, cb) {
try {
if (input.type === 'string') {
return convert(input.data, cb);
return convert(input.data, options, cb);
}
else if (input.type === 'json') {
return convert(input.data, cb);
return convert(input.data, options, cb);
}
else if (input.type === 'file') {
return fs.read(input.data, function(err, data) {
if (err) {
return cb(err);
}
return convert(data, cb);
return convert(data, options, cb);
});
}
return cb(null, {

View File

@@ -21,7 +21,7 @@ var sdk = require('postman-collection'),
}
},
convert: function (data, callback) {
convert: function (data, options, callback) {
var validation = util.parseSpec(data),
openapi = {},
description,
@@ -30,6 +30,9 @@ var sdk = require('postman-collection'),
if (!validation.result) {
callback(validation);
}
// options
util.options.requestName = options.hasOwnProperty('requestName') ? options.requestName : 'operationId';
util.options.schemaFaker = options.hasOwnProperty('schemaFaker') ? options.schemaFaker : true;
// TODO - Have to handle global level security scheme
@@ -109,6 +112,6 @@ var sdk = require('postman-collection'),
};
// module.exports = converter;
module.exports = function(json, callback) {
converter.convert(json, callback);
module.exports = function(json, options, callback) {
converter.convert(json, options, callback);
};

View File

@@ -95,6 +95,7 @@ class Trie {
module.exports = {
// list of predefined schemas in components
components: {},
options: {},
/**
* Adds the neccessary server variables to the collection
@@ -196,9 +197,11 @@ module.exports = {
i,
summary,
path,
xthis,
// creating a root node for the trie (serves as the root dir)
trie = new Trie(root);
xthis = this;
for (path in paths) {
if (paths.hasOwnProperty(path)) {
// decalring a variable to be in this loops context only
@@ -256,7 +259,7 @@ module.exports = {
currentNode.addMethod({
name: summary,
id: operationItem.operationId,
id: operationItem[xthis.options.requestName],
method: method,
path: path,
properties: operationItem,
@@ -297,7 +300,7 @@ module.exports = {
_.forEach(pathVariables, (variable) => {
variables.push({
key: variable.name,
value: safeSchemaFaker(variable.schema || {}, this.components),
value: this.options.schemaFaker ? safeSchemaFaker(variable.schema || {}, this.components) : '',
description: variable.description || ''
});
});
@@ -408,7 +411,7 @@ module.exports = {
_.forOwn(responseHeaders, (value, key) => {
header = {
key: key,
value: safeSchemaFaker(value.schema || {}, this.components),
value: this.options.schemaFaker ? safeSchemaFaker(value.schema || {}, this.components) : '',
description: value.description || ''
};
headerArray.push(header);
@@ -484,7 +487,7 @@ module.exports = {
// This part is to remove format:binary from any string-type properties
// will cause schemaFaker to crash if left untreated
if (bodyObj.hasOwnProperty('schema')) {
bodyData = safeSchemaFaker(bodyObj.schema || {}, this.components);
bodyData = this.options.schemaFaker ? safeSchemaFaker(bodyObj.schema || {}, this.components) : '';
}
else if (bodyObj.hasOwnProperty('examples')) {
bodyData = this.getExampleData(openapi, bodyObj.examples);
@@ -522,7 +525,8 @@ module.exports = {
_.forEach(queryParameters, (param) => {
// check for existence of schema
if (param.hasOwnProperty('schema')) {
paramValue = safeSchemaFaker(param.schema, this.components); // fake data generated
// fake data generated
paramValue = this.options.schemaFaker ? safeSchemaFaker(param.schema, this.components) : '';
paramType = param.schema.type;
// checking the type of the query parameter
if (paramType === 'array') {
@@ -607,7 +611,7 @@ module.exports = {
_.forEach(headers, (header) => {
if (header.hasOwnProperty('schema')) {
fakeData = safeSchemaFaker(header.schema || {}, this.components);
fakeData = options.schemaFaker ? safeSchemaFaker(header.schema || {}, this.components) : '';
}
else {
fakeData = '';