MCPcopy Create free account
hub / github.com/EvilFreelancer/openapi-to-cli / resolveRefs

Method resolveRefs

src/openapi-loader.ts:89–133  ·  view source on GitHub ↗
(
    value: unknown,
    context: {
      currentSource: string;
      currentDocument: unknown;
      rawDocCache: Map<string, unknown>;
      resolvingRefs: Set<string>;
    }
  )

Source from the content-addressed store, hash-verified

87 }
88
89 private async resolveRefs(
90 value: unknown,
91 context: {
92 currentSource: string;
93 currentDocument: unknown;
94 rawDocCache: Map<string, unknown>;
95 resolvingRefs: Set<string>;
96 }
97 ): Promise<unknown> {
98 if (Array.isArray(value)) {
99 const items = await Promise.all(value.map((item) => this.resolveRefs(item, context)));
100 return items;
101 }
102
103 if (!value || typeof value !== "object") {
104 return value;
105 }
106
107 const record = value as Record<string, unknown>;
108 const ref = record.$ref;
109
110 if (typeof ref === "string") {
111 const siblingEntries = Object.entries(record).filter(([key]) => key !== "$ref");
112 const resolvedRef = await this.resolveRef(ref, context);
113 const resolvedSiblings = Object.fromEntries(
114 await Promise.all(
115 siblingEntries.map(async ([key, siblingValue]) => [key, await this.resolveRefs(siblingValue, context)] as const)
116 )
117 );
118
119 if (resolvedRef && typeof resolvedRef === "object" && !Array.isArray(resolvedRef)) {
120 return {
121 ...(resolvedRef as Record<string, unknown>),
122 ...resolvedSiblings,
123 };
124 }
125
126 return Object.keys(resolvedSiblings).length > 0 ? resolvedSiblings : resolvedRef;
127 }
128
129 const resolvedEntries = await Promise.all(
130 Object.entries(record).map(async ([key, nested]) => [key, await this.resolveRefs(nested, context)] as const)
131 );
132 return Object.fromEntries(resolvedEntries);
133 }
134
135 private async resolveRef(
136 ref: string,

Callers 2

loadAndResolveSpecMethod · 0.95
resolveRefMethod · 0.95

Calls 1

resolveRefMethod · 0.95

Tested by

no test coverage detected