Skip to content

Commit ff05ac3

Browse files
authored
Merge pull request #2 from JaredAAT/attempt-to-convert-default-values
Attempt to convert default values
2 parents 06ba749 + 949c077 commit ff05ac3

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

src/Convertor.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,37 @@ class Convertor {
8989
}
9090
}
9191

92+
const convertDefaultValues = () => {
93+
if (schema.type === 'boolean') {
94+
if (schema.default === 'true' || schema.default === 'false') {
95+
if (schema.default === 'true')
96+
schema.default = true
97+
else
98+
schema.default = false
99+
}
100+
}
101+
102+
if (schema.type === 'number' || schema.type === 'integer') {
103+
if (typeof schema.default === 'string') {
104+
schema.default = parseInt(schema.default, 10)
105+
}
106+
}
107+
108+
if (schema.type === 'array' && schema.items === undefined) {
109+
schema.items = {nullable: true}
110+
}
111+
112+
if (schema.type === 'string') {
113+
if (Object.keys(schema).indexOf('default')) {
114+
schema.default = `${schema.default}`
115+
}
116+
}
117+
}
118+
92119
convertNull()
93120
convertTypeArrays()
94121
bannedWordsRemoval()
122+
convertDefaultValues()
95123

96124
if (this.specialProperties.indexOf(parentKeyword) !== -1) {
97125
if (this.ofProperties.indexOf(parentKeyword) !== -1) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title": "JSON API Schema",
4+
"description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
5+
"type": "object",
6+
"required": [
7+
"errors"
8+
],
9+
"properties": {
10+
"errors": {
11+
"type": "object"
12+
},
13+
"Price": {
14+
"type": "number",
15+
"default": "100"
16+
},
17+
"VAT": {
18+
"type": "integer",
19+
"default": "20"
20+
},
21+
"hasPrice": {
22+
"type": "boolean",
23+
"default": "true"
24+
},
25+
"things": {
26+
"type": "array"
27+
}
28+
}
29+
}

test/src/Convertor.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const complexPropertyDefinitionSchema = require('../schemas/complex-propertyDefi
2121
const complexResolvedDefinitionSchema = require('../schemas/complex-resolvedDefinition')
2222
const complexNullTypeSchema = require('../schemas/complex-null')
2323
const complexTypeArraySchema = require('../schemas/complex-typeArray')
24+
const complexDefaultValuesSchema = require('../schemas/complex-defaultValues')
2425

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

@@ -41,6 +42,7 @@ describe('Convertor', () => {
4142
delete require.cache[require.resolve('../schemas/complex-resolvedDefinition')];
4243
delete require.cache[require.resolve('../schemas/complex-null')];
4344
delete require.cache[require.resolve('../schemas/complex-typeArray')];
45+
delete require.cache[require.resolve('../schemas/complex-defaultValues')];
4446
convertor = new Convertor(simpleSchema)
4547
});
4648

@@ -416,6 +418,29 @@ describe('Convertor', () => {
416418
});
417419
});
418420

421+
describe('convert a schema with default values that are incorrectly defined', () => {
422+
it('should return a schema valid for OpenAPI v3.0.0', async function() {
423+
const complexConvertor = new Convertor(complexDefaultValuesSchema)
424+
const components = complexConvertor.convert()
425+
426+
const cloned = JSON.parse(JSON.stringify(simpleOpenAPI))
427+
let valid = await validator.validateInner(cloned, {})
428+
expect(valid).to.be.true
429+
Object.assign(cloned, {components})
430+
expect(cloned).to.have.property('components')
431+
expect(cloned.components).to.have.property('schemas')
432+
expect(cloned.components.schemas).to.have.property('main')
433+
expect(cloned.components.schemas.main).to.not.have.property('definitions')
434+
expect(cloned.components.schemas.main.properties.Price.default).to.be.equal(100)
435+
expect(cloned.components.schemas.main.properties.hasPrice.default).to.be.true
436+
valid = await validator.validateInner(cloned, {})
437+
.catch(err => {
438+
console.log(err)
439+
})
440+
expect(valid).to.be.true
441+
});
442+
});
443+
419444
describe('use a repo with lots of schemas to find failing ones', () => {
420445
xit('should convert all schemas successfully', async function() {
421446
this.timeout(5000);

0 commit comments

Comments
 (0)