Skip to content

Commit 018b476

Browse files
authored
Merge pull request #11 from JaredCE/make-nulls-nullable
Update Convertor.js
2 parents 7b356f7 + f3a5a7a commit 018b476

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-schema-for-openapi",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
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

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Convertor {
1111
this.specialProperties = ['allOf', 'anyOf', 'items', 'oneOf', 'not', 'properties']
1212
this.ofProperties = ['allOf', 'anyOf', 'oneOf']
1313
this.referencedSchemas = {}
14-
this.bannedKeyWords = ['$schema', '$comment', '$id', 'version']
14+
this.bannedKeyWords = ['$schema', '$comment', '$id', 'version', 'examples']
1515

1616
this.components = {
1717
schemas: {}
@@ -54,6 +54,15 @@ class Convertor {
5454
}
5555

5656
bannedWordsRemoval()
57+
58+
const convertNull = () => {
59+
if (schema.type === 'null') {
60+
schema.nullable = true
61+
delete schema.type
62+
}
63+
}
64+
65+
convertNull()
5766

5867
if (this.specialProperties.indexOf(parentKeyword) !== -1) {
5968
if (this.ofProperties.indexOf(parentKeyword) !== -1) {

test/schemas/complex-null.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"$id": "http://example.com/build/complex-null.json",
4+
"version": "1.1.1",
5+
"title": "JSON API Schema",
6+
"description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
7+
"type": "object",
8+
"required": [
9+
"errors"
10+
],
11+
"properties": {
12+
"errors": {
13+
"type": "object",
14+
"properties": {
15+
"message": {
16+
"oneOf": [
17+
{
18+
"$ref": "#/definitions/message"
19+
},
20+
{
21+
"type": "null"
22+
}
23+
]
24+
}
25+
}
26+
}
27+
},
28+
"definitions": {
29+
"message": {
30+
"type": "string"
31+
}
32+
}
33+
}

test/src/Convertor.spec.js

+22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const moreComplexOneOfSchema = require('../schemas/morecomplex-oneOf')
1818
const complexPropertySchema = require('../schemas/complex-property')
1919
const complexPropertyDefinitionSchema = require('../schemas/complex-propertyDefinition')
2020
const complexResolvedDefinitionSchema = require('../schemas/complex-resolvedDefinition')
21+
const complexNullTypeSchema = require('../schemas/complex-null')
2122

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

@@ -36,6 +37,7 @@ describe('Convertor', () => {
3637
delete require.cache[require.resolve('../schemas/complex-property')];
3738
delete require.cache[require.resolve('../schemas/complex-propertyDefinition')];
3839
delete require.cache[require.resolve('../schemas/complex-resolvedDefinition')];
40+
delete require.cache[require.resolve('../schemas/complex-null')];
3941
convertor = new Convertor(simpleSchema)
4042
});
4143

@@ -369,4 +371,24 @@ describe('Convertor', () => {
369371
expect(valid).to.be.true
370372
});
371373
});
374+
375+
describe('convert a schema with null types', () => {
376+
it('should return a schema valid for OpenAPI v3.0.0', async function() {
377+
const complexConvertor = new Convertor(complexNullTypeSchema)
378+
const components = complexConvertor.convert()
379+
const cloned = JSON.parse(JSON.stringify(simpleOpenAPI))
380+
let valid = await validator.validateInner(cloned, {})
381+
expect(valid).to.be.true
382+
Object.assign(cloned, {components})
383+
expect(cloned).to.have.property('components')
384+
expect(cloned.components).to.have.property('schemas')
385+
expect(cloned.components.schemas).to.have.property('main')
386+
expect(cloned.components.schemas.main).to.not.have.property('definitions')
387+
valid = await validator.validateInner(cloned, {})
388+
.catch(err => {
389+
console.log(err)
390+
})
391+
expect(valid).to.be.true
392+
});
393+
});
372394
});

0 commit comments

Comments
 (0)