Skip to content

Commit d27d402

Browse files
committed
test for multiple items in an array
1 parent 43122ac commit d27d402

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/Convertor.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,7 @@ class Convertor {
244244
convertArrays(schema) {
245245
if (Array.isArray(schema.items)) {
246246
const obj = {}
247-
for (const item of schema.items) {
248-
Object.assign(obj, item)
249-
}
247+
Object.assign(obj, schema.items[0])
250248
schema.items = obj
251249
}
252250
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
"names": {
8+
"type": "array",
9+
"items": [{
10+
"type": "object",
11+
"properties": {
12+
"blah": {
13+
"type": "string"
14+
}
15+
}
16+
},
17+
{
18+
"type": "number"
19+
}]
20+
}
21+
}
22+
}

test/src/Convertor.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const defaultNumbers = require('../schemas/defaultValues/defaultNumbers.json')
2828
const defaultString = require('../schemas/defaultValues/defaultString.json')
2929
// array items
3030
const basicArray = require('../schemas/arrayItems/basicArray.json')
31+
const multiArray = require('../schemas/arrayItems/multipleItemArray.json')
3132
// const values
3233
const basicConst = require('../schemas/const/basicConst.json')
3334
// if/then/else schemas
@@ -331,6 +332,24 @@ describe('Convertor', () => {
331332
let valid = await validator.validateInner(cloned, {})
332333
expect(valid).to.be.true
333334
});
335+
336+
it('should only use the first item in the array and discard the others', async function() {
337+
const newConvertor = new Convertor(multiArray)
338+
const result = newConvertor.convert('basic')
339+
expect(result.schemas.basic.properties.names).to.have.property('type')
340+
expect(result.schemas.basic.properties.names.type).to.be.equal('array')
341+
expect(result.schemas.basic.properties.names.items).to.be.an('object')
342+
expect(result.schemas.basic.properties.names.items).to.not.be.an('array')
343+
expect(result.schemas.basic.properties.names.items.type).to.equal('object')
344+
345+
const cloned = JSON.parse(JSON.stringify(basicOpenAPI))
346+
Object.assign(cloned, {components: result})
347+
expect(cloned).to.have.property('components')
348+
expect(cloned.components).to.have.property('schemas')
349+
expect(cloned.components.schemas).to.have.property('basic')
350+
let valid = await validator.validateInner(cloned, {})
351+
expect(valid).to.be.true
352+
});
334353
});
335354

336355
describe('const values', () => {

0 commit comments

Comments
 (0)