private async resolveRef()

in lib/swagger/jsonLoader.ts [225:329]


  private async resolveRef(
    object: Json,
    pathArr: string[],
    rootObject: Json,
    relativeFilePath: string,
    skipResolveChildRef: boolean,
    keepRefSiblings?: boolean
  ): Promise<Json> {
    if (isRefLike(object)) {
      const refObjResult: any = {};
      if (object.readOnly !== undefined) {
        refObjResult[xmsReadonlyRef] = true;
      }
      const ref = object.$ref;
      const sp = ref.split("#");
      if (sp.length > 2) {
        throw new Error("ref format error multiple #");
      }

      const [refFilePath, refObjPath] = sp;
      if (refFilePath === "") {
        // Local reference
        if (!jsonPointer.has(rootObject as {}, refObjPath)) {
          throw new JsonLoaderRefError(object);
        }
        const mockName = (rootObject as any)[$id];
        if (keepRefSiblings) {
          object.$ref = `${mockName}#${refObjPath}`;
          return object;
        }
        refObjResult.$ref = `${mockName}#${refObjPath}`;
        return refObjResult;
      }
      const refObj = await this.load(
        pathJoin(pathDirname(relativeFilePath), refFilePath),
        skipResolveChildRef
      );
      const refMockName = (refObj as any)[$id];
      if (refObjPath !== undefined) {
        if (!jsonPointer.has(refObj as {}, refObjPath)) {
          throw new JsonLoaderRefError(object);
        }
        if (keepRefSiblings) {
          object.$ref = `${refMockName}#${refObjPath}`;
          return object;
        }
        refObjResult.$ref = `${refMockName}#${refObjPath}`;
        return refObjResult;
      } else {
        if (keepRefSiblings) {
          object.$ref = refMockName;
          return object;
        }
        refObjResult.$ref = refMockName;
        return refObjResult;
      }
    }

    if (Array.isArray(object)) {
      for (let idx = 0; idx < object.length; ++idx) {
        const item = object[idx];
        if (typeof item === "object" && item !== null) {
          const newRef = await this.resolveRef(
            item,
            pathArr.concat([idx.toString()]),
            rootObject,
            relativeFilePath,
            skipResolveChildRef
          );
          if (newRef !== item) {
            // eslint-disable-next-line require-atomic-updates
            (object as any)[idx] = newRef;
          }
        }
      }
    } else if (typeof object === "object" && object !== null) {
      const obj = object as any;
      if (this.opts.eraseDescription && typeof obj.description === "string") {
        delete obj.description;
      }
      if (this.opts.eraseXmsExamples && obj[xmsExamples] !== undefined) {
        delete obj[xmsExamples];
      }

      for (const key of Object.keys(obj)) {
        const item = obj[key];
        if (typeof item === "object" && item !== null) {
          const newRef = await this.resolveRef(
            item,
            pathArr.concat([key]),
            rootObject,
            relativeFilePath,
            skipResolveChildRef || this.skipResolveRefKeys.has(key)
          );
          if (newRef !== item) {
            obj[key] = newRef;
          }
        }
      }
    } else {
      throw new Error("Invalid json");
    }

    return object;
  }