Skip to content

Commit 731f0d1

Browse files
committed
deal weith fields that should be arrays
1 parent f0da00e commit 731f0d1

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/Convertor.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ class Convertor {
2727
'externalDocs',
2828
]
2929

30+
this.arrayFields = [
31+
'allOf',
32+
'oneOf',
33+
'anyOf',
34+
'enum',
35+
'required',
36+
]
37+
3038
this.specialSchemaFields = [
3139
'type',
3240
'allOf',
@@ -106,6 +114,7 @@ class Convertor {
106114
this.convertIfThenElse(schema)
107115
this.convertTypeArrays(schema)
108116
this.dealWithCamelCase(schema)
117+
this.ensureArrayFields(schema)
109118
this.convertDependencies(schema)
110119
this.removeEmptyRequired(schema)
111120
this.convertNullProperty(schema)
@@ -350,6 +359,20 @@ class Convertor {
350359
}
351360
}
352361

362+
ensureArrayFields(schema) {
363+
for (const key of Object.keys(schema)) {
364+
const arrayField = this.arrayFields.filter(field => {
365+
if (key.toLowerCase() === field.toLowerCase()) {
366+
return field
367+
}
368+
})
369+
370+
if (arrayField.length && Array.isArray(schema[key]) === false) {
371+
schema[arrayField] = [schema[key]]
372+
}
373+
}
374+
}
375+
353376
convertDependencies(schema) {
354377
if (schema.dependencies || schema.dependentSchemas || schema.dependentRequired) {
355378
const allOf = []
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
"properties": {
7+
"name": {
8+
"oneof": {
9+
"type": "string"
10+
}
11+
}
12+
}
13+
}

test/src/Convertor.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const dependentRequired = require('../schemas/dependencies/dependentRequired.jso
4646
const dependentSchemas = require('../schemas/dependencies/dependentSchemas.json')
4747
// camelcased keys
4848
const camelCased = require('../schemas/camelCasedKey/camelCasedKey.json')
49+
// array Keys
50+
const arrayKeyOneOf = require('../schemas/arrayKeys/arrayKeyOneOf.json')
4951
// External Schemas That I Cannot Currently Convert
5052
const listOfBannedSchemas = require('../schemas/SchemasThatCannotBeConverted/list.json')
5153

@@ -585,6 +587,23 @@ describe('Convertor', () => {
585587
});
586588
});
587589

590+
describe('array keys', () => {
591+
it('should make sure expected array keys are actually arrays', async function() {
592+
const newConvertor = new Convertor(arrayKeyOneOf)
593+
const result = newConvertor.convert('basic')
594+
expect(result.schemas.basic.properties.name).to.have.property('oneOf')
595+
expect(result.schemas.basic.properties.name.oneOf).to.be.an('array')
596+
597+
const cloned = JSON.parse(JSON.stringify(basicOpenAPI))
598+
Object.assign(cloned, {components: result})
599+
expect(cloned).to.have.property('components')
600+
expect(cloned.components).to.have.property('schemas')
601+
expect(cloned.components.schemas).to.have.property('basic')
602+
let valid = await validator.validateInner(cloned, {})
603+
expect(valid).to.be.true
604+
});
605+
});
606+
588607
xdescribe('use a repo with lots of schemas to find failing ones', () => {
589608
it('should convert all schemas successfully', async function() {
590609
this.timeout(1000000);

0 commit comments

Comments
 (0)