(
schema: T,
sharedDefinitionNames: Iterable<string>,
externalSchemaFile: string,
preserveDefinitions = false
)
| 1399 | } |
| 1400 | |
| 1401 | export function rewriteSharedDefinitionReferences<T>( |
| 1402 | schema: T, |
| 1403 | sharedDefinitionNames: Iterable<string>, |
| 1404 | externalSchemaFile: string, |
| 1405 | preserveDefinitions = false |
| 1406 | ): T { |
| 1407 | const sharedNames = new Set(sharedDefinitionNames); |
| 1408 | if (sharedNames.size === 0) return cloneSchemaForCodegen(schema); |
| 1409 | |
| 1410 | const rewriteRef = (ref: string): string => { |
| 1411 | const localRef = parseLocalDefinitionRef(ref); |
| 1412 | return localRef && sharedNames.has(localRef) ? `${externalSchemaFile}#/definitions/${localRef}` : ref; |
| 1413 | }; |
| 1414 | |
| 1415 | const rewrite = (value: unknown): unknown => { |
| 1416 | if (Array.isArray(value)) { |
| 1417 | return value.map((item) => rewrite(item)); |
| 1418 | } |
| 1419 | |
| 1420 | if (!value || typeof value !== "object") { |
| 1421 | return value; |
| 1422 | } |
| 1423 | |
| 1424 | const source = value as Record<string, unknown>; |
| 1425 | const result: Record<string, unknown> = {}; |
| 1426 | for (const [childKey, childValue] of Object.entries(source)) { |
| 1427 | if ((childKey === "definitions" || childKey === "$defs") && childValue && typeof childValue === "object" && !Array.isArray(childValue)) { |
| 1428 | const definitions: Record<string, unknown> = {}; |
| 1429 | for (const [definitionName, definitionValue] of Object.entries(childValue as Record<string, unknown>)) { |
| 1430 | if (preserveDefinitions || !sharedNames.has(definitionName)) { |
| 1431 | definitions[definitionName] = rewrite(definitionValue); |
| 1432 | } |
| 1433 | } |
| 1434 | result[childKey] = definitions; |
| 1435 | continue; |
| 1436 | } |
| 1437 | |
| 1438 | result[childKey] = rewrite(childValue); |
| 1439 | } |
| 1440 | |
| 1441 | if (typeof result.$ref === "string") { |
| 1442 | result.$ref = rewriteRef(result.$ref); |
| 1443 | } |
| 1444 | |
| 1445 | return result; |
| 1446 | }; |
| 1447 | |
| 1448 | return rewrite(schema) as T; |
| 1449 | } |
| 1450 | |
| 1451 | export function inlineExternalSchemaDefinitions<T>( |
| 1452 | schema: T, |
no test coverage detected
searching dependent graphs…