@openapi-contrib/json-schema-to-openapi-schema
Advanced tools
Comparing version 1.0.1 to 1.1.0
23
index.js
@@ -9,2 +9,3 @@ 'use strict'; | ||
const readFileAsync = require('util').promisify(fs.readFile); | ||
const oas3schema = require('./refs/oas3-schema.json'); | ||
@@ -56,4 +57,6 @@ function InvalidTypeError(message) { | ||
schema.items = {}; | ||
} | ||
} | ||
// should be called last | ||
schema = convertIllegalKeywordsAsExtensions(schema); | ||
return schema; | ||
@@ -165,2 +168,13 @@ } | ||
// keywords (or property names) that are not recognized within OAS3 are rewritten into extensions. | ||
function convertIllegalKeywordsAsExtensions(schema) { | ||
Object.keys(schema) | ||
.filter(keyword => !keyword.startsWith(oasExtensionPrefix) && !allowedKeywords.includes(keyword)) | ||
.forEach(keyword => { | ||
schema[oasExtensionPrefix + keyword] = schema[keyword]; | ||
delete schema[keyword]; | ||
}); | ||
return schema; | ||
} | ||
function convertExamples(schema) { | ||
@@ -251,2 +265,9 @@ if (schema['examples'] && Array.isArray(schema['examples'])) { | ||
const oasExtensionPrefix = 'x-'; | ||
// TODO: having definitions inside an oas3 schema isn't exactly valid, | ||
// maybe it is an idea to extract and split them into multiple oas3 schemas and reference to them. | ||
// For now leaving as is. | ||
const allowedKeywords = ['$ref', 'definitions'].concat(Object.keys(oas3schema.definitions.Schema.properties)); | ||
module.exports = convert; |
{ | ||
"name": "@openapi-contrib/json-schema-to-openapi-schema", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Converts a JSON Schema to OpenAPI Schema Object", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"test": "nyc --reporter=html --reporter=text mocha", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls" | ||
"coverage": "nyc report --reporter=text-lcov" | ||
}, | ||
@@ -15,9 +15,10 @@ "repository": "github:openapi-contrib/json-schema-to-openapi-schema", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^3.0.0", | ||
"mocha": "^5.0.0", | ||
"nock": "^11.3.6", | ||
"nyc": "^11.6.0", | ||
"coveralls": "^3.0.11", | ||
"mocha": "^7.1.1", | ||
"nock": "^11.9.1", | ||
"nyc": "^15.0.1", | ||
"semantic-release": "^17.0.4", | ||
"should": "^13.2.0" | ||
@@ -28,6 +29,6 @@ }, | ||
"@stoplight/json-ref-resolver": "^2.3.0", | ||
"@stoplight/yaml": "^3.3.1", | ||
"@stoplight/yaml": "^3.7.1", | ||
"node-fetch": "^2.6.0", | ||
"tslib": "^1.10.0" | ||
"tslib": "^1.11.1" | ||
} | ||
} |
# JSON Schema to OpenAPI Schema | ||
A little NodeJS package to convert JSON Schema to [OpenAPI Schema Objects](https://swagger.io/specification/#schemaObject). | ||
A little NodeJS package to convert JSON Schema to a [OpenAPI Schema Object](http://spec.openapis.org/oas/v3.0.3.html#schema-object). | ||
[](https://treeware.earth) | ||
## Features | ||
* converts JSON Schema Draft 00 Wright (a.k.a draft v5) to OpenAPI 3.0 Schema Object | ||
* converts JSON Schema Draft 04 to OpenAPI 3.0 Schema Object | ||
* switches `type: ['foo', 'null']` to `type: foo` and `nullable: true` | ||
@@ -15,6 +17,8 @@ * supports deep structures with nested `allOf`s etc. | ||
``` shell | ||
npm install --save @openapi-contrib/json-schema-to-openapi-schema | ||
``` | ||
npm install --save json-schema-to-openapi-schema | ||
``` | ||
Requires NodeJS v10 or greater. | ||
## Usage | ||
@@ -25,3 +29,3 @@ | ||
```js | ||
const toOpenApi = require('json-schema-to-openapi-schema'); | ||
const convert = require('@openapi-contrib/json-schema-to-openapi-schema'); | ||
@@ -35,3 +39,3 @@ const schema = { | ||
(async () => { | ||
const convertedSchema = await toOpenApi(schema); | ||
const convertedSchema = await convert(schema); | ||
console.log(convertedSchema); | ||
@@ -66,8 +70,6 @@ })(); | ||
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. | ||
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. This has been solved for OpenAPI v3.1, but for those using OpenAPI v3.0, you can use this tool to solve [the divergence](https://apisyouwonthate.com/blog/openapi-and-json-schema-divergence). | ||
 | ||
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). | ||
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). | ||
@@ -78,10 +80,6 @@ | ||
- **From:** [JSON Schema Draft v5 †](http://json-schema.org/specification-links.html#draft-5) | ||
- **To:** [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md) | ||
- **To:** [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md) | ||
_† 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._ | ||
## TODO | ||
- [ ] Support later JSON Schema drafts via [cloudflare/json-schema-transformer] when it adds that functionality | ||
## Converting Back | ||
@@ -99,5 +97,9 @@ | ||
## Treeware | ||
This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/{venfor}/{package}) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats. | ||
## Thanks | ||
- [Stoplight][] for donating time and effort to this project, and many more. | ||
- [Stoplight][] for [donating time and effort](https://stoplight.io/blog/companies-supporting-open-source/) to this project, and many more. | ||
- [mikunn][] for creating [openapi-schema-to-json-schema] which this is based on. | ||
@@ -112,4 +114,3 @@ - [Phil Sturgeon][] for flipping that conversion script about face. | ||
[Phil Sturgeon]: https://github.com/philsturgeon | ||
[openapi-schema-to-json-schema]: https://github.com/mikunn/openapi-schema-to-json-schema | ||
[link-contributors]: https://github.com/openapi-js/json-schema-to-openapi-schema/graphs/contributors | ||
[cloudflare/json-schema-transformer]: https://github.com/cloudflare/json-schema-tools/blob/master/workspaces/json-schema-transform/README.md | ||
[openapi-schema-to-json-schema]: https://github.com/openapi-contrib/openapi-schema-to-json-schema | ||
[link-contributors]: https://github.com/openapi-contrib/json-schema-to-openapi-schema/graphs/contributors |
@@ -7,4 +7,4 @@ 'use strict'; | ||
['basic', 'address', 'calendar'].forEach(test => { | ||
it(`converts ${test}/openapi.json`, async () => { | ||
['basic', 'address', 'calendar', 'events'].forEach(test => { | ||
it(`converts ${test}/openapi.json`, async () => { | ||
const schema = getSchema(test + '/json-schema.json'); | ||
@@ -15,3 +15,3 @@ const result = await convert(schema); | ||
should(result).deepEqual(expected, 'converted'); | ||
}); | ||
}); | ||
@@ -18,0 +18,0 @@ it(`converting ${test}/openapi.json in place`, async () => { |
@@ -11,4 +11,6 @@ 'use strict'; | ||
$schema: 'http://json-schema.org/draft-04/schema#', | ||
foo: { | ||
$ref: '#/definitions/foo', | ||
properties: { | ||
foo: { | ||
$ref: '#/definitions/foo', | ||
}, | ||
}, | ||
@@ -15,0 +17,0 @@ definitions: { |
91263
39
3657
110
6
Updated@stoplight/yaml@^3.7.1
Updatedtslib@^1.11.1