Skip to content

Commit 68f7b69

Browse files
fix: resolving ref for object property with default value (#535)
1 parent 4df76b7 commit 68f7b69

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ function buildCode (location) {
337337
const propertiesLocation = mergeLocation(location, 'properties')
338338
Object.keys(schema.properties || {}).forEach((key) => {
339339
let propertyLocation = mergeLocation(propertiesLocation, key)
340-
if (propertyLocation.$ref) {
341-
propertyLocation = resolveRef(location, propertyLocation.$ref)
340+
if (propertyLocation.schema.$ref) {
341+
propertyLocation = resolveRef(location, propertyLocation.schema.$ref)
342342
}
343343

344344
const sanitized = JSON.stringify(key)

test/ref.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,3 +1981,27 @@ test('nested schema should overwrite anchor scope', (t) => {
19811981
t.equal(output, JSON.stringify(data))
19821982
t.throws(() => build({ $ref: 'root#anchor' }, { schema: externalSchema }))
19831983
})
1984+
1985+
test('object property reference with default value', (t) => {
1986+
t.plan(1)
1987+
1988+
const schema = {
1989+
definitions: {
1990+
prop: {
1991+
type: 'string',
1992+
default: 'foo'
1993+
}
1994+
},
1995+
type: 'object',
1996+
properties: {
1997+
prop: {
1998+
$ref: '#/definitions/prop'
1999+
}
2000+
}
2001+
}
2002+
2003+
const stringify = build(schema)
2004+
const output = stringify({})
2005+
2006+
t.equal(output, '{"prop":"foo"}')
2007+
})

0 commit comments

Comments
 (0)