- Notifications
You must be signed in to change notification settings - Fork46
wework/json-schema-to-openapi-schema
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A little NodeJS package to convert JSON Schema toOpenAPI Schema Objects.
- converts JSON Schema Draft 00 Wright (a.k.a draft v5) to OpenAPI 3.0 Schema Object
- switches
type: ['foo', 'null']totype: fooandnullable: true - supports deep structures with nested
allOfs etc. - switches
patternPropertiestox-patternProperties - converts
dependenciesto an allOf + oneOf OpenAPI-valid equivalent
npm install --save json-schema-to-openapi-schemaHere's a small example to get the idea:
consttoOpenApi=require('json-schema-to-openapi-schema');constschema={'$schema':'http://json-schema.org/draft-04/schema#',type:['string','null'],format:'date-time',};constconvertedSchema=toOpenApi(schema);console.log(convertedSchema);
The example prints out
{type:'string',format:'date-time',nullable:true}
NOTE:$refs are not dereferenced. Use a dereferencer such asjson-schema-ref-parser prior to using this package.
The function acceptsoptions object as the second argument.
If set tofalse, converts the provided schema in place. Iftrue, clones the schema by converting it to JSON and back. The overhead of the cloning is usually negligible. Defaults totrue.
OpenAPI is often described as an extension of JSON Schema, but both specs have changed over time and grown independently. OpenAPI v2 was based on JSON Schema draft v4 with a long list of deviations, but OpenAPI v3 shrank that list, upping their support to draft v4 and making the list of discrepancies shorter. Despite OpenAPI v3 closing the gap, the issue of JSON Schema divergence has not been resolved fully.
Carefully writing JSON Schema for your data model kiiiinda works, but it is possible to write JSON Schema that creates invalid OpenAPI, and vice versa. For more on this, read the articleOpenAPI and JSON Schema Divergence.
This tool sets out to allow folks to convert from JSON Schema (their one source of truth for everything) to OpenAPI (a thing for HTML docs and making SDKs).
† Draft v5 is also known as Draft Wright 00, as the drafts are often named after the author, and this was the first one by A. Wright. Amongst other things, draft v5 aimed to rewrite the meta files, but the experiment failed, meaning we need to continue to use the draft v4 metafiles. Ugh.
- Support later JSON Schema drafts viacloudflare/json-schema-transformer when it adds that functionality
To convert the other way, check outopenapi-schema-to-json-schema, which this package was based on.
To run the test-suite:
npmtest- mikunn for creatingopenapi-schema-to-json-schema which this is based on
- Phil Sturgeon for flipping that conversion script about face
- All Contributors
About
A little NodeJS package to convert JSON Schema to OpenAPI Schema Objects
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.
