|
1 |
| -var Collection = require('postman-collection').Collection, |
2 |
| - _ = require('lodash'), |
| 1 | +var _ = require('lodash'), |
3 | 2 | Helpers = require('./helpers.js'),
|
| 3 | + Swagger2OpenAPI = require('swagger2openapi'), |
| 4 | + OpenAPI2Postman = require('openapi-to-postmanv2'), |
4 | 5 | Converter = null,
|
5 | 6 | fs = require('fs');
|
6 | 7 |
|
7 | 8 | Converter = {
|
8 | 9 | // Static Props:
|
9 | 10 | collection: null, // will hold the V2 collection object
|
10 | 11 |
|
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 |
| - }, |
24 | 12 |
|
25 | 13 | // takes in a swagger2 JSON object
|
26 | 14 | // 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); |
36 | 17 |
|
| 18 | + var parseResult = Helpers.parse(input); |
| 19 | + |
| 20 | + if (!parseResult.result) { |
| 21 | + return callback(new Error('Invalid input')); |
| 22 | + } |
37 | 23 | 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); |
46 | 30 | }
|
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 |
53 | 31 |
|
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 | + }); |
79 | 43 | });
|
80 | 44 | }
|
81 | 45 | catch (e) {
|
82 | 46 | return callback(e);
|
83 | 47 | }
|
84 |
| - |
85 |
| - return callback(null, result); |
86 | 48 | }
|
87 | 49 | };
|
88 | 50 |
|
|
0 commit comments