diff --git a/src/contracts/compactJsonSchema.ts b/src/contracts/compactJsonSchema.ts index fbfef35d75a..78ee32caf6c 100644 --- a/src/contracts/compactJsonSchema.ts +++ b/src/contracts/compactJsonSchema.ts @@ -464,52 +464,68 @@ export const inlineWithinBudget = ( return serializedBytes(schema) <= budgetBytes ? schema : undefined; }; -/** Preserve every possible type, including referenced and nullable unions. */ -const detectedTypes = ( - schema: unknown, - references: JsonSchemaReferences, - inherited: JsonSchemaScope | undefined, - active: ReadonlySet = new Set(), -): readonly string[] | undefined => { - if (!isSchemaObject(schema)) return undefined; - const scope = references.enter(schema, inherited); - if (scope === undefined) return undefined; - if (typeof schema.$ref === "string") { - if (active.has(schema)) return undefined; - const target = references.resolve(scope, schema.$ref); - return target.kind === "target" - ? detectedTypes( - target.value, - references, - scope, - new Set([...active, schema]), - ) - : undefined; - } - if (typeof schema.type === "string") return [schema.type]; - if ( - Array.isArray(schema.type) && - schema.type.every((type) => typeof type === "string") - ) - return schema.type; - if (Array.isArray(schema.enum) && schema.enum.length > 0) { - const enumTypes = enumValueTypes(schema.enum); - if (enumTypes.every((type) => jsonValueTypes.includes(type))) - return enumTypes; - } - const alternatives = Array.isArray(schema.anyOf) - ? schema.anyOf - : Array.isArray(schema.oneOf) - ? schema.oneOf - : []; - if (alternatives.length === 0) return undefined; - const types = alternatives.map((branch) => - detectedTypes(branch, references, scope, active), - ); - if (types.some((type) => type === undefined)) return undefined; - return [...new Set(types.flatMap((type) => type ?? []))]; +/** Resolve references and union branches without losing their source scopes. */ +const collectAlternatives = ( + observe: ( + schema: Record, + scope: JsonSchemaScope, + ) => readonly Item[] | undefined | null, + combine: (items: Item[]) => readonly Item[] = (items) => items, +) => { + const collect = ( + schema: unknown, + references: JsonSchemaReferences, + inherited: JsonSchemaScope | undefined, + active: ReadonlySet = new Set(), + ): readonly Item[] | undefined => { + if (!isSchemaObject(schema)) return undefined; + const scope = references.enter(schema, inherited); + if (scope === undefined) return undefined; + if (typeof schema.$ref === "string") { + if (active.has(schema)) return undefined; + const target = references.resolve(scope, schema.$ref); + return target.kind === "target" + ? collect(target.value, references, scope, new Set([...active, schema])) + : undefined; + } + // null asks to inspect the union; undefined is an unresolved result. + const observed = observe(schema, scope); + if (observed !== null) return observed; + const alternatives = Array.isArray(schema.anyOf) + ? schema.anyOf + : Array.isArray(schema.oneOf) + ? schema.oneOf + : []; + if (alternatives.length === 0) return undefined; + const nested = alternatives.map((branch) => + collect(branch, references, scope, active), + ); + return nested.some((items) => items === undefined) + ? undefined + : combine(nested.flatMap((items) => items ?? [])); + }; + return collect; }; +/** Preserve every possible type, including referenced and nullable unions. */ +const detectedTypes = collectAlternatives( + (schema) => { + if (typeof schema.type === "string") return [schema.type]; + if ( + Array.isArray(schema.type) && + schema.type.every((type) => typeof type === "string") + ) + return schema.type; + if (Array.isArray(schema.enum) && schema.enum.length > 0) { + const enumTypes = enumValueTypes(schema.enum); + if (enumTypes.every((type) => jsonValueTypes.includes(type))) + return enumTypes; + } + return null; + }, + (types) => [...new Set(types)], +); + interface ScopedSchema { readonly schema: Record; readonly scope: JsonSchemaScope; @@ -520,52 +536,21 @@ interface ScopedSchema { * schemas and unresolved references are unknown because one item type would * not describe every accepted element. */ -const arrayItemAlternatives = ( - schema: unknown, - references: JsonSchemaReferences, - inherited: JsonSchemaScope | undefined, - active: ReadonlySet = new Set(), -): readonly ScopedSchema[] | undefined => { - if (!isSchemaObject(schema)) return undefined; - const scope = references.enter(schema, inherited); - if (scope === undefined) return undefined; - if (typeof schema.$ref === "string") { - if (active.has(schema)) return undefined; - const target = references.resolve(scope, schema.$ref); - return target.kind === "target" - ? arrayItemAlternatives( - target.value, - references, - scope, - new Set([...active, schema]), - ) - : undefined; - } - const types = - typeof schema.type === "string" - ? [schema.type] - : Array.isArray(schema.type) - ? schema.type - : undefined; - if (types !== undefined) { +const arrayItemAlternatives = collectAlternatives( + (schema, scope) => { + const types = + typeof schema.type === "string" + ? [schema.type] + : Array.isArray(schema.type) + ? schema.type + : undefined; + if (types === undefined) return null; if (!types.includes("array")) return []; return isSchemaObject(schema.items) && schema.prefixItems === undefined ? [{ schema: schema.items, scope }] : undefined; - } - const alternatives = Array.isArray(schema.anyOf) - ? schema.anyOf - : Array.isArray(schema.oneOf) - ? schema.oneOf - : []; - if (alternatives.length === 0) return undefined; - const nested = alternatives.map((branch) => - arrayItemAlternatives(branch, references, scope, active), - ); - return nested.some((items) => items === undefined) - ? undefined - : nested.flatMap((items) => items ?? []); -}; + }, +); /** Nested arrays beyond this level advertise an unconstrained item schema. */ const REDUCED_ITEM_MAX_DEPTH = 3;