Skip to content

Commit 6e359ad

Browse files
committed
Using Swagger-OpenAPI + OpenAPI-Postman converters
1 parent 5d09db2 commit 6e359ad

File tree

6 files changed

+947
-722
lines changed

6 files changed

+947
-722
lines changed

.npmignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
/.gitmodules
55
/.coverage
66
/.vscode
7-
/tests
7+
/test
8+
standalone.js
9+
.travis.yml
10+
.eslintrc
811

912
### Borrowed from .gitignore
1013
### ========================

lib/convert.js

Lines changed: 27 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,50 @@
1-
var Collection = require('postman-collection').Collection,
2-
_ = require('lodash'),
1+
var _ = require('lodash'),
32
Helpers = require('./helpers.js'),
3+
Swagger2OpenAPI = require('swagger2openapi'),
4+
OpenAPI2Postman = require('openapi-to-postmanv2'),
45
Converter = null,
56
fs = require('fs');
67

78
Converter = {
89
// Static Props:
910
collection: null, // will hold the V2 collection object
1011

11-
createCollectionStructure: function(swaggerData, tree) {
12-
// this takes in the tree structure, and creates the collection struct
13-
// with folders and params and requests
14-
var itemToAdd, child;
15-
16-
// Add each of the root children to the collection items
17-
for (child in tree.children) {
18-
if (tree.children.hasOwnProperty(child)) {
19-
itemToAdd = Helpers.convertChildToItemGroup(swaggerData, tree.children[child]);
20-
itemToAdd && this.collection.items.add(itemToAdd);
21-
}
22-
}
23-
},
2412

2513
// takes in a swagger2 JSON object
2614
// returns a V2 collection JSON object
27-
convert: function(json, options, callback) {
28-
// No validation needed. If the app didn't call validate, this will throw an error
29-
var result = {
30-
result: true,
31-
output: []
32-
},
33-
parseResult,
34-
swaggerData = {},
35-
tree;
15+
convert: function (input, options, callback) {
16+
options = _.assign({}, options);
3617

18+
var parseResult = Helpers.parse(input);
19+
20+
if (!parseResult.result) {
21+
return callback(new Error('Invalid input'));
22+
}
3723
try {
38-
if (typeof json === 'string') {
39-
//parse
40-
parseResult = Helpers.parse(json);
41-
if (!parseResult.result) {
42-
return callback({
43-
result: false,
44-
reason: 'Invalid Swagger object'
45-
});
24+
return Swagger2OpenAPI.convertObj(parseResult.swagger, {
25+
fatal: false,
26+
warnOnly: true
27+
}, function(err, oas3Wrapper) {
28+
if (err) {
29+
return callback(err);
4630
}
47-
json = parseResult.swagger;
48-
}
49-
// options
50-
Helpers.options.schemaFaker = options.hasOwnProperty('schemaFaker') ? options.schemaFaker : true;
51-
Helpers.options.requestName = options.hasOwnProperty('requestName') ? options.requestName : 'fallback';
52-
// Set data needed for swagger->Postman conversion
5331

54-
// Set schema-wide input and output formats
55-
swaggerData.globalConsumes = json.consumes || [];
56-
swaggerData.globalProduces = json.produces || [];
57-
58-
// Read global properties from the JSON:
59-
swaggerData.basePath = Helpers.getBasePath(json);
60-
swaggerData.baseParams = json.parameters;
61-
62-
// Read definitions, response schemas, security schemes
63-
swaggerData.securityDefs = json.securityDefinitions; // global auth
64-
swaggerData.sampleDefinitions = json.definitions; // global schema defs
65-
swaggerData.sampleResponses = json.responses; // global sample responses
66-
67-
// Start building out collection:
68-
this.collection = new Collection();
69-
this.collection.name = json.info.title;
70-
this.collection.describe(json.info.description);
71-
this.collection.variables = _.map(swaggerData.sampleDefinitions);
72-
73-
tree = Helpers.getTreeFromPaths(json);
74-
this.createCollectionStructure(swaggerData, tree);
75-
76-
result.output.push({
77-
type: 'collection',
78-
data: this.collection.toJSON()
32+
return OpenAPI2Postman.convert({
33+
type: 'json',
34+
data: oas3Wrapper.openapi
35+
}, options, (error, result) => {
36+
if (error) {
37+
return callback('Error importing Swagger 2.0 spec');
38+
}
39+
else {
40+
return callback(null, result);
41+
}
42+
});
7943
});
8044
}
8145
catch (e) {
8246
return callback(e);
8347
}
84-
85-
return callback(null, result);
8648
}
8749
};
8850

0 commit comments

Comments
 (0)