diff --git a/index.js b/index.js index a5db8099..a7c5f766 100644 --- a/index.js +++ b/index.js @@ -75,6 +75,7 @@ function build (schema, options) { ` } + const fullSchema = schema if (schema.$ref) { schema = refFinder(schema.$ref, schema, options.schema) } @@ -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 diff --git a/test/ref.test.js b/test/ref.test.js index f3e97b73..aa211dd2 100644 --- a/test/ref.test.js +++ b/test/ref.test.js @@ -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)