json-schema-to-openapi-schema
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -9,4 +9,11 @@ # Changelog | ||
## [0.2.0] - 2018-05-10 | ||
### Fixed | ||
* Implemented [@cloudflare/json-schema-walker] to make sure all subschemas are | ||
processed | ||
[@cloudflare/json-schema-walker]: https://github.com/cloudflare/json-schema-tools#cloudflarejson-schema-walker | ||
## [0.1.1] - 2018-04-09 | ||
### Added | ||
* Convert `dependencies` to an allOf + oneOf OpenAPI-valid equivalent |
122
index.js
@@ -1,2 +0,2 @@ | ||
const structs = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties']; | ||
const schemaWalker = require('@cloudflare/json-schema-walker'); | ||
@@ -10,18 +10,17 @@ function InvalidTypeError(message) { | ||
function convert(schema, options) { | ||
options = options || {}; | ||
options.cloneSchema = ! (options.cloneSchema === false); | ||
function convert(schema, options = {}) { | ||
const { cloneSchema = true } = options; | ||
if (options.cloneSchema) { | ||
if (cloneSchema) { | ||
schema = JSON.parse(JSON.stringify(schema)); | ||
} | ||
schema = removeRootKeywords(schema); | ||
schema = convertSchema(schema); | ||
const vocab = schemaWalker.getVocabulary(schema, schemaWalker.vocabularies.DRAFT_04); | ||
schemaWalker.schemaWalk(schema, convertSchema, null, vocab); | ||
return schema; | ||
} | ||
function removeRootKeywords(schema) { | ||
function stripIllegalKeywords(schema) { | ||
delete schema['$schema']; | ||
delete schema['$id']; | ||
delete schema['id']; | ||
@@ -31,37 +30,4 @@ return schema; | ||
function convertSchema(schema) { | ||
let i = 0; | ||
let j = 0; | ||
let struct = null; | ||
for (i; i < structs.length; i++) { | ||
struct = structs[i]; | ||
if (Array.isArray(schema[struct])) { | ||
for (j; j < schema[struct].length; j++) { | ||
schema[struct][j] = convertSchema(schema[struct][j]); | ||
} | ||
} else if (typeof schema[struct] === 'object') { | ||
schema[struct] = convertSchema(schema[struct]); | ||
} | ||
} | ||
if (typeof schema.properties === 'object') { | ||
schema.properties = convertProperties(schema.properties); | ||
if (Array.isArray(schema.required)) { | ||
schema.required = cleanRequired(schema.required, schema.properties); | ||
if (schema.required.length === 0) { | ||
delete schema.required; | ||
} | ||
} | ||
if (Object.keys(schema.properties).length === 0) { | ||
delete schema.properties; | ||
} | ||
} | ||
validateType(schema.type); | ||
function convertSchema(schema, path, parent, parentPath) { | ||
schema = stripIllegalKeywords(schema); | ||
schema = convertTypes(schema); | ||
@@ -86,15 +52,2 @@ schema = convertDependencies(schema); | ||
function convertProperties(properties) { | ||
let key = {}; | ||
let property = {}; | ||
let props = {}; | ||
for (key in properties) { | ||
property = properties[key]; | ||
props[key] = convertSchema(property); | ||
} | ||
return props; | ||
} | ||
function convertDependencies(schema) { | ||
@@ -146,4 +99,2 @@ const deps = schema.dependencies; | ||
function convertTypes(schema) { | ||
var newType; | ||
if (schema.type === undefined) { | ||
@@ -153,28 +104,32 @@ return schema; | ||
// type needs to be a string, not an array | ||
if (schema.type instanceof Array && schema.type.includes('null')) { | ||
var numTypes = schema.type.length; | ||
validateType(schema.type); | ||
schema.nullable = true; | ||
if (Array.isArray(schema.type)) { | ||
// if it was just type: ['null'] for some reason | ||
switch (numTypes) { | ||
case 1: | ||
// Didn't know what else to do | ||
newType = 'string'; | ||
if (schema.type.length > 2 || !schema.type.includes('null')) { | ||
throw new Error('Type of ' + schema.type.join(',') + ' is too confusing for OpenAPI to understand. Found in ' + JSON.stringify(schema)); | ||
} | ||
switch (schema.type.length) { | ||
case 0: | ||
delete schema.type; | ||
break; | ||
case 2: | ||
newType = schema.type.find(function(element) { | ||
return element !== 'null'; | ||
}); | ||
case 1: | ||
if (schema.type === 'null') { | ||
schema.nullable = true; | ||
} | ||
else { | ||
schema.type = schema.type[0]; | ||
} | ||
break; | ||
default: | ||
throw 'type cannot be an array, and you have ' + numTypes + ' types'; | ||
schema.type = schema.type.find(type => type !== 'null'); | ||
schema.nullable = true; | ||
} | ||
} | ||
if (newType) { | ||
schema.type = newType; | ||
else if (schema.type === 'null') { | ||
delete schema.type; | ||
schema.nullable = true; | ||
} | ||
@@ -193,17 +148,2 @@ | ||
function cleanRequired(required, properties) { | ||
var i = 0; | ||
required = required || []; | ||
properties = properties || {}; | ||
for (i; i < required.length; i++) { | ||
if (properties[required[i]] === undefined) { | ||
required.splice(i, 1); | ||
} | ||
} | ||
return required; | ||
} | ||
module.exports = convert; |
{ | ||
"name": "json-schema-to-openapi-schema", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Converts a JSON Schema to OpenAPI Schema Object", | ||
@@ -18,3 +18,6 @@ "main": "index.js", | ||
"should": "^13.2.0" | ||
}, | ||
"dependencies": { | ||
"@cloudflare/json-schema-walker": "^0.1.1" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# JSON Schema to OpenAPI Schema | ||
# JSON Schema to OpenAPI Schema | ||
@@ -47,2 +47,4 @@ A little NodeJS package to convert JSON Schema to [OpenAPI Schema Objects](https://swagger.io/specification/#schemaObject). | ||
**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. | ||
### Options | ||
@@ -76,11 +78,8 @@ | ||
- [ ] Support later JSON Schema drafts through a yet-to-be-released json-schema-migrator package | ||
- [ ] Support later JSON Schema drafts via [cloudflare/json-schema-transformer] when it adds that functionality | ||
## Converting Back | ||
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. | ||
To convert the other way, check out [openapi-schema-to-json-schema], which this package was based on. | ||
**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. | ||
## Tests | ||
@@ -103,1 +102,3 @@ | ||
[openapi-schema-to-json-schema]: https://github.com/mikunn/openapi-schema-to-json-schema | ||
[link-contributors]: https://github.com/wework/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 |
@@ -20,2 +20,22 @@ 'use strict'; | ||
it('supports nullables inside sub-schemas', () => { | ||
const schema = { | ||
$schema: 'http://json-schema.org/draft-04/schema#', | ||
oneOf: [ | ||
{ type: 'string' }, | ||
{ type: 'null' } | ||
] | ||
}; | ||
const result = convert(schema); | ||
should(result).deepEqual({ | ||
oneOf: [ | ||
{ type: 'string' }, | ||
{ nullable: true } | ||
] | ||
}); | ||
}); | ||
it('does not add nullable for non null types', () => { | ||
@@ -22,0 +42,0 @@ const schema = { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
42585
26
1556
102
0
1
+ Added@cloudflare/json-schema-walker@0.1.1(transitive)