Created
July 16, 2020 17:47
-
-
Save adaptive-shield-matrix/2b320d671977b33d39b91c9321a85e98 to your computer and use it in GitHub Desktop.
openapi2jsonSchema
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/openapi-contrib/openapi-schema-to-json-schema#readme | |
const toJsonSchema = require("@openapi-contrib/openapi-schema-to-json-schema") | |
const chalk = require("chalk") | |
const fs = require("fs") | |
const refParser = require("json-schema-ref-parser") | |
const myArgs = process.argv.slice(2) | |
const openApiFile = myArgs[0] | |
const destDir = myArgs[1] | |
if (myArgs.length != 2) { | |
console.log(chalk.cyan("\nDereference + convert openAPI Specification to JSON-Schema-Draft-4")); | |
console.log(chalk.cyan("Usage: import-openapi-file export-json-schema-dir\n")); | |
process.exit(1); | |
} | |
if (!fs.existsSync(openApiFile)) { | |
console.log(`Source openAPI file ${openApiFile} does not exist!`) | |
process.exit(1) | |
} | |
async function convertOpenApi(openApiFile, dstDir) { | |
const openapi = await refParser.dereference(openApiFile) // deref all $ref pointers | |
const schemas = Object.keys(openapi.components.schemas) | |
console.log("schemas", schemas) | |
for (let s of schemas) { | |
convertOpenApiSingleSchema(openapi, s, dstDir) | |
} | |
} | |
async function convertOpenApiSingleSchema(openapi, schemaName, dstDir) { | |
const schema = openapi.components.schemas[schemaName] // select only relevant part | |
if (!schema) { | |
console.log( | |
chalk.red( | |
`\nSchema "${schemaName}" not found in openapi.components.schemas\n` | |
) | |
) | |
console.log( | |
chalk.red( | |
`Choose one of:\n${Object.keys(openapi.components.schemas).join("\n")}` | |
) | |
) | |
process.exit(1) | |
} | |
const destFile = dstDir + "/" + schemaName +".json" | |
if (!schema) { | |
console.log( | |
chalk.red( | |
`\nSchema "${schemaName}" not found in openapi.components.schemas\n` | |
) | |
) | |
console.log( | |
chalk.red( | |
`Choose one of:\n${Object.keys(openapi.components.schemas).join("\n")}` | |
) | |
) | |
process.exit(1) | |
} | |
const jSchema = JSON.stringify(toJsonSchema(schema), null, 2) // converte to Draft-4 (removes examples & co.) | |
fs.writeFileSync(destFile, jSchema, null, 4) // write the file | |
console.log( | |
chalk.green( | |
`Wrote validation Schema (JSON-Schema-Draft-4) to file: ${destFile}\n` | |
) | |
) | |
if (!isSchemaSecure(require(path.resolve(destFile)))) { | |
console.log(chalk.bold.red(`\nYour Schema contains unsafe regex!`)) | |
} | |
} | |
convertOpenApi(openApiFile, destDir) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "any-project", | |
"version": "1.0.0", | |
"dependencies": { | |
"@openapi-contrib/openapi-schema-to-json-schema": "^3.0.3", | |
"chalk": "^4.1.0", | |
"json-schema-ref-parser": "^9.0.3", | |
"speccy": "^0.11.0", | |
"swagmock": "^1.0.0" | |
}, | |
"scripts": { | |
"serve": "speccy serve src/main/resources/spec/openapi.yaml", | |
"lint": "speccy lint src/main/resources/spec/openapi.yaml", | |
"generate:json-schema:public": "node src/main/js/openapi2jsonSchema.js src/main/resources/spec/openapi-template.yaml src/main/resources/schema", | |
"generate:json-schema:private": "node src/main/js/openapi2jsonSchema.js src/main/resources/spec/private-template.yaml src/main/resources/schema" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment