Skip to content

Commit af642a7

Browse files
committed
Support OpenAPI 3.0 as source data
1 parent db63b60 commit af642a7

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

bin/node-red-nodegen.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var request = require('request');
2222
var yamljs = require('yamljs');
2323
var argv = require('minimist')(process.argv.slice(2));
2424
var colors = require('colors');
25+
var Converter = require('api-spec-converter');
2526
var nodegen = require('../lib/nodegen.js');
2627

2728
// Command options
@@ -113,10 +114,17 @@ if (argv.help || argv.h) {
113114
request(sourcePath, function (error, response, body) {
114115
if (!error) {
115116
data.src = JSON.parse(body);
116-
nodegen.swagger2node(data, options).then(function (result) {
117-
console.log('Success: ' + result);
118-
}).catch(function (error) {
119-
console.log('Error: ' + error);
117+
Converter.convert({
118+
from: data.src.openapi && data.src.openapi.startsWith('3.0') ? 'openapi_3' : 'swagger_2',
119+
to: 'swagger_2',
120+
source: data.src,
121+
}).then(function (convertedData) {
122+
data.src = convertedData.spec;
123+
nodegen.swagger2node(data, options).then(function (result) {
124+
console.log('Success: ' + result);
125+
}).catch(function (error) {
126+
console.log('Error: ' + error);
127+
});
120128
});
121129
} else {
122130
console.error(error);
@@ -163,18 +171,33 @@ if (argv.help || argv.h) {
163171
});
164172
}
165173
else {
174+
Converter.convert({
175+
from: data.src.openapi && data.src.openapi.startsWith('3.0') ? 'openapi_3' : 'swagger_2',
176+
to: 'swagger_2',
177+
source: data.src,
178+
}).then(function (convertedData) {
179+
data.src = convertedData.spec;
180+
nodegen.swagger2node(data, options).then(function (result) {
181+
console.log('Success: ' + result);
182+
}).catch(function (error) {
183+
console.log('Error: ' + error);
184+
});
185+
});
186+
}
187+
} else if (sourcePath.endsWith('.yaml')) {
188+
data.src = yamljs.load(sourcePath);
189+
console.log(JSON.stringify(data.src, null, 4)); // hoge
190+
Converter.convert({
191+
from: data.src.openapi && data.src.openapi.startsWith('3.0') ? 'openapi_3' : 'swagger_2',
192+
to: 'swagger_2',
193+
source: data.src,
194+
}).then(function (convertedData) {
195+
data.src = convertedData.spec;
166196
nodegen.swagger2node(data, options).then(function (result) {
167197
console.log('Success: ' + result);
168198
}).catch(function (error) {
169199
console.log('Error: ' + error);
170200
});
171-
}
172-
} else if (sourcePath.endsWith('.yaml')) {
173-
data.src = yamljs.load(sourcePath);
174-
nodegen.swagger2node(data, options).then(function (result) {
175-
console.log('Success: ' + result);
176-
}).catch(function (error) {
177-
console.log('Error: ' + error);
178201
});
179202
} else if (sourcePath.endsWith('.js')) {
180203
data.src = fs.readFileSync(sourcePath);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
],
4444
"dependencies": {
4545
"ajv": "6.12.0",
46+
"api-spec-converter": "2.11.0",
4647
"cldr": "5.5.4",
4748
"colors": "1.4.0",
4849
"csv-string": "3.1.8",

0 commit comments

Comments
 (0)