Skip to content

Commit 7b2cdb2

Browse files
authored
Merge pull request #14 from JaredCE/remove-definitions
Remove definitions
2 parents 7170c90 + bb75079 commit 7b2cdb2

File tree

7 files changed

+127
-8
lines changed

7 files changed

+127
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-schema-for-openapi",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Converts a regular JSON Schema to a compatible OpenAPI 3 Schema Object, extracting out $ref schemas to their own schema object",
55
"keywords": [
66
"json",

src/Convertor.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class Convertor {
77
constructor(schema) {
88
this.schema = JSON.parse(JSON.stringify(schema))
99

10-
this.specialProperties = ['allOf', 'anyOf', 'items', 'oneOf', 'not', 'properties', 'additionalProperties']
11-
this.ofProperties = ['allOf', 'anyOf', 'oneOf']
10+
this.specialProperties = ['allOf', 'anyOf', 'items', 'oneOf', 'not', 'properties', 'additionalProperties',]
11+
this.ofProperties = ['allOf', 'anyOf', 'oneOf',]
1212
this.referencedSchemas = {}
13-
this.bannedKeyWords = ['$schema', '$comment', '$id', 'version', 'examples', 'id']
13+
this.bannedKeyWords = ['$schema', '$comment', '$id', 'version', 'examples', 'id',]
1414

1515
this.components = {
1616
schemas: {}
@@ -184,6 +184,14 @@ class Convertor {
184184
delete path.reduce((previous, current) => previous[current], this.schema)[pathKey]
185185
}
186186

187+
const removeDefinitions = (
188+
schema,
189+
) => {
190+
delete schema.definitions
191+
}
192+
193+
traverse(this.schema, removeDefinitions)
194+
187195
this.removeEmpty(this.schema)
188196

189197
// force remove definitions
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"type": "object",
3+
"additionalProperties": false,
4+
"required": [
5+
"clientId",
6+
"contact",
7+
"credentials"
8+
],
9+
"properties": {
10+
"contact": {
11+
"$id": "https: //json.schemastore.org/bungee-plugin",
12+
"$schema": "http: //json-schema.org/draft-07/schema#",
13+
"additionalProperties": true,
14+
"definitions": {
15+
"plugin-name": {
16+
"type": "string",
17+
"pattern": "^[A-Za-z0-9_\\.-]+$"
18+
}
19+
},
20+
"properties": {
21+
"name": {
22+
"description": "The name of the plugin.",
23+
"type": "string",
24+
"pattern": "^[A-Za-z0-9_\\.-]+$"
25+
},
26+
"main": {
27+
"description": "Plugin main class.",
28+
"type": "string",
29+
"pattern": "^([a-zA-Z_$][a-zA-Z\\d_$]*\\.)*[a-zA-Z_$][a-zA-Z\\d_$]*$"
30+
},
31+
"version": {
32+
"description": "Plugin version.",
33+
"type": "string"
34+
},
35+
"author": {
36+
"description": "Plugin author.",
37+
"type": "string"
38+
},
39+
"depends": {
40+
"description": "Plugin hard dependencies.",
41+
"type": "array",
42+
"items": {
43+
"type": "string",
44+
"pattern": "^[A-Za-z0-9_\\.-]+$"
45+
}
46+
},
47+
"softDepends": {
48+
"description": "Plugin soft dependencies.",
49+
"type": "array",
50+
"items": {
51+
"type": "string",
52+
"pattern": "^[A-Za-z0-9_\\.-]+$"
53+
}
54+
},
55+
"description": {
56+
"description": "Optional description.",
57+
"type": "string"
58+
}
59+
},
60+
"required": [
61+
"name",
62+
"main"
63+
],
64+
"title": "JSON schema for BungeeCord Plugin YAML",
65+
"type": "object"
66+
},
67+
"clientId": {
68+
"type": "string"
69+
},
70+
"credentials": {
71+
"type": "object",
72+
"additionalProperties": false,
73+
"properties": {
74+
"username": {
75+
"type": "string",
76+
"example": "user@example.com"
77+
},
78+
"password": {
79+
"type": "string"
80+
},
81+
"callbackURL": {
82+
"type": "string",
83+
"example": "http: //www.xyz.com"
84+
}
85+
}
86+
}
87+
}
88+
}

test/src/Convertor.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const complexTypeArraySchema = require('../schemas/complex-typeArray')
2424
const complexDefaultValuesSchema = require('../schemas/complex-defaultValues')
2525
const complexAdditionalPropertiesSchema = require('../schemas/complex-additionalProperties')
2626
const complexItemsAsArraySchema = require('../schemas/complex-itemsAsArray')
27+
const complexEmbeddedDefinitionsSchema = require('../schemas/complex-embeddedDefinitions')
2728

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

@@ -47,6 +48,7 @@ describe('Convertor', () => {
4748
delete require.cache[require.resolve('../schemas/complex-defaultValues')];
4849
delete require.cache[require.resolve('../schemas/complex-additionalProperties')];
4950
delete require.cache[require.resolve('../schemas/complex-itemsAsArray')];
51+
delete require.cache[require.resolve('../schemas/complex-embeddedDefinitions')];
5052
convertor = new Convertor(simpleSchema)
5153
});
5254

@@ -359,6 +361,27 @@ describe('Convertor', () => {
359361
})
360362
expect(valid).to.be.true
361363
});
364+
365+
it('should return a schema valid for OpenAPI v3.0.0 when definitions are deeply embedded', async function() {
366+
const complexConvertor = new Convertor(complexEmbeddedDefinitionsSchema)
367+
const components = complexConvertor.convert()
368+
const cloned = JSON.parse(JSON.stringify(simpleOpenAPI))
369+
let valid = await validator.validateInner(cloned, {})
370+
expect(valid).to.be.true
371+
Object.assign(cloned, {components})
372+
expect(cloned).to.have.property('components')
373+
expect(cloned.components).to.have.property('schemas')
374+
expect(cloned.components.schemas).to.have.property('main')
375+
expect(cloned.components.schemas.main.properties).to.have.property('contact')
376+
expect(cloned.components.schemas.main.properties.contact).to.not.have.property('definitions')
377+
expect(cloned.components.schemas.main.properties).to.have.property('clientId')
378+
expect(cloned.components.schemas.main.properties).to.have.property('credentials')
379+
valid = await validator.validateInner(cloned, {})
380+
.catch(err => {
381+
console.log(err)
382+
})
383+
expect(valid).to.be.true
384+
});
362385
});
363386

364387
describe('convert a schema that has definitions that have already been resolved', () => {

test/src/Factory.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ describe('Factory', () => {
2929
expect(expected.schemas).to.have.property('message')
3030
});
3131
});
32-
});
32+
});

test/src/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ describe('index', () => {
2929
expect(expected.schemas).to.have.property('message')
3030
});
3131
});
32-
});
32+
});

0 commit comments

Comments
 (0)