Skip to content

Commit 8222d04

Browse files
committed
fix: 修正 ref 可选性判断
1 parent 31ebf30 commit 8222d04

File tree

12 files changed

+169
-40
lines changed

12 files changed

+169
-40
lines changed

src/printer/Schemata.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ export class Schemata {
2727
return {
2828
comments: JsDoc.fromRef(schema),
2929
type: this.named.getRefType(schema.$ref) || 'unknown',
30-
// 引用类型,常规为必填
31-
required: true,
30+
required: false,
3231
};
3332
}
3433

src/printer/index.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,15 @@ export class Printer {
8181

8282
print(configs?: PrinterConfigs) {
8383
Object.assign(this.configs, configs);
84-
const { hideInfo, hideComponents, hideImports, hidePaths } = this.configs;
84+
const { hideLintComments, hideInfo, hideComponents, hideImports, hidePaths } = this.configs;
85+
const eslintComments = [
86+
//
87+
'/* eslint-disable @typescript-eslint/ban-ts-comment */',
88+
'/* eslint-disable @typescript-eslint/no-explicit-any */',
89+
].join('\n');
8590

8691
return [
87-
//
92+
!hideLintComments && eslintComments,
8893
!hideInfo && this._printInfo(),
8994
!hideImports && this._printImports(),
9095
!hideComponents && this._printComponents(),
@@ -94,6 +99,25 @@ export class Printer {
9499
.join('\n\n');
95100
}
96101

102+
private _printInfo() {
103+
const { contact, description, license, summary, termsOfService, title, version } = this.document.info;
104+
const { externalDocs } = this.document;
105+
const { name, email, url } = contact || {};
106+
const jsDoc = new JsDoc();
107+
const { module } = this.configs;
108+
if (module) jsDoc.addComments({ module });
109+
const extDoc = JsDoc.printExternalDoc(externalDocs);
110+
jsDoc.addComments({
111+
title,
112+
version,
113+
contact: name || url || email ? [name, email ? `<${email}>` : '', url ? `(${url})` : ''].filter(Boolean).join(' ') : undefined,
114+
description,
115+
summary,
116+
see: extDoc,
117+
});
118+
return jsDoc.print();
119+
}
120+
97121
private _printImports() {
98122
const {
99123
axiosImportName = AXIOS_IMPORT_NAME,
@@ -111,9 +135,6 @@ export class Printer {
111135

112136
return [
113137
//
114-
'/* eslint-disable @typescript-eslint/ban-ts-comment */',
115-
'/* eslint-disable @typescript-eslint/no-explicit-any */',
116-
'',
117138
axiosNamedImport
118139
? // 具名导入
119140
`import {${axiosImportName}} from "${importPath}";`
@@ -127,25 +148,6 @@ export class Printer {
127148
].join('\n');
128149
}
129150

130-
private _printInfo() {
131-
const { contact, description, license, summary, termsOfService, title, version } = this.document.info;
132-
const { externalDocs } = this.document;
133-
const { name, email, url } = contact || {};
134-
const jsDoc = new JsDoc();
135-
const { module } = this.configs;
136-
if (module) jsDoc.addComments({ module });
137-
const extDoc = JsDoc.printExternalDoc(externalDocs);
138-
jsDoc.addComments({
139-
title,
140-
version,
141-
contact: name || url || email ? [name, email ? `<${email}>` : '', url ? `(${url})` : ''].filter(Boolean).join(' ') : undefined,
142-
description,
143-
summary,
144-
see: extDoc,
145-
});
146-
return jsDoc.print();
147-
}
148-
149151
private _printComponents() {
150152
return Object.entries(this.document.components?.schemas || {})
151153
.map(([name, schema]) => {

src/printer/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export type PrinterConfigs = {
119119
*/
120120
file?: string;
121121

122+
hideLintComments?: boolean;
122123
hideInfo?: boolean;
123124
hideImports?: boolean;
124125
hideComponents?: boolean;

test/example-dest/3.0/pet-store.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* eslint-disable @typescript-eslint/ban-ts-comment */
2+
/* eslint-disable @typescript-eslint/no-explicit-any */
3+
14
/**
25
* @title Swagger Petstore - OpenAPI 3.0
36
* @version 1.0.19
@@ -13,9 +16,6 @@ Some useful links:
1316
* @see {@link http://swagger.io Find out more about Swagger}
1417
*/
1518

16-
/* eslint-disable @typescript-eslint/ban-ts-comment */
17-
/* eslint-disable @typescript-eslint/no-explicit-any */
18-
1919
import axios from "axios";
2020
import type {AxiosRequestConfig, AxiosPromise} from "axios";
2121
import type {OneOf, AllOf, AnyOf, AnyObject, AnyArray} from "pkg-name-for-test/client";
@@ -151,7 +151,7 @@ export type Pet = {
151151
* @example doggie
152152
*/
153153
"name":string;
154-
"category":Category;
154+
"category"?:Category;
155155
"photoUrls":((string)[]);
156156
"tags"?:((Tag)[]);
157157
/**

test/example-dest/3.1/pet-store.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* eslint-disable @typescript-eslint/ban-ts-comment */
2+
/* eslint-disable @typescript-eslint/no-explicit-any */
3+
14
/**
25
* @title Swagger Petstore - OpenAPI 3.1
36
* @version 1.0.6
@@ -9,9 +12,6 @@ Swagger at [http://swagger.io](http://swagger.io).
912
* @see {@link http://swagger.io Find out more about Swagger}
1013
*/
1114

12-
/* eslint-disable @typescript-eslint/ban-ts-comment */
13-
/* eslint-disable @typescript-eslint/no-explicit-any */
14-
1515
import axios from "axios";
1616
import type {AxiosRequestConfig, AxiosPromise} from "axios";
1717
import type {OneOf, AllOf, AnyOf, AnyObject, AnyArray} from "pkg-name-for-test/client";
@@ -46,7 +46,7 @@ export type Pet = {
4646
/**
4747
* @description Pet Category
4848
*/
49-
"category":unknown;
49+
"category"?:unknown;
5050
/**
5151
* @example doggie
5252
*/
@@ -62,8 +62,8 @@ export type Pet = {
6262
* @example 7
6363
*/
6464
"availableInstances"?:number;
65-
"petDetailsId":unknown;
66-
"petDetails":PetDetails;
65+
"petDetailsId"?:unknown;
66+
"petDetails"?:PetDetails;
6767
};
6868

6969
export type PetDetails = {
@@ -75,8 +75,8 @@ export type PetDetails = {
7575
/**
7676
* @description PetDetails Category
7777
*/
78-
"category":Category;
79-
"tag":Tag;
78+
"category"?:Category;
79+
"tag"?:Tag;
8080
};
8181

8282
export type Tag = {

test/printer/path.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test('1路径 + 1请求', () => {
1616

1717
expect(
1818
printer.print({
19+
hideLintComments: true,
1920
hideInfo: true,
2021
hideImports: true,
2122
}),
@@ -53,7 +54,10 @@ test('1路径 + 1请求 * module', () => {
5354
hideImports: true,
5455
}),
5556
).toMatchInlineSnapshot(`
56-
"/**
57+
"/* eslint-disable @typescript-eslint/ban-ts-comment */
58+
/* eslint-disable @typescript-eslint/no-explicit-any */
59+
60+
/**
5761
* @module TTT
5862
* @title api
5963
* @version v1
@@ -90,6 +94,7 @@ test('1路径 + 2请求', () => {
9094

9195
expect(
9296
printer.print({
97+
hideLintComments: true,
9398
hideInfo: true,
9499
hideImports: true,
95100
}),
@@ -149,6 +154,7 @@ test('1路径 + 1请求 + 1query', () => {
149154

150155
expect(
151156
printer.print({
157+
hideLintComments: true,
152158
hideInfo: true,
153159
hideImports: true,
154160
}),
@@ -199,6 +205,7 @@ test('1路径 + 1请求 + 1query with duplicate', () => {
199205

200206
expect(
201207
printer.print({
208+
hideLintComments: true,
202209
hideInfo: true,
203210
hideImports: true,
204211
}),
@@ -250,6 +257,7 @@ test('1路径 + 1请求 + 1path', () => {
250257

251258
expect(
252259
printer.print({
260+
hideLintComments: true,
253261
hideInfo: true,
254262
hideImports: true,
255263
}),
@@ -307,6 +315,7 @@ test('1路径 + 1请求 + 2path', () => {
307315

308316
expect(
309317
printer.print({
318+
hideLintComments: true,
310319
hideInfo: true,
311320
hideImports: true,
312321
}),
@@ -371,6 +380,7 @@ test('1路径 + 1请求 + 2query', () => {
371380

372381
expect(
373382
printer.print({
383+
hideLintComments: true,
374384
hideInfo: true,
375385
hideImports: true,
376386
}),
@@ -446,6 +456,7 @@ test('1路径 + 1请求 + 2query + 1path', () => {
446456

447457
expect(
448458
printer.print({
459+
hideLintComments: true,
449460
hideInfo: true,
450461
hideImports: true,
451462
}),
@@ -532,6 +543,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request primitive', () => {
532543

533544
expect(
534545
printer.print({
546+
hideLintComments: true,
535547
hideInfo: true,
536548
hideImports: true,
537549
}),
@@ -626,6 +638,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request object', () => {
626638

627639
expect(
628640
printer.print({
641+
hideLintComments: true,
629642
hideInfo: true,
630643
hideImports: true,
631644
}),
@@ -732,6 +745,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request object + 1response primitive
732745

733746
expect(
734747
printer.print({
748+
hideLintComments: true,
735749
hideInfo: true,
736750
hideImports: true,
737751
}),
@@ -856,6 +870,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request object + 1response object',
856870

857871
expect(
858872
printer.print({
873+
hideLintComments: true,
859874
hideInfo: true,
860875
hideImports: true,
861876
}),

test/printer/ref-parameter.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ test('ref-parameter', () => {
4040
});
4141
expect(
4242
printer.print({
43+
hideLintComments: true,
4344
hideInfo: true,
4445
hideImports: true,
4546
}),

test/printer/ref-request.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ test('ref-request', () => {
4747
});
4848
expect(
4949
printer.print({
50+
hideLintComments: true,
5051
hideInfo: true,
5152
hideImports: true,
5253
}),

0 commit comments

Comments
 (0)