Skip to content

Commit 7fcb7b4

Browse files
authored
Fix: Add error handling and cyclic reference detection in update-3rd-party.ts
- Added import for `fetch` from 'node-fetch'. - Implemented error handling for `fetch` requests. - Added cyclic reference detection to prevent infinite loops. - Improved type checking for response body.
1 parent 76264f7 commit 7fcb7b4

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

scripts/update-3rd-party.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,33 @@ const cleanSchemaObject = (obj: any) => {
3131
* Flatten remotely referenced schemas into a single, combined schema.
3232
* Handles merging `properties` and `definitions` from a root `allOf`.
3333
*/
34-
const inlineRemoteRefs = async (json: any): Promise<void> => {
35-
for (const entry of json.allOf) {
36-
if (entry.$ref && entry.$ref.startsWith('https://')) {
37-
const res = await fetch(entry.$ref);
38-
39-
if (res.body && (res.body as any).message) {
40-
throw new Error((res.body as any).message);
34+
const inlineRemoteRefs = async (json: any, seenRefs = new Set<string>()) => {
35+
if (json.allOf) {
36+
for (const entry of json.allOf) {
37+
if (entry.$ref && entry.$ref.startsWith('https://')) {
38+
if (seenRefs.has(entry.$ref)) {
39+
throw new Error(`Cyclic reference detected: ${entry.$ref}`);
40+
}
41+
seenRefs.add(entry.$ref);
42+
43+
try {
44+
const res = await fetch(entry.$ref);
45+
if (!res.ok) {
46+
throw new Error(`Failed to fetch ${entry.$ref}: ${res.statusText}`);
47+
}
48+
49+
const refJson = await res.json();
50+
51+
json.properties = { ...json.properties, ...refJson.properties };
52+
json.definitions = { ...json.definitions, ...refJson.definitions };
53+
} catch (err) {
54+
throw new Error(`Error fetching or parsing ${entry.$ref}: ${err.message}`);
55+
}
4156
}
42-
43-
const refJson = await res.json();
44-
45-
json.properties = { ...json.properties, ...refJson.properties };
46-
json.definitions = { ...json.definitions, ...refJson.definitions };
4757
}
48-
}
4958

50-
delete json.allOf;
59+
delete json.allOf;
60+
}
5161
};
5262

5363
/**

0 commit comments

Comments
 (0)