Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add error handling and cyclic reference detection in update-3rd-… #5986

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions scripts/update-3rd-party.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,33 @@ const cleanSchemaObject = (obj: any) => {
* Flatten remotely referenced schemas into a single, combined schema.
* Handles merging `properties` and `definitions` from a root `allOf`.
*/
const inlineRemoteRefs = async (json: any): Promise<void> => {
for (const entry of json.allOf) {
if (entry.$ref && entry.$ref.startsWith('https://')) {
const res = await fetch(entry.$ref);

if (res.body && (res.body as any).message) {
throw new Error((res.body as any).message);
const inlineRemoteRefs = async (json: any, seenRefs = new Set<string>()) => {
if (json.allOf) {
for (const entry of json.allOf) {
if (entry.$ref && entry.$ref.startsWith('https://')) {
if (seenRefs.has(entry.$ref)) {
throw new Error(`Cyclic reference detected: ${entry.$ref}`);
}
seenRefs.add(entry.$ref);

try {
const res = await fetch(entry.$ref);
if (!res.ok) {
throw new Error(`Failed to fetch ${entry.$ref}: ${res.statusText}`);
}

const refJson = await res.json();

json.properties = { ...json.properties, ...refJson.properties };
json.definitions = { ...json.definitions, ...refJson.definitions };
} catch (err) {
throw new Error(`Error fetching or parsing ${entry.$ref}: ${err.message}`);
}
}

const refJson = await res.json();

json.properties = { ...json.properties, ...refJson.properties };
json.definitions = { ...json.definitions, ...refJson.definitions };
}
}

delete json.allOf;
delete json.allOf;
}
};

/**
Expand Down
Loading