Skip to content

Commit

Permalink
fix $ref fragment preserve
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm committed May 21, 2020
1 parent b86b7e6 commit 792c516
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ref-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function jsonSchemaResolver (options) {
debug('Processed root schema')

debug('Generating %d refs', allRefs.length)
allRefs.forEach(({ baseUri, ref, json }) => {
allRefs.forEach(({ baseUri, ref, refUri, json }) => {
debug('Evaluating $ref %s', ref)
if (ref[0] === '#') { return }

Expand All @@ -110,7 +110,7 @@ function jsonSchemaResolver (options) {
return
}
evaluatedJson[kConsumed] = true
json.$ref = `#/definitions/${evaluatedJson[kRefToDef]}`
json.$ref = `#/definitions/${evaluatedJson[kRefToDef]}${refUri.fragment || ''}`
})

if (externalSchemas) {
Expand Down Expand Up @@ -166,6 +166,7 @@ function jsonSchemaResolver (options) {
const ref = URI.serialize(refUri)
allRefs.push({
baseUri: URI.serialize(baseUri),
refUri,
ref,
json
})
Expand Down
53 changes: 53 additions & 0 deletions test/ref-fragment.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict'

const { test } = require('tap')

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

// eslint-disable-next-line
const save = (out) => require('fs').writeFileSync(`./out-${Date.now()}.json`, JSON.stringify(out, null, 2))

test('Preserve $ref fragment', t => {
t.plan(1)
const opts = {
externalSchemas: [
{
$id: 'example',
type: 'object',
properties: {
hello: { type: 'string' }
}
}
]
}
const resolver = RefResolver()

const out = resolver.resolve({
$id: 'my-schema',
type: 'object',
properties: {
world: { $ref: 'example#/properties/hello' }
}
}, opts)

t.deepEqual(out, {
$id: 'my-schema',
type: 'object',
properties: {
world: {
$ref: '#/definitions/def-0/properties/hello'
}
},
definitions: {
'def-0': {
$id: 'example',
type: 'object',
properties: {
hello: {
type: 'string'
}
}
}
}
}, 'the $ref fragment has been preserved')
})

0 comments on commit 792c516

Please sign in to comment.