Skip to content
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