|
2 | 2 |
|
3 | 3 | A little NodeJS package to convert JSON Schema to [OpenAPI Schema Objects](https://swagger.io/specification/#schemaObject).
|
4 | 4 |
|
5 |
| -- **From:** [JSON Schema Draft v5](http://json-schema.org/specification-links.html#draft-5) |
6 |
| -- **To:** [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md) |
7 |
| - |
8 |
| -## Why? |
9 |
| - |
10 |
| -OpenAPI is often described as an extension of JSON Schema, but both specs have changed over time and grown independently. OpenAPI v2 based based on JSON Schema draft 4 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. |
11 |
| - |
12 |
| - |
13 |
| - |
14 |
| -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 article [_OpenAPI and JSON Schema Divergence_](https://blog.apisyouwonthate.com/openapi-and-json-schema-divergence-part-1-1daf6678d86e). |
15 |
| - |
16 |
| -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). |
17 |
| - |
18 |
| -## Converting Back |
19 |
| - |
20 |
| -To convert the other way, check out [openapi-schema-to-json-schema](https://github.com/mikunn/openapi-schema-to-json-schema), which this package was based on. |
21 |
| - |
22 | 5 | ## Features
|
23 | 6 |
|
24 | 7 | * converts JSON Schema Draft 00 Wright (a.k.a draft v5) to OpenAPI 3.0 Schema Object
|
25 | 8 | * switches `type: ['foo', 'null']` to `type: foo` and `nullable: true`
|
26 | 9 | * supports deep structures with nested `allOf`s etc.
|
27 | 10 | * switches `patternProperties` to `x-patternProperties` in the Schema Object
|
28 | 11 |
|
29 |
| -**NOTE**: `$ref`s are not dereferenced. Use a dereferencer such as [json-schema-ref-parser](https://www.npmjs.com/package/json-schema-ref-parser) prior to using this package. |
30 |
| - |
31 | 12 | ## Installation
|
32 | 13 |
|
33 | 14 | ```
|
@@ -71,6 +52,34 @@ The function accepts `options` object as the second argument.
|
71 | 52 | If set to `false`, converts the provided schema in place. If `true`, clones the schema by converting it to JSON and back. The overhead of the cloning is usually negligible. Defaults to `true`.
|
72 | 53 |
|
73 | 54 |
|
| 55 | +## Why? |
| 56 | + |
| 57 | +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. |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +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 article [_OpenAPI and JSON Schema Divergence_](https://blog.apisyouwonthate.com/openapi-and-json-schema-divergence-part-1-1daf6678d86e). |
| 62 | + |
| 63 | +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). |
| 64 | + |
| 65 | +## Versions |
| 66 | + |
| 67 | +- **From:** [JSON Schema Draft v5 †](http://json-schema.org/specification-links.html#draft-5) |
| 68 | +- **To:** [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md) |
| 69 | + |
| 70 | +_† 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._ |
| 71 | + |
| 72 | +## TODO |
| 73 | + |
| 74 | +- [ ] Support later JSON Schema drafts through a yet-to-be-released json-schema-migrator package |
| 75 | + |
| 76 | +## Converting Back |
| 77 | + |
| 78 | +To convert the other way, check out [openapi-schema-to-json-schema](https://github.com/mikunn/openapi-schema-to-json-schema), which this package was based on. |
| 79 | + |
| 80 | +**NOTE**: `$ref`s are not dereferenced. Use a dereferencer such as [json-schema-ref-parser](https://www.npmjs.com/package/json-schema-ref-parser) prior to using this package. |
| 81 | + |
| 82 | + |
74 | 83 | ## Tests
|
75 | 84 |
|
76 | 85 | To run the test-suite:
|
|
0 commit comments