Skip to content

Commit bb82ea0

Browse files
committed
fix
1 parent 94a8f75 commit bb82ea0

1 file changed

Lines changed: 20 additions & 43 deletions

File tree

tools/proto-convert/src/utils/helper.ts

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,35 +51,19 @@ export function resolveObj(
5151
): OpenAPIV3.SchemaObject | undefined {
5252
if (!obj) return undefined;
5353

54+
if ("$ref" in obj) {
55+
return resolveRef(obj.$ref, root);
56+
}
5457
return obj as OpenAPIV3.SchemaObject;
5558
}
5659

57-
export function resolveRef(
58-
ref: string,
59-
root: OpenAPIV3.Document
60-
): OpenAPIV3.SchemaObject | undefined {
61-
if (!ref.startsWith("#/")) {
62-
return undefined;
63-
}
64-
65-
const pathParts = ref.replace(/^#\//, "").split("/");
66-
67-
let current: any = root;
68-
for (const part of pathParts) {
69-
if (current == null || typeof current !== "object") {
70-
return undefined;
71-
}
72-
current = current[part];
73-
}
74-
75-
if (!current) {
76-
return undefined;
60+
export function resolveRef (ref: string, root: Record<string, any>): Record<string, any> | undefined {
61+
const paths = ref.replace('#/', '').split('/')
62+
for (const p of paths) {
63+
root = root[p]
64+
if (root === undefined) break
7765
}
78-
if ("$ref" in current) {
79-
return resolveObj(current, root);
80-
}
81-
82-
return current as OpenAPIV3.SchemaObject;
66+
return root
8367
}
8468

8569
export function isPrimitiveType(schema: OpenAPIV3.SchemaObject): boolean {
@@ -135,39 +119,32 @@ export function deleteMatchingKeys(obj: any, condition: (item: any) => boolean):
135119

136120
/**
137121
* Find all $ref references in the spec, including nested references.
138-
* Important: We must capture $refs without recursively resolving them,
139-
* so we can find transitive references (e.g., schema A -> schema B -> schema C)
140122
*/
141-
export function find_refs(
142-
current: Record<string, any>,
143-
root?: Record<string, any>,
144-
call_stack: string[] = []
145-
): Set<string> {
146-
const results = new Set<string>();
123+
export function find_refs (current: Record<string, any>, root?: Record<string, any>, call_stack: string[] = []): Set<string> {
124+
var results = new Set<string>()
147125

148126
if (root === undefined) {
149-
root = current;
150-
current = current.paths;
127+
root = current
128+
current = current.paths
151129
}
152130

153131
if (current?.$ref != null) {
154-
const ref = current.$ref as string;
155-
results.add(ref);
156-
157-
const ref_node = resolveRef(ref, root as OpenAPIV3.Document);
132+
const ref = current.$ref as string
133+
results.add(ref)
134+
const ref_node = resolveRef(ref, root as OpenAPIV3.Document)
158135
if (ref_node !== undefined && !call_stack.includes(ref)) {
159-
call_stack.push(ref);
160-
find_refs(ref_node as Record<string, any>, root, call_stack).forEach((ref) => results.add(ref));
136+
call_stack.push(ref)
137+
find_refs(ref_node, root, call_stack).forEach((ref) => results.add(ref))
161138
}
162139
}
163140

164141
if (_.isObject(current)) {
165142
_.forEach(current, (v) => {
166143
find_refs(v as Record<string, any>, root, call_stack).forEach((ref) => results.add(ref));
167-
});
144+
})
168145
}
169146

170-
return results;
147+
return results
171148
}
172149

173150
/**

0 commit comments

Comments
 (0)