diff --git a/index.js b/index.js index 22e1d341..2507f10a 100644 --- a/index.js +++ b/index.js @@ -438,7 +438,7 @@ function cloneOriginSchema (context, schema, schemaId) { for (const key in schema) { let value = schema[key] - if (key === '$ref' && value.charAt(0) === '#') { + if (key === '$ref' && typeof value === 'string' && value.charAt(0) === '#') { value = schemaId + value } diff --git a/test/allof.test.js b/test/allof.test.js index 4c5e3ba5..5c80873d 100644 --- a/test/allof.test.js +++ b/test/allof.test.js @@ -717,3 +717,39 @@ test('external recursive allOfs', (t) => { const stringify = build(schema, { schema: { externalSchema } }) t.equal(stringify(data), '{"a":{"bar":"42","foo":{}},"b":{"bar":"42","foo":{}}}') }) + +test('do not crash with $ref prop', (t) => { + t.plan(1) + + const schema = { + title: 'object with $ref', + type: 'object', + properties: { + outside: { + $ref: '#/$defs/outside' + } + }, + $defs: { + inside: { + type: 'object', + properties: { + $ref: { + type: 'string' + } + } + }, + outside: { + allOf: [{ + $ref: '#/$defs/inside' + }] + } + } + } + const stringify = build(schema) + const value = stringify({ + outside: { + $ref: 'true' + } + }) + t.equal(value, '{"outside":{"$ref":"true"}}') +})