Skip to content

Commit

Permalink
fixup! schemaloader: Fix expandRefs not to crash if $ref'ed schema al…
Browse files Browse the repository at this point in the history
…so has another $ref to self internally

Testing via CodecepJS and reference.html revealed a bug in my second
patch: if the ref is of the form

    schema.json#/json/pointer1#/json/pointer2

we need to load schema.json, dereference pointer1 and then dereference
pointer2 on the result.

Original code was doing this second step, but my patch did not, and so
it was breaking as e.g.:

https://github.com/json-editor/json-editor/actions/runs/6074053135/job/16477372582#step:6:456
  • Loading branch information
navytux committed Sep 4, 2023
1 parent 3647b7b commit e4e61f0
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/schemaloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,31 +166,37 @@ export class SchemaLoader {
const refObj = (refWithPointerSplit.length > 2)
? this.refs_with_info['#' + refWithPointerSplit[1]]
: this.refs_with_info[_schema.$ref]
const refPointersTail = refWithPointerSplit.slice(2)
delete _schema.$ref
const fetchUrl = refObj.$ref.startsWith('#')
? refObj.fetchUrl
: ''
const ref = this._getRef(fetchUrl, refObj)

// lookup schema via .refs[refBase] and dereference via JSON-pointer from ref
refWithPointerSplit = ref.split('#')
let refBase = ref
let refPointer = null
let refPointers = []
if (refWithPointerSplit.length > 1) {
refBase = refWithPointerSplit[0]
refPointer = refWithPointerSplit[1]
refPointers = refWithPointerSplit.slice(1)
}

if (!this.refs[refBase]) { /* if reference not found */
// eslint-disable-next-line no-console
console.warn(`reference:'${ref}' not found!`)
}

let refSchema = this.refs[refBase]
if (refPointer !== null) {
refSchema = this.expandRecursivePointer(refSchema, refPointer)
while (refPointers.length > 0) {
refSchema = this.expandRecursivePointer(refSchema, refPointers.shift())
}

// further expand JSON-pointer from tail of original fragment [2, ...]
while (refPointersTail.length > 0) {
refSchema = this.expandRecursivePointer(refSchema, refPointersTail.shift())
}

if (recurseAllOf && hasOwnProperty(refSchema, 'allOf')) {
const allOf = this.refs[ref].allOf
const allOf = refSchema.allOf
Object.keys(allOf).forEach(key => {
allOf[key] = this.expandRefs(allOf[key], true)
})
Expand Down

0 comments on commit e4e61f0

Please sign in to comment.