Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-schema-for-openapi",
"version": "0.2.0",
"version": "0.2.1",
"description": "Converts a regular JSON Schema to a compatible OpenAPI 3 Schema Object, extracting out $ref schemas to their own schema object",
"keywords": [
"json",
Expand Down
14 changes: 11 additions & 3 deletions src/Convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class Convertor {
constructor(schema) {
this.schema = JSON.parse(JSON.stringify(schema))

this.specialProperties = ['allOf', 'anyOf', 'items', 'oneOf', 'not', 'properties', 'additionalProperties']
this.ofProperties = ['allOf', 'anyOf', 'oneOf']
this.specialProperties = ['allOf', 'anyOf', 'items', 'oneOf', 'not', 'properties', 'additionalProperties',]
this.ofProperties = ['allOf', 'anyOf', 'oneOf',]
this.referencedSchemas = {}
this.bannedKeyWords = ['$schema', '$comment', '$id', 'version', 'examples', 'id']
this.bannedKeyWords = ['$schema', '$comment', '$id', 'version', 'examples', 'id',]

this.components = {
schemas: {}
Expand Down Expand Up @@ -184,6 +184,14 @@ class Convertor {
delete path.reduce((previous, current) => previous[current], this.schema)[pathKey]
}

const removeDefinitions = (
schema,
) => {
delete schema.definitions
}

traverse(this.schema, removeDefinitions)

this.removeEmpty(this.schema)

// force remove definitions
Expand Down
88 changes: 88 additions & 0 deletions test/schemas/complex-embeddedDefinitions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"type": "object",
"additionalProperties": false,
"required": [
"clientId",
"contact",
"credentials"
],
"properties": {
"contact": {
"$id": "https: //json.schemastore.org/bungee-plugin",
"$schema": "http: //json-schema.org/draft-07/schema#",
"additionalProperties": true,
"definitions": {
"plugin-name": {
"type": "string",
"pattern": "^[A-Za-z0-9_\\.-]+$"
}
},
"properties": {
"name": {
"description": "The name of the plugin.",
"type": "string",
"pattern": "^[A-Za-z0-9_\\.-]+$"
},
"main": {
"description": "Plugin main class.",
"type": "string",
"pattern": "^([a-zA-Z_$][a-zA-Z\\d_$]*\\.)*[a-zA-Z_$][a-zA-Z\\d_$]*$"
},
"version": {
"description": "Plugin version.",
"type": "string"
},
"author": {
"description": "Plugin author.",
"type": "string"
},
"depends": {
"description": "Plugin hard dependencies.",
"type": "array",
"items": {
"type": "string",
"pattern": "^[A-Za-z0-9_\\.-]+$"
}
},
"softDepends": {
"description": "Plugin soft dependencies.",
"type": "array",
"items": {
"type": "string",
"pattern": "^[A-Za-z0-9_\\.-]+$"
}
},
"description": {
"description": "Optional description.",
"type": "string"
}
},
"required": [
"name",
"main"
],
"title": "JSON schema for BungeeCord Plugin YAML",
"type": "object"
},
"clientId": {
"type": "string"
},
"credentials": {
"type": "object",
"additionalProperties": false,
"properties": {
"username": {
"type": "string",
"example": "user@example.com"
},
"password": {
"type": "string"
},
"callbackURL": {
"type": "string",
"example": "http: //www.xyz.com"
}
}
}
}
}
23 changes: 23 additions & 0 deletions test/src/Convertor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const complexTypeArraySchema = require('../schemas/complex-typeArray')
const complexDefaultValuesSchema = require('../schemas/complex-defaultValues')
const complexAdditionalPropertiesSchema = require('../schemas/complex-additionalProperties')
const complexItemsAsArraySchema = require('../schemas/complex-itemsAsArray')
const complexEmbeddedDefinitionsSchema = require('../schemas/complex-embeddedDefinitions')

const simpleOpenAPI = require('../openAPI/simple')

Expand All @@ -47,6 +48,7 @@ describe('Convertor', () => {
delete require.cache[require.resolve('../schemas/complex-defaultValues')];
delete require.cache[require.resolve('../schemas/complex-additionalProperties')];
delete require.cache[require.resolve('../schemas/complex-itemsAsArray')];
delete require.cache[require.resolve('../schemas/complex-embeddedDefinitions')];
convertor = new Convertor(simpleSchema)
});

Expand Down Expand Up @@ -359,6 +361,27 @@ describe('Convertor', () => {
})
expect(valid).to.be.true
});

it('should return a schema valid for OpenAPI v3.0.0 when definitions are deeply embedded', async function() {
const complexConvertor = new Convertor(complexEmbeddedDefinitionsSchema)
const components = complexConvertor.convert()
const cloned = JSON.parse(JSON.stringify(simpleOpenAPI))
let valid = await validator.validateInner(cloned, {})
expect(valid).to.be.true
Object.assign(cloned, {components})
expect(cloned).to.have.property('components')
expect(cloned.components).to.have.property('schemas')
expect(cloned.components.schemas).to.have.property('main')
expect(cloned.components.schemas.main.properties).to.have.property('contact')
expect(cloned.components.schemas.main.properties.contact).to.not.have.property('definitions')
expect(cloned.components.schemas.main.properties).to.have.property('clientId')
expect(cloned.components.schemas.main.properties).to.have.property('credentials')
valid = await validator.validateInner(cloned, {})
.catch(err => {
console.log(err)
})
expect(valid).to.be.true
});
});

describe('convert a schema that has definitions that have already been resolved', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/src/Factory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ describe('Factory', () => {
expect(expected.schemas).to.have.property('message')
});
});
});
});
2 changes: 1 addition & 1 deletion test/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ describe('index', () => {
expect(expected.schemas).to.have.property('message')
});
});
});
});