mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
add support for new error type openApiErr.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
var sdk = require('postman-collection'),
|
||||
util = require('./util.js'),
|
||||
_ = require('lodash'),
|
||||
openApiErr = require('./error.js'),
|
||||
|
||||
COLLECTION_NAME = 'OPENAPI 3.0',
|
||||
|
||||
@@ -57,6 +58,9 @@ var sdk = require('postman-collection'),
|
||||
openapi.servers = _.isEmpty(openapi.servers) ? [{ url: '/' }] : openapi.servers;
|
||||
openapi.securityDefs = _.get(openapi, 'components.securitySchemes', {});
|
||||
openapi.baseUrl = _.get(openapi, 'servers[0].url');
|
||||
if (!openapi.baseUrl) {
|
||||
throw new openApiErr('openapi should have valid url in each servers object');
|
||||
}
|
||||
openapi.baseUrlVariables = _.get(openapi, 'servers[0].variables');
|
||||
|
||||
|
||||
@@ -137,6 +141,14 @@ module.exports = function(json, options, callback) {
|
||||
converter.convert(json, options, callback);
|
||||
}
|
||||
catch (e) {
|
||||
callback(e.toString());
|
||||
if (e instanceof openApiErr) {
|
||||
callback(null, {
|
||||
result: false,
|
||||
reason: e.message
|
||||
});
|
||||
}
|
||||
else {
|
||||
callback(e.toString());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
40
lib/deref.js
40
lib/deref.js
@@ -1,21 +1,23 @@
|
||||
var type = {
|
||||
integer: {
|
||||
int32: '<integer>',
|
||||
int64: '<long>'
|
||||
},
|
||||
number: {
|
||||
float: '<float>',
|
||||
double: '<double>'
|
||||
},
|
||||
string: {
|
||||
byte: '<byte>',
|
||||
binary: '<binary>',
|
||||
date: '<date>',
|
||||
'date-time': '<dateTime>',
|
||||
password: '<password>'
|
||||
},
|
||||
boolean: '<boolean>'
|
||||
};
|
||||
const openApiErr = require('./error.js'),
|
||||
type = {
|
||||
integer: {
|
||||
int32: '<integer>',
|
||||
int64: '<long>'
|
||||
},
|
||||
number: {
|
||||
float: '<float>',
|
||||
double: '<double>'
|
||||
},
|
||||
string: {
|
||||
byte: '<byte>',
|
||||
binary: '<binary>',
|
||||
date: '<date>',
|
||||
'date-time': '<dateTime>',
|
||||
password: '<password>'
|
||||
},
|
||||
boolean: '<boolean>'
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* Resolves references to components for a given schema.
|
||||
@@ -35,7 +37,7 @@ module.exports = {
|
||||
splitRef = schema.$ref.split('/');
|
||||
|
||||
if (splitRef.length !== 4) {
|
||||
return { value: 'Unsupported $ref format ' + schema.$ref };
|
||||
throw new openApiErr(`ref ${schema.$ref} not found.`);
|
||||
}
|
||||
|
||||
savedSchemaName = splitRef.slice(3)[0];
|
||||
|
||||
12
lib/error.js
Normal file
12
lib/error.js
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* constructor openApiErr
|
||||
* @constructor
|
||||
* @param {*} message errorMessage
|
||||
*/
|
||||
function openApiErr(message) {
|
||||
this.message = message || '';
|
||||
}
|
||||
|
||||
openApiErr.prototype = Error();
|
||||
|
||||
module.exports = openApiErr;
|
||||
15
lib/util.js
15
lib/util.js
@@ -2,7 +2,8 @@ var sdk = require('postman-collection'),
|
||||
schemaFaker = require('json-schema-faker'),
|
||||
parse = require('./parse.js'),
|
||||
deref = require('./deref.js'),
|
||||
_ = require('lodash');
|
||||
_ = require('lodash'),
|
||||
openApiErr = require('./error.js');
|
||||
|
||||
const URLENCODED = 'application/x-www-form-urlencoded',
|
||||
APP_JSON = 'application/json',
|
||||
@@ -941,7 +942,15 @@ module.exports = {
|
||||
var refObj, savedSchema;
|
||||
|
||||
savedSchema = $ref.split('/').slice(2);
|
||||
refObj = this.components[savedSchema[0]][savedSchema[1]];
|
||||
if (savedSchema.length !== 2) {
|
||||
throw new openApiErr(`ref ${$ref} not found.`);
|
||||
}
|
||||
|
||||
refObj = _.get(this.components, [savedSchema[0], savedSchema[1]]);
|
||||
if (!refObj) {
|
||||
throw new openApiErr(`ref ${$ref} not found.`);
|
||||
}
|
||||
|
||||
if (refObj.$ref) {
|
||||
return this.getRefObject(refObj.$ref);
|
||||
}
|
||||
@@ -1022,7 +1031,7 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
if (!reqName) {
|
||||
throw new Error(`requestName (${this.options.requestName})` +
|
||||
throw new openApiErr(`requestName (${this.options.requestName})` +
|
||||
` in options is invalid or property does not exist in ${operationItem.path}`);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ var expect = require('chai').expect,
|
||||
Utils = require('../../lib/util.js'),
|
||||
deref = require('../../lib/deref.js'),
|
||||
fs = require('fs'),
|
||||
// sdk = require('postman-collection'),
|
||||
openApiErr = require('../../lib/error.js'),
|
||||
path = require('path'),
|
||||
VALID_OPENAPI_PATH = '../data/valid_openapi',
|
||||
INVALID_OPENAPI_PATH = '../data/invalid_openapi';
|
||||
@@ -11,38 +11,67 @@ var expect = require('chai').expect,
|
||||
/* Utility function Unit tests */
|
||||
describe('UTILITY FUNCTION TESTS ', function () {
|
||||
describe('safeSchemaFaker Function ', function() {
|
||||
var schema = {
|
||||
anyOf: [
|
||||
{
|
||||
it('should return result supports json-schema', function(done) {
|
||||
var schema = {
|
||||
anyOf: [{
|
||||
'$ref': '#/components/schemas/schema1'
|
||||
},
|
||||
{
|
||||
'$ref': '#/components/schemas/schema2'
|
||||
}]
|
||||
},
|
||||
components = {
|
||||
schemas: {
|
||||
schema1: {
|
||||
anyOf: [{
|
||||
'$ref': '#/components/schemas/schema3'
|
||||
}]
|
||||
},
|
||||
schema2: {
|
||||
type: 'string'
|
||||
},
|
||||
schema3: {
|
||||
$ref: '#/components/schemas/schema2'
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
components = {
|
||||
schemas: {
|
||||
schema1: {
|
||||
anyOf: [
|
||||
{
|
||||
};
|
||||
|
||||
expect(Utils.safeSchemaFaker(schema, components)).to.equal('<string>');
|
||||
done();
|
||||
});
|
||||
|
||||
it('should throw error ref not found', function(done) {
|
||||
var schema = {
|
||||
anyOf: [{
|
||||
'$ref': '#/components/schemas/schema1'
|
||||
},
|
||||
{
|
||||
'$ref': '#/components/schemas/schema2'
|
||||
}]
|
||||
},
|
||||
components = {
|
||||
schemas: {
|
||||
schema1: {
|
||||
anyOf: [{
|
||||
'$ref': '#/components/schemas/schema4'
|
||||
},
|
||||
{
|
||||
'$ref': '#/components'
|
||||
}
|
||||
]
|
||||
},
|
||||
schema2: {
|
||||
type: 'string'
|
||||
},
|
||||
schema4: {
|
||||
$ref: '#/components/schem2'
|
||||
]
|
||||
},
|
||||
schema2: {
|
||||
type: 'string'
|
||||
},
|
||||
schema4: {
|
||||
$ref: '#/components/schem2'
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
it('should return result supports json-schema', function(done) {
|
||||
Utils.safeSchemaFaker(schema, components);
|
||||
};
|
||||
|
||||
expect(function() {
|
||||
Utils.safeSchemaFaker(schema, components);
|
||||
}).to.throw(openApiErr, 'ref #/components/schem2 not found.');
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -1018,7 +1047,7 @@ describe('CONVERT FUNCTION TESTS ', function() {
|
||||
});
|
||||
});
|
||||
it('Should generate collection conforming to schema for and fail if not valid ' +
|
||||
specPath, function(done) {
|
||||
specPath1, function(done) {
|
||||
Converter.convert({ type: 'file', data: specPath1 }, { requestName: 'url' }, (err, conversionResult) => {
|
||||
expect(err).to.be.null;
|
||||
expect(conversionResult.result).to.equal(true);
|
||||
@@ -1037,9 +1066,10 @@ describe('CONVERT FUNCTION TESTS ', function() {
|
||||
|
||||
it('for invalid request name, converter should throw an error', function(done) {
|
||||
var openapi = fs.readFileSync(specPath, 'utf8');
|
||||
Converter.convert({ type: 'string', data: openapi }, { requestName: 'uKnown' }, (err) => {
|
||||
expect(err.toString()).to.equal(
|
||||
'Error: requestName (uKnown) in options is invalid or property does not exist in pets');
|
||||
Converter.convert({ type: 'string', data: openapi }, { requestName: 'uKnown' }, (err, conversionResult) => {
|
||||
expect(err).to.be.null;
|
||||
expect(conversionResult.reason).to.equal(
|
||||
'requestName (uKnown) in options is invalid or property does not exist in pets');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user