forked from ITISFoundation/osparc-simcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
34 lines (26 loc) · 1.26 KB
/
converter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const toOpenApi = require('json-schema-to-openapi-schema');
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml')
const inputPath = '/input';
const outputPath = '/output/';
const filenames = fs.readdirSync(inputPath)
filenames.forEach(filepath => {
const extName = path.extname(filepath);
if (extName == ".json") {
console.log("converting " + filepath + "...");
const contents = fs.readFileSync(inputPath + "/" + filepath, 'utf8');
const object = JSON.parse(contents);
const convertedSchema = toOpenApi(object);
console.log("converted " + filepath + " succesfully");
var yamlSchema = yaml.dump(convertedSchema)
// console.log(yamlSchema);
// there is a BUG here. examples in a schema are not allowed in openapi, they should be replaced by example
// Note that schemas and properties support singular example but not plural examples.
// [link to problem](https://swagger.io/docs/specification/adding-examples/)
yamlSchema = yamlSchema.replace(/examples:\n/g, "example:\n");
// write as yaml
fs.writeFileSync(outputPath + path.basename(filepath, ".json") + "-converted.yaml", yamlSchema);
// fs.writeFileSync(outputPath + "converted_" + filepath, JSON.stringify(convertedSchema));
}
});