Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding dereference of refs in anyOfs #207

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,12 +904,26 @@ function buildArrayTypeCondition (type, accessor) {
return condition
}

function dereferenceAnyOfRefs (schema, externalSchema, fullSchema) {
schema.anyOf.forEach((s, index) => {
// follow the refs
while (s.$ref) {
schema.anyOf[index] = refFinder(s.$ref, fullSchema, externalSchema)
s = schema.anyOf[index]
}
})
}

function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKey) {
var code = ''
var funcName

subKey = subKey || ''

if (schema.$ref) {
schema = refFinder(schema.$ref, fullSchema, externalSchema)
}

if (schema.type === undefined) {
var inferedType = inferTypeByKeyword(schema)
if (inferedType) {
Expand Down Expand Up @@ -955,6 +969,7 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
break
case undefined:
if ('anyOf' in schema) {
dereferenceAnyOfRefs(schema, externalSchema, fullSchema)
schema.anyOf.forEach((s, index) => {
var nestedResult = nested(laterCode, name, key, s, externalSchema, fullSchema, subKey !== '' ? subKey : 'i' + index)
code += `
Expand Down
148 changes: 148 additions & 0 deletions test/anyof.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,151 @@ test('null value in schema', (t) => {
t.fail()
}
})

test('anyOf and $ref together', (t) => {
t.plan(2)

const schema = {
type: 'object',
properties: {
cs: {
anyOf: [
{
$ref: '#/definitions/Option'
},
{
type: 'boolean'
}
]
}
},
definitions: {
Option: {
type: 'string'
}
}
}

const stringify = build(schema)

try {
const value = stringify({
cs: 'franco'
})
t.is(value, '{"cs":"franco"}')
} catch (e) {
t.fail()
}

try {
const value = stringify({
cs: true
})
t.is(value, '{"cs":true}')
} catch (e) {
t.fail()
}
})

test('anyOf and $ref: 2 levels are fine', (t) => {
t.plan(1)

const schema = {
type: 'object',
properties: {
cs: {
anyOf: [
{
$ref: '#/definitions/Option'
},
{
type: 'boolean'
}
]
}
},
definitions: {
Option: {
anyOf: [
{
type: 'number'
},
{
type: 'boolean'
}
]
}
}
}

const stringify = build(schema)
try {
const value = stringify({
cs: 3
})
t.is(value, '{"cs":3}')
} catch (e) {
t.fail()
}
})

test('anyOf and $ref: multiple levels should throw at build.', (t) => {
t.plan(3)

const schema = {
type: 'object',
properties: {
cs: {
anyOf: [
{
$ref: '#/definitions/Option'
},
{
type: 'boolean'
}
]
}
},
definitions: {
Option: {
anyOf: [
{
$ref: '#/definitions/Option2'
},
{
type: 'string'
}
]
},
Option2: {
type: 'number'
}
}
}

const stringify = build(schema)
try {
const value = stringify({
cs: 3
})
t.is(value, '{"cs":3}')
} catch (e) {
t.fail(e)
}
try {
const value = stringify({
cs: true
})
t.is(value, '{"cs":true}')
} catch (e) {
t.fail(e)
}
try {
const value = stringify({
cs: 'pippo'
})
t.is(value, '{"cs":"pippo"}')
} catch (e) {
t.fail(e)
}
})