refactor(contracts): share compact schema alternative traversal (#16698)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-04 19:06:39 +08:00
1 parent 3be7f44224
commit 6a8f09aafe
1 file changed
+71 -86
+71 -86
View File
@@ -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<object> = 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 = <Item>(
observe: (
schema: Record<string, unknown>,
scope: JsonSchemaScope,
) => readonly Item[] | undefined | null,
combine: (items: Item[]) => readonly Item[] = (items) => items,
) => {
const collect = (
schema: unknown,
references: JsonSchemaReferences,
inherited: JsonSchemaScope | undefined,
active: ReadonlySet<object> = 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<string>(
(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<string, unknown>;
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<object> = 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<ScopedSchema>(
(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;