Skip to content

Commit e4d927a

Browse files
committed
fix resolveRef
Signed-off-by: xil <xil@uber.com> Signed-off-by: xil <fridalu66@gmail.com>
1 parent 68c1884 commit e4d927a

1 file changed

Lines changed: 18 additions & 42 deletions

File tree

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

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,13 @@ export function resolveObj(
5757
return obj as OpenAPIV3.SchemaObject;
5858
}
5959

60-
export function resolveRef(
61-
ref: string,
62-
root: OpenAPIV3.Document
63-
): OpenAPIV3.SchemaObject | undefined {
64-
if (!ref.startsWith("#/")) {
65-
return undefined;
66-
}
67-
68-
const pathParts = ref.replace(/^#\//, "").split("/");
69-
70-
let current: any = root;
71-
for (const part of pathParts) {
72-
if (current == null || typeof current !== "object") {
73-
return undefined;
74-
}
75-
current = current[part];
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
7665
}
77-
78-
if (!current) {
79-
return undefined;
80-
}
81-
if ("$ref" in current) {
82-
return resolveObj(current, root);
83-
}
84-
85-
return current as OpenAPIV3.SchemaObject;
66+
return root
8667
}
8768

8869
export function isPrimitiveType(schema: OpenAPIV3.SchemaObject): boolean {
@@ -137,38 +118,33 @@ export function deleteMatchingKeys(obj: any, condition: (item: any) => boolean):
137118
}
138119

139120
/**
140-
* Find all $ref references in the spec, including nested references
121+
* Find all $ref references in the spec, including nested references.
141122
*/
142-
export function find_refs(
143-
current: Record<string, any>,
144-
root?: Record<string, any>,
145-
call_stack: string[] = []
146-
): Set<string> {
147-
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>()
148125

149126
if (root === undefined) {
150-
root = current;
151-
current = current.paths;
127+
root = current
128+
current = current.paths
152129
}
153130

154131
if (current?.$ref != null) {
155-
const ref = current.$ref as string;
156-
results.add(ref);
157-
158-
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)
159135
if (ref_node !== undefined && !call_stack.includes(ref)) {
160-
call_stack.push(ref);
161-
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))
162138
}
163139
}
164140

165141
if (_.isObject(current)) {
166142
_.forEach(current, (v) => {
167143
find_refs(v as Record<string, any>, root, call_stack).forEach((ref) => results.add(ref));
168-
});
144+
})
169145
}
170146

171-
return results;
147+
return results
172148
}
173149

174150
/**

0 commit comments

Comments
 (0)