u
This commit is contained in:
@@ -560,6 +560,16 @@ function summarizeSchema(
|
||||
if (!resolved) return schemaRefName(schema) || '';
|
||||
if (resolved.$ref) return schemaRefName(resolved) || '';
|
||||
|
||||
const union = resolved.oneOf || resolved.anyOf;
|
||||
if (Array.isArray(union) && union.length) {
|
||||
const parts = union
|
||||
.map((s) => summarizeSchema(s, components, depth + 1, markOptional, aliases))
|
||||
.filter(Boolean);
|
||||
if (parts.length) {
|
||||
return [...new Set(parts)].join('|');
|
||||
}
|
||||
}
|
||||
|
||||
if (resolved.type === 'array') {
|
||||
const item = summarizeSchema(resolved.items, components, depth + 1, markOptional, aliases) || 'any';
|
||||
return `${item}[]`;
|
||||
@@ -567,7 +577,7 @@ function summarizeSchema(
|
||||
|
||||
if (resolved.type === 'object' || resolved.properties) {
|
||||
const shape = aliases && summarizeSchema(resolved, components, 0, markOptional);
|
||||
const alias = shape && aliases.get(`${markOptional ? 1 : 0}\0${shape}`);
|
||||
const alias = shape && aliases.get(shape);
|
||||
if (alias && !skipRootAlias) return alias;
|
||||
|
||||
const hasExplicitRequired = Array.isArray(resolved.required);
|
||||
@@ -602,6 +612,22 @@ function summarizeSchema(
|
||||
}
|
||||
|
||||
if (resolved.type) {
|
||||
if (Array.isArray(resolved.type)) {
|
||||
const types = resolved.type.map((raw) => {
|
||||
if (raw === 'null') return 'null';
|
||||
if (aliases?.compactTypes) {
|
||||
return {
|
||||
string: 'str',
|
||||
integer: 'int',
|
||||
number: 'num',
|
||||
boolean: 'bool',
|
||||
object: '{}',
|
||||
}[raw] || raw;
|
||||
}
|
||||
return raw;
|
||||
});
|
||||
return types.join('|');
|
||||
}
|
||||
let t = resolved.type;
|
||||
if (aliases?.compactTypes) {
|
||||
t = {
|
||||
@@ -633,12 +659,11 @@ function collectSchemaAliases(operations) {
|
||||
if (resolved.type !== 'object' && !resolved.properties) return;
|
||||
|
||||
const shape = summarizeSchema(resolved, spec.components, 0, markOptional);
|
||||
const key = `${markOptional ? 1 : 0}\0${shape}`;
|
||||
const current = shapes.get(key);
|
||||
const current = shapes.get(shape);
|
||||
if (current) {
|
||||
current.count += 1;
|
||||
} else {
|
||||
shapes.set(key, { count: 1, shape, schema: resolved, markOptional });
|
||||
shapes.set(shape, { count: 1, shape, schema: resolved, markOptional });
|
||||
}
|
||||
for (const prop of Object.values(resolved.properties || {})) {
|
||||
visit(prop, markOptional);
|
||||
@@ -664,9 +689,9 @@ function collectSchemaAliases(operations) {
|
||||
.sort((a, b) => b[1].shape.length - a[1].shape.length || a[0].localeCompare(b[0]));
|
||||
const aliases = new Map();
|
||||
const definitions = [];
|
||||
for (const [key, item] of candidates) {
|
||||
for (const [, item] of candidates) {
|
||||
const alias = `T${definitions.length + 1}`;
|
||||
aliases.set(key, alias);
|
||||
aliases.set(item.shape, alias);
|
||||
definitions.push({ alias, ...item });
|
||||
}
|
||||
aliases.compactTypes = true;
|
||||
@@ -680,7 +705,7 @@ function collectSchemaAliases(operations) {
|
||||
* @returns {string}
|
||||
*/
|
||||
function paramType(param, aliases = null) {
|
||||
return summarizeSchema(param?.schema, spec.components, 0, false, aliases) || 'string';
|
||||
return summarizeSchema(param?.schema, spec.components, 0, false, aliases) || (aliases?.compactTypes ? 'str' : 'string');
|
||||
}
|
||||
|
||||
function summarizeOperationParams(operation, aliases = null) {
|
||||
|
||||
Reference in New Issue
Block a user