Skip to content

Commit

Permalink
fix: 修复 UnknownObject
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Aug 5, 2024
1 parent 1f4798b commit 6959961
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
45 changes: 23 additions & 22 deletions src/printer/Schemata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export class Schemata {
};
}

const { type, allOf, oneOf, anyOf } = schema;
const { type, allOf, oneOf, anyOf, required } = schema;
const requiredBool = isBoolean(required) ? required : false;
const comments = JsDoc.fromSchema(schema);

if (allOf && allOf.length > 0) {
Expand Down Expand Up @@ -66,6 +67,19 @@ export class Schemata {
}

if (isArray(type)) {
if (type.length === 0) {
return this._printUnknown(schema, requiredBool);
}

if (type.length === 1) {
return this.print({
...schema,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
type: type[0],
});
}

return {
comments,
type: withGroup(
Expand Down Expand Up @@ -165,38 +179,27 @@ export class Schemata {
} else if ('items' in schema) {
return this._printArray(schema as unknown as OpenAPILatest.ArraySchemaObject);
} else {
return this._printUnknown(schema);
return this._printUnknown(schema, requiredBool);
}
}

default:
never(type);
return this._printUnknown(schema);
return this._printUnknown(schema, requiredBool);
}
}

private _printArray(schema: OpenAPILatest.ArraySchemaObject) {
const comments = JsDoc.fromSchema(schema);
const { minItems, maxItems } = schema;
// const items = 'items' in schema ? schema.items : undefined;

// if (!items) {
// return this._printUnknown(schema, 'array');
// }

// const explicit = isArray(items);
// const subSchemas = explicit ? items : [items];
// const subTypes = subSchemas.map((s) => this.toString(s));
// const subTypes = [this.toString(items)];
const { minItems, maxItems, items } = schema;

return {
comments: {
...comments,
minItems,
maxItems,
},
// type: withNullable(explicit ? `[${subTypes.join(',')}]` : `((${subTypes.join('|')})[])`, schema.nullable),
type: this.toString(schema.items) + '[]',
type: this.toString(items) + '[]',
required: false,
};
}
Expand Down Expand Up @@ -235,7 +238,7 @@ export class Schemata {
const objectTypes = [...explicitTypes, ...genericTypes];

if (objectTypes.length === 0) {
return this._printUnknown(schema, 'object', isBoolean(schema.required) ? schema.required : false);
return this._printUnknown(schema, isBoolean(schema.required) ? schema.required : false, 'UnknownObject');
}

return {
Expand All @@ -245,15 +248,13 @@ export class Schemata {
};
}

private _printUnknown(schema: OpenApiLatest_Schema, type: 'object' | 'array' | 'primitive' = 'primitive', required?: boolean) {
private _printUnknown(schema: OpenApiLatest_Schema, required = false, type = 'unknown') {
const comments = JsDoc.fromSchema(schema);

return {
comments,
type:
//
type === 'object' ? 'AnyObject' : type === 'array' ? 'AnyArray' : 'unknown',
required: isBoolean(required) ? required : type === 'primitive',
type,
required,
};
}

Expand Down
7 changes: 3 additions & 4 deletions src/printer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,12 @@ export async function ${funcName}(${requestArgs.toArgs(axiosRequestConfigTypeNam
this._parseContents(arg, content, response, (contentType, content) => contentMatch(contentType, content, response));
}

// type OneOf<T extends unknown[]> = T extends [infer A, ...infer B] ? A | OneOf<B> : never;
// type AllOf<T extends unknown[]> = T extends [infer A, ...infer B] ? A & AllOf<B> : unknown;
static helpersCode = `
// helpers --- start
type OneOf<T extends unknown[]> = T extends [infer A, ...infer B] ? A | OneOf<B> : never;
type AllOf<T extends unknown[]> = T extends [infer A, ...infer B] ? A & AllOf<B> : unknown;
type AnyOf<T extends unknown[]> = T extends [infer A, ...infer B] ? A | AnyOf<B> | (A & AnyOf<B>) : never;
type AnyObject = Record<string, any>;
type AnyArray = any[];
type UnknownObject = Record<string, unknown>;
// helpers --- end
`;
}

0 comments on commit 6959961

Please sign in to comment.