Skip to content

Commit 792c516

Browse files
committed
fix $ref fragment preserve
1 parent b86b7e6 commit 792c516

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

ref-resolver.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function jsonSchemaResolver (options) {
100100
debug('Processed root schema')
101101

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

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

116116
if (externalSchemas) {
@@ -166,6 +166,7 @@ function jsonSchemaResolver (options) {
166166
const ref = URI.serialize(refUri)
167167
allRefs.push({
168168
baseUri: URI.serialize(baseUri),
169+
refUri,
169170
ref,
170171
json
171172
})

test/ref-fragment.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
5+
const RefResolver = require('../ref-resolver')
6+
7+
// eslint-disable-next-line
8+
const save = (out) => require('fs').writeFileSync(`./out-${Date.now()}.json`, JSON.stringify(out, null, 2))
9+
10+
test('Preserve $ref fragment', t => {
11+
t.plan(1)
12+
const opts = {
13+
externalSchemas: [
14+
{
15+
$id: 'example',
16+
type: 'object',
17+
properties: {
18+
hello: { type: 'string' }
19+
}
20+
}
21+
]
22+
}
23+
const resolver = RefResolver()
24+
25+
const out = resolver.resolve({
26+
$id: 'my-schema',
27+
type: 'object',
28+
properties: {
29+
world: { $ref: 'example#/properties/hello' }
30+
}
31+
}, opts)
32+
33+
t.deepEqual(out, {
34+
$id: 'my-schema',
35+
type: 'object',
36+
properties: {
37+
world: {
38+
$ref: '#/definitions/def-0/properties/hello'
39+
}
40+
},
41+
definitions: {
42+
'def-0': {
43+
$id: 'example',
44+
type: 'object',
45+
properties: {
46+
hello: {
47+
type: 'string'
48+
}
49+
}
50+
}
51+
}
52+
}, 'the $ref fragment has been preserved')
53+
})

0 commit comments

Comments
 (0)