Skip to content

Commit 37a23a9

Browse files
committed
fix missing $id in root schema
1 parent 831da65 commit 37a23a9

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

ref-resolver.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function jsonSchemaResolver (options) {
6464
// represent a valid URI-reference [RFC3986]. This value SHOULD be
6565
// normalized, and SHOULD NOT be an empty fragment <#> or an empty
6666
// string <>.
67-
const appUri = URI.parse(rootSchema.$id)
67+
const appUri = URI.parse(rootSchema.$id || 'application.uri')
6868
appUri.fragment = undefined // remove fragment
6969
debug('Found app URI %o', appUri)
7070

@@ -74,7 +74,9 @@ function jsonSchemaResolver (options) {
7474
}
7575

7676
const baseUri = URI.serialize(appUri) // canonical absolute-URI
77-
rootSchema.$id = baseUri // fix the schema $id value
77+
if (rootSchema.$id) {
78+
rootSchema.$id = baseUri // fix the schema $id value
79+
}
7880
rootSchema[kIgnore] = true
7981

8082
mapIds(ee, appUri, rootSchema)

test/ref-resolver.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,24 @@ test('dont resolve external schema missing #2', t => {
165165
const out = resolver.resolve(schema)
166166
t.deepEquals(schema, out, 'the output is the same input not modified')
167167
})
168+
169+
test('missing id in root schema', t => {
170+
t.plan(3)
171+
const schema = {
172+
$ref: 'relativePerson'
173+
}
174+
175+
const opts = {
176+
externalSchemas: [
177+
factory('relativeId-externalAndLocalRef'),
178+
factory('relativeId-noRef')
179+
]
180+
}
181+
182+
const resolver = RefResolver()
183+
const out = resolver.resolve(schema, opts)
184+
save(out)
185+
t.deepEquals(schema, out, 'the output is the same input modified')
186+
t.ok(out.definitions, 'definitions has been added')
187+
t.deepEquals(Object.values(out.definitions), opts.externalSchemas, 'external schema has been added to definitions')
188+
})

0 commit comments

Comments
 (0)