Skip to content

Commit

Permalink
fix: 修正 ref 可选性判断
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Jul 22, 2024
1 parent 31ebf30 commit 8222d04
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 40 deletions.
3 changes: 1 addition & 2 deletions src/printer/Schemata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export class Schemata {
return {
comments: JsDoc.fromRef(schema),
type: this.named.getRefType(schema.$ref) || 'unknown',
// 引用类型,常规为必填
required: true,
required: false,
};
}

Expand Down
50 changes: 26 additions & 24 deletions src/printer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,15 @@ export class Printer {

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

return [
//
!hideLintComments && eslintComments,
!hideInfo && this._printInfo(),
!hideImports && this._printImports(),
!hideComponents && this._printComponents(),
Expand All @@ -94,6 +99,25 @@ export class Printer {
.join('\n\n');
}

private _printInfo() {
const { contact, description, license, summary, termsOfService, title, version } = this.document.info;
const { externalDocs } = this.document;
const { name, email, url } = contact || {};
const jsDoc = new JsDoc();
const { module } = this.configs;
if (module) jsDoc.addComments({ module });
const extDoc = JsDoc.printExternalDoc(externalDocs);
jsDoc.addComments({
title,
version,
contact: name || url || email ? [name, email ? `<${email}>` : '', url ? `(${url})` : ''].filter(Boolean).join(' ') : undefined,
description,
summary,
see: extDoc,
});
return jsDoc.print();
}

private _printImports() {
const {
axiosImportName = AXIOS_IMPORT_NAME,
Expand All @@ -111,9 +135,6 @@ export class Printer {

return [
//
'/* eslint-disable @typescript-eslint/ban-ts-comment */',
'/* eslint-disable @typescript-eslint/no-explicit-any */',
'',
axiosNamedImport
? // 具名导入
`import {${axiosImportName}} from "${importPath}";`
Expand All @@ -127,25 +148,6 @@ export class Printer {
].join('\n');
}

private _printInfo() {
const { contact, description, license, summary, termsOfService, title, version } = this.document.info;
const { externalDocs } = this.document;
const { name, email, url } = contact || {};
const jsDoc = new JsDoc();
const { module } = this.configs;
if (module) jsDoc.addComments({ module });
const extDoc = JsDoc.printExternalDoc(externalDocs);
jsDoc.addComments({
title,
version,
contact: name || url || email ? [name, email ? `<${email}>` : '', url ? `(${url})` : ''].filter(Boolean).join(' ') : undefined,
description,
summary,
see: extDoc,
});
return jsDoc.print();
}

private _printComponents() {
return Object.entries(this.document.components?.schemas || {})
.map(([name, schema]) => {
Expand Down
1 change: 1 addition & 0 deletions src/printer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export type PrinterConfigs = {
*/
file?: string;

hideLintComments?: boolean;
hideInfo?: boolean;
hideImports?: boolean;
hideComponents?: boolean;
Expand Down
8 changes: 4 additions & 4 deletions test/example-dest/3.0/pet-store.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-explicit-any */

/**
* @title Swagger Petstore - OpenAPI 3.0
* @version 1.0.19
Expand All @@ -13,9 +16,6 @@ Some useful links:
* @see {@link http://swagger.io Find out more about Swagger}
*/

/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-explicit-any */

import axios from "axios";
import type {AxiosRequestConfig, AxiosPromise} from "axios";
import type {OneOf, AllOf, AnyOf, AnyObject, AnyArray} from "pkg-name-for-test/client";
Expand Down Expand Up @@ -151,7 +151,7 @@ export type Pet = {
* @example doggie
*/
"name":string;
"category":Category;
"category"?:Category;
"photoUrls":((string)[]);
"tags"?:((Tag)[]);
/**
Expand Down
16 changes: 8 additions & 8 deletions test/example-dest/3.1/pet-store.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-explicit-any */

/**
* @title Swagger Petstore - OpenAPI 3.1
* @version 1.0.6
Expand All @@ -9,9 +12,6 @@ Swagger at [http://swagger.io](http://swagger.io).
* @see {@link http://swagger.io Find out more about Swagger}
*/

/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-explicit-any */

import axios from "axios";
import type {AxiosRequestConfig, AxiosPromise} from "axios";
import type {OneOf, AllOf, AnyOf, AnyObject, AnyArray} from "pkg-name-for-test/client";
Expand Down Expand Up @@ -46,7 +46,7 @@ export type Pet = {
/**
* @description Pet Category
*/
"category":unknown;
"category"?:unknown;
/**
* @example doggie
*/
Expand All @@ -62,8 +62,8 @@ export type Pet = {
* @example 7
*/
"availableInstances"?:number;
"petDetailsId":unknown;
"petDetails":PetDetails;
"petDetailsId"?:unknown;
"petDetails"?:PetDetails;
};

export type PetDetails = {
Expand All @@ -75,8 +75,8 @@ export type PetDetails = {
/**
* @description PetDetails Category
*/
"category":Category;
"tag":Tag;
"category"?:Category;
"tag"?:Tag;
};

export type Tag = {
Expand Down
17 changes: 16 additions & 1 deletion test/printer/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ test('1路径 + 1请求', () => {

expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down Expand Up @@ -53,7 +54,10 @@ test('1路径 + 1请求 * module', () => {
hideImports: true,
}),
).toMatchInlineSnapshot(`
"/**
"/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* @module TTT
* @title api
* @version v1
Expand Down Expand Up @@ -90,6 +94,7 @@ test('1路径 + 2请求', () => {

expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down Expand Up @@ -149,6 +154,7 @@ test('1路径 + 1请求 + 1query', () => {

expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down Expand Up @@ -199,6 +205,7 @@ test('1路径 + 1请求 + 1query with duplicate', () => {

expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down Expand Up @@ -250,6 +257,7 @@ test('1路径 + 1请求 + 1path', () => {

expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down Expand Up @@ -307,6 +315,7 @@ test('1路径 + 1请求 + 2path', () => {

expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down Expand Up @@ -371,6 +380,7 @@ test('1路径 + 1请求 + 2query', () => {

expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down Expand Up @@ -446,6 +456,7 @@ test('1路径 + 1请求 + 2query + 1path', () => {

expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down Expand Up @@ -532,6 +543,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request primitive', () => {

expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down Expand Up @@ -626,6 +638,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request object', () => {

expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down Expand Up @@ -732,6 +745,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request object + 1response primitive

expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down Expand Up @@ -856,6 +870,7 @@ test('1路径 + 1请求 + 2query + 1path + 1request object + 1response object',

expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down
1 change: 1 addition & 0 deletions test/printer/ref-parameter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ test('ref-parameter', () => {
});
expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down
1 change: 1 addition & 0 deletions test/printer/ref-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ test('ref-request', () => {
});
expect(
printer.print({
hideLintComments: true,
hideInfo: true,
hideImports: true,
}),
Expand Down
Loading

0 comments on commit 8222d04

Please sign in to comment.