Skip to content

Commit

Permalink
fix(printer): 处理非基本类型格式的JS文档注释
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Jul 19, 2024
1 parent a749471 commit 4dff5e9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/printer/JsDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function formatLine(key: string, val: unknown) {
if (isString(val) || isNumber(val)) return `@${key} ${val}`;
}

const supportTypes = ['string', 'number', 'boolean', 'object', 'array', 'null'];

export class JsDoc {
lines: string[] = [];

Expand Down Expand Up @@ -61,13 +63,15 @@ export class JsDoc {
}

static fromSchema(schema: OpenApi3.SchemaObject) {
const { deprecated, description, default: defaultValue, format, example, title, externalDocs } = schema;
const { deprecated, description, default: defaultValue, format, example, title, externalDocs, type } = schema;
const types = isArray(type) ? type : isString(type) ? [type] : undefined;

return {
summary: title,
description,
deprecated,
default: defaultValue,
format,
format: format || types?.filter((type) => !supportTypes.includes(type)).join(' | ') || false,
example,
externalDocs,
};
Expand Down
5 changes: 3 additions & 2 deletions src/printer/Schemata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class Schemata {
enum: schema.enum,
type,
} as OpenApi3.SchemaObject),
true,
),
),
'|',
Expand Down Expand Up @@ -263,9 +264,9 @@ export class Schemata {
};
}

toString(schema: OpenApi3_Schema) {
toString(schema: OpenApi3_Schema, ignoreComments = false) {
const result = this.print(schema);
return Schemata.toString(result);
return Schemata.toString(result, ignoreComments);
}

static toString(result: SchemaResult, ignoreComments = false) {
Expand Down
3 changes: 3 additions & 0 deletions test/printer/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ test('1路径 + 1请求 + 2path', () => {
*/
export async function getApiAbcDef(path:{
"var":string;
/**
* @format integer
*/
"xyz"?:number;
},config?:AxiosRequestConfig): AxiosPromise<unknown> {
return axios({
Expand Down

0 comments on commit 4dff5e9

Please sign in to comment.