Skip to content

Commit 111f8c8

Browse files
migolovanovmcollina
authored andcommitted
Fix relative ref resolving issue (#196)
* fix ref resolve * add unit test
1 parent a6652fd commit 111f8c8

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ function idFinder (schema, searchedId) {
479479
}
480480

481481
function refFinder (ref, schema, externalSchema) {
482+
if (externalSchema && externalSchema[ref]) return externalSchema[ref]
483+
482484
// Split file from walk
483485
ref = ref.split('#')
484486

test/ref.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,3 +846,33 @@ test('ref to nested ref definition', (t) => {
846846

847847
t.equal(output, '{"foo":"foo"}')
848848
})
849+
850+
test('ref in definition with exact match', (t) => {
851+
t.plan(2)
852+
853+
const externalSchema = {
854+
'#/definitions/foo': {
855+
type: 'string'
856+
}
857+
}
858+
859+
const schema = {
860+
type: 'object',
861+
properties: {
862+
foo: { $ref: '#/definitions/foo' }
863+
}
864+
}
865+
866+
const object = { foo: 'foo' }
867+
const stringify = build(schema, { schema: externalSchema })
868+
const output = stringify(object)
869+
870+
try {
871+
JSON.parse(output)
872+
t.pass()
873+
} catch (e) {
874+
t.fail()
875+
}
876+
877+
t.equal(output, '{"foo":"foo"}')
878+
})

0 commit comments

Comments
 (0)