Skip to content

Commit

Permalink
pass full schema to buildObject if $ref is defined at the top level (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukiano authored Feb 21, 2020
1 parent 82bfe28 commit 9c51c10
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function build (schema, options) {
`
}

const fullSchema = schema
if (schema.$ref) {
schema = refFinder(schema.$ref, schema, options.schema)
}
Expand All @@ -90,7 +91,7 @@ function build (schema, options) {
switch (schema.type) {
case 'object':
main = '$main'
code = buildObject(schema, code, main, options.schema, schema)
code = buildObject(schema, code, main, options.schema, fullSchema)
break
case 'string':
main = schema.nullable ? $asStringNullable.name : $asString.name
Expand Down
35 changes: 35 additions & 0 deletions test/ref.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,41 @@ test('ref internal - multiple $ref format', (t) => {
t.equal(output, '{"zero":"test","a":"test","b":"test","c":"test","d":"test","e":"test"}')
})

test('ref in root internal', (t) => {
t.plan(2)

const schema = {
title: 'object with $ref in root schema',
$ref: '#/definitions/num',
definitions: {
num: {
type: 'object',
properties: {
int: {
$ref: '#/definitions/int'
}
}
},
int: {
type: 'integer'
}
}
}

const object = { int: 42 }
const stringify = build(schema)
const output = stringify(object)

try {
JSON.parse(output)
t.pass()
} catch (e) {
t.fail()
}

t.equal(output, '{"int":42}')
})

test('ref in root external', (t) => {
t.plan(2)

Expand Down

0 comments on commit 9c51c10

Please sign in to comment.