export function followReference()

in packages/azure-openapi-validator/core/src/utils.ts [13:41]


export function followReference(doc: any, schema: any, inventory?: ISwaggerInventory):any {
  const getRefModel = (docToSearch:any,refValue: string, visited: string[]) => {
    if (visited.includes(refValue)) {
      throw new Error("Found circle reference: " + visited.join("->"))
    }
    visited.push(refValue)
    const refSlices = parseJsonRef(refValue)
    const pathExpression = refSlices[1].split("/").slice(1)
    try {
      const result = nodes(docToSearch, stringify(pathExpression))
      return result.length !== 0 ? result[0].value : undefined
    } catch (err) {
      return undefined
    }
  }

  if (schema && doc) {
    if (schema.$ref) {
      const refSlices = parseJsonRef(schema.$ref)
      if (inventory && refSlices[0]) {
        doc = inventory.getDocuments(refSlices[0])
      }
      schema = getRefModel(doc,`#${refSlices[1]}`, [])
      return followReference(doc, schema, inventory)
    }
    return schema
  }
  return undefined
}