Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions test/ref-local.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
'use strict'

const { test } = require('tap')

const RefResolver = require('../ref-resolver')

test('Resolve absolute refs in schema', t => {
t.plan(1)
const opts = {
externalSchemas: [
{
$id: 'ObjectA',
type: 'object',
properties: {
example: {
type: 'string'
}
}
},
{
$id: 'ObjectC',
type: 'object',
properties: {
referencedObjA: {
$ref: 'ObjectA#'
},
referencedObjC: {
$ref: 'ObjectC#/properties/ObjectD'
},
ObjectD: {
type: 'object',
properties: {
d: {
type: 'string'
}
}
}
}
}
]
}
const resolver = RefResolver()

const out = resolver.resolve({
$ref: 'ObjectC#'
}, opts)

t.same(out, {
$ref: '#/definitions/def-1',
definitions: {
'def-0': {
$id: 'ObjectA',
type: 'object',
properties: {
example: {
type: 'string'
}
}
},
'def-1': {
$id: 'ObjectC',
type: 'object',
properties: {
referencedObjA: {
$ref: '#/definitions/def-0'
},
referencedObjC: {
$ref: '#/definitions/def-1/properties/ObjectD'
},
ObjectD: {
type: 'object',
properties: {
d: {
type: 'string'
}
}
}
}
}
}
})
})

test('Resolve relative refs in schema', t => {
t.plan(1)
const opts = {
externalSchemas: [
{
$id: 'ObjectA',
type: 'object',
properties: {
sample: {
type: 'object',
properties: {
a: { type: 'string' },
b: { type: 'object', properties: { d: { type: 'string' } } }
}
},
someValue: { type: 'string' },
relativeExample: {
$ref: '#/properties/sample'
}
}
}
]
}
const resolver = RefResolver()

const out = resolver.resolve({
$ref: 'ObjectA#/properties/relativeExample'
}, opts)

t.same(out, {
$ref: '#/definitions/def-0/properties/relativeExample',
definitions: {
'def-0': {
$id: 'ObjectA',
type: 'object',
properties: {
sample: {
type: 'object',
properties: {
a: {
type: 'string'
},
b: {
type: 'object',
properties: {
d: {
type: 'string'
}
}
}
}
},
someValue: {
type: 'string'
},
relativeExample: {
$ref: '#/properties/sample'
}
}
}
}
})
})