This commit is contained in:
seb
2026-10-17 03:08:18 +02:00
parent c61df92d63
commit 988b4848cf
3 changed files with 341 additions and 345 deletions

View File

@@ -684,17 +684,46 @@ function collectSchemaAliases(operations) {
}
}
const candidates = [...shapes.entries()]
.filter(([, item]) => item.count >= 3 && item.shape.length >= 36)
.sort((a, b) => b[1].shape.length - a[1].shape.length || a[0].localeCompare(b[0]));
let candidates = [...shapes.values()]
.filter((item) => item.count >= 3 && item.shape.length >= 36)
.sort((a, b) => b.shape.length - a.shape.length || a.shape.localeCompare(b.shape));
const isShort = (item, rendered) => {
const propCount = Object.keys(item.schema.properties || {}).length;
if (propCount <= 1) return true;
if (rendered.length < 36) return true;
return false;
};
while (true) {
const aliases = new Map();
aliases.compactTypes = true;
for (let i = 0; i < candidates.length; i++) {
aliases.set(candidates[i].shape, `T${i + 1}`);
}
const filtered = [];
let droppedAny = false;
for (const item of candidates) {
const rendered = summarizeSchema(item.schema, spec.components, 0, item.markOptional, aliases, true);
if (isShort(item, rendered)) {
droppedAny = true;
} else {
filtered.push(item);
}
}
candidates = filtered;
if (!droppedAny) break;
}
const aliases = new Map();
aliases.compactTypes = true;
const definitions = [];
for (const [, item] of candidates) {
for (const item of candidates) {
const alias = `T${definitions.length + 1}`;
aliases.set(item.shape, alias);
definitions.push({ alias, ...item });
}
aliases.compactTypes = true;
return { aliases, definitions };
}