Skip to content

Commit d0a325a

Browse files
authored
fix $ref to absolute uri (#2)
1 parent db07339 commit d0a325a

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ const anotherResolvedSchema = ref.resolve(input_2_Schema)
121121
const sharedDefinitions = ref.definitions()
122122
```
123123

124+
## Debug
125+
126+
To debug this module, simply set:
127+
128+
```bash
129+
export DEBUG=json-schema-resolver
130+
```
131+
124132
## License
125133

126134
Licensed under [MIT](./LICENSE).

ref-resolver.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function jsonSchemaResolver (options) {
106106

107107
const evaluatedJson = allIds.get(baseUri)
108108
if (!evaluatedJson) {
109-
debug('External $ref %s not provided', ref)
109+
debug('External $ref %s not provided with baseUri %s', ref, baseUri)
110110
return
111111
}
112112
evaluatedJson[kConsumed] = true
@@ -161,6 +161,8 @@ function jsonSchemaResolver (options) {
161161
const newBaseUri = Object.assign({}, baseUri)
162162
newBaseUri.path = refUri.path
163163
baseUri = newBaseUri
164+
} else if (refUri.reference === 'uri' || refUri.reference === 'absolute') {
165+
baseUri = { ...refUri, fragment: undefined }
164166
}
165167

166168
const ref = URI.serialize(refUri)

test/ref-resolver.test.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,65 @@ test('missing id in root schema', t => {
182182

183183
const resolver = RefResolver()
184184
const out = resolver.resolve(schema, opts)
185-
save(out)
186185
t.deepEquals(schema, out, 'the output is the same input modified')
187186
t.ok(out.definitions, 'definitions has been added')
188187
t.deepEquals(Object.values(out.definitions), opts.externalSchemas, 'external schema has been added to definitions')
189188
})
189+
190+
test('absolute $ref', t => {
191+
t.plan(2)
192+
const schema = { $ref: 'http://example.com/#/definitions/idParam' }
193+
194+
const absSchemaId = {
195+
$id: 'http://example.com/',
196+
definitions: {
197+
uuid: {
198+
type: 'string',
199+
pattern: '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'
200+
},
201+
idParam: {
202+
type: 'object',
203+
required: ['id'],
204+
properties: {
205+
id: { $ref: '#/definitions/uuid' }
206+
},
207+
additionalProperties: false
208+
}
209+
}
210+
}
211+
212+
const externalSchemas = [absSchemaId]
213+
214+
const resolver = RefResolver({ clone: true, applicationUri: 'todo.com', externalSchemas })
215+
const out = resolver.resolve(schema)
216+
217+
t.notEqual(out.$ref, 'http://example.com/#/definitions/idParam')
218+
t.deepEquals(resolver.definitions(), {
219+
definitions: {
220+
'def-0': absSchemaId
221+
}
222+
})
223+
})
224+
225+
test('absolute $ref #2', t => {
226+
t.plan(2)
227+
const schema = factory('absoluteId-absoluteRef')
228+
229+
const absSchemaId = {
230+
$id: 'http://other-site.com/relativeAddress',
231+
type: 'object',
232+
properties: {
233+
uuid: {
234+
type: 'string',
235+
pattern: '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'
236+
}
237+
}
238+
}
239+
240+
const externalSchemas = [absSchemaId]
241+
242+
const resolver = RefResolver({ clone: true })
243+
const out = resolver.resolve(schema, { externalSchemas })
244+
t.equal(out.properties.address.$ref, '#/definitions/def-0')
245+
t.equal(out.properties.houses.items.$ref, '#/definitions/def-0')
246+
})

0 commit comments

Comments
 (0)