Skip to content

Commit 9c51c10

Browse files
authored
pass full schema to buildObject if $ref is defined at the top level (#209)
1 parent 82bfe28 commit 9c51c10

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function build (schema, options) {
7575
`
7676
}
7777

78+
const fullSchema = schema
7879
if (schema.$ref) {
7980
schema = refFinder(schema.$ref, schema, options.schema)
8081
}
@@ -90,7 +91,7 @@ function build (schema, options) {
9091
switch (schema.type) {
9192
case 'object':
9293
main = '$main'
93-
code = buildObject(schema, code, main, options.schema, schema)
94+
code = buildObject(schema, code, main, options.schema, fullSchema)
9495
break
9596
case 'string':
9697
main = schema.nullable ? $asStringNullable.name : $asString.name

test/ref.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,41 @@ test('ref internal - multiple $ref format', (t) => {
685685
t.equal(output, '{"zero":"test","a":"test","b":"test","c":"test","d":"test","e":"test"}')
686686
})
687687

688+
test('ref in root internal', (t) => {
689+
t.plan(2)
690+
691+
const schema = {
692+
title: 'object with $ref in root schema',
693+
$ref: '#/definitions/num',
694+
definitions: {
695+
num: {
696+
type: 'object',
697+
properties: {
698+
int: {
699+
$ref: '#/definitions/int'
700+
}
701+
}
702+
},
703+
int: {
704+
type: 'integer'
705+
}
706+
}
707+
}
708+
709+
const object = { int: 42 }
710+
const stringify = build(schema)
711+
const output = stringify(object)
712+
713+
try {
714+
JSON.parse(output)
715+
t.pass()
716+
} catch (e) {
717+
t.fail()
718+
}
719+
720+
t.equal(output, '{"int":42}')
721+
})
722+
688723
test('ref in root external', (t) => {
689724
t.plan(2)
690725

0 commit comments

Comments
 (0)