Skip to content

Commit

Permalink
Fix allOf with $ref property (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Apr 9, 2024
1 parent bb08e77 commit 4471a40
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
36 changes: 36 additions & 0 deletions test/allof.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}')
})

0 comments on commit 4471a40

Please sign in to comment.