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