Skip to content

Commit ab47674

Browse files
fix: unmatched type for value in oneOf should be ignored (#344)
1 parent b8532dc commit ab47674

File tree

2 files changed

+112
-5
lines changed

2 files changed

+112
-5
lines changed

index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,9 +1023,11 @@ function buildArray (location, code, name, key = null) {
10231023

10241024
code += `
10251025
var l = obj.length
1026-
var w = l - 1
1026+
10271027
for (var i = 0; i < l; i++) {
1028-
if (i > 0) {
1028+
var jsonLastChar = json[json.length - 1]
1029+
1030+
if (i > 0 && jsonLastChar !== '[' && jsonLastChar !== ',') {
10291031
json += ','
10301032
}
10311033
${result.code}
@@ -1214,9 +1216,12 @@ function nested (laterCode, name, key, location, subKey, isArray) {
12141216
`
12151217
laterCode = nestedResult.laterCode
12161218
})
1217-
code += `
1218-
else json+= null
1219-
`
1219+
1220+
if (!isArray) {
1221+
code += `
1222+
else json+= null
1223+
`
1224+
}
12201225
} else if (isEmpty(schema)) {
12211226
code += `
12221227
json += JSON.stringify(obj${accessor})

test/oneof.test.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,105 @@ test('oneOf object with field of type string with format or null', (t) => {
401401
prop: toStringify
402402
}), `{"prop":"${toStringify.toISOString()}"}`)
403403
})
404+
405+
test('one array item match oneOf types', (t) => {
406+
t.plan(1)
407+
408+
const schema = {
409+
type: 'object',
410+
additionalProperties: false,
411+
required: ['data'],
412+
properties: {
413+
data: {
414+
type: 'array',
415+
minItems: 1,
416+
items: {
417+
oneOf: [
418+
{
419+
type: 'string'
420+
},
421+
{
422+
type: 'number'
423+
}
424+
]
425+
}
426+
}
427+
}
428+
}
429+
430+
const stringify = build(schema)
431+
432+
const responseWithMappedType = stringify({
433+
data: [false, 'foo']
434+
})
435+
436+
t.equal('{"data":["foo"]}', responseWithMappedType)
437+
})
438+
439+
test('some array items match oneOf types', (t) => {
440+
t.plan(1)
441+
442+
const schema = {
443+
type: 'object',
444+
additionalProperties: false,
445+
required: ['data'],
446+
properties: {
447+
data: {
448+
type: 'array',
449+
minItems: 1,
450+
items: {
451+
oneOf: [
452+
{
453+
type: 'string'
454+
},
455+
{
456+
type: 'number'
457+
}
458+
]
459+
}
460+
}
461+
}
462+
}
463+
464+
const stringify = build(schema)
465+
466+
const responseWithMappedTypes = stringify({
467+
data: [false, 'foo', true, 5]
468+
})
469+
470+
t.equal('{"data":["foo",5]}', responseWithMappedTypes)
471+
})
472+
473+
test('all array items does not match oneOf types', (t) => {
474+
t.plan(1)
475+
476+
const schema = {
477+
type: 'object',
478+
additionalProperties: false,
479+
required: ['data'],
480+
properties: {
481+
data: {
482+
type: 'array',
483+
minItems: 1,
484+
items: {
485+
oneOf: [
486+
{
487+
type: 'string'
488+
},
489+
{
490+
type: 'number'
491+
}
492+
]
493+
}
494+
}
495+
}
496+
}
497+
498+
const stringify = build(schema)
499+
500+
const emptyResponse = stringify({
501+
data: [null, false, true, undefined, [], {}]
502+
})
503+
504+
t.equal('{"data":[]}', emptyResponse)
505+
})

0 commit comments

Comments
 (0)