Skip to content

Commit

Permalink
feat: add swagger operationId (#3122)
Browse files Browse the repository at this point in the history
* feat: add swagger operationId

* chore(docs): update documents for swagger
  • Loading branch information
odex21 committed Aug 1, 2023
1 parent a196b53 commit 4db39a6
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 12 deletions.
12 changes: 12 additions & 0 deletions packages/swagger/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* @see https://github.com/OAI/OpenAPI-Specification/blob/3.0.0-rc0/versions/3.0.md
*/

import type { RouterOption } from '@midwayjs/core';

export interface OpenAPIObject {
openapi: string;
info: InfoObject;
Expand Down Expand Up @@ -412,4 +414,14 @@ export interface SwaggerOptions {
useUnsafeMarkdown?: boolean;
tryItOutEnabled?: boolean;
};
documentOptions?: {
/**
* 自定义 operationIdFactory,用于生成 operationId
* @default () => controllerKey_webRouter.methodKey
*/
operationIdFactory?: (
controllerKey: string,
webRouter: RouterOption
) => string;
};
}
16 changes: 15 additions & 1 deletion packages/swagger/src/swaggerExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export class SwaggerExplorer {
private swaggerConfig: SwaggerOptions;

private documentBuilder = new DocumentBuilder();
private operationIdFactory = (
controllerKey: string,
webRouter: RouterOption
) => `${controllerKey.toLowerCase()}_${webRouter.method.toLocaleLowerCase()}`;

@Init()
async init() {
Expand Down Expand Up @@ -92,6 +96,12 @@ export class SwaggerExplorer {
this.documentBuilder.addTag(t.name, t.description, t.externalDocs);
}
}

if (this.swaggerConfig?.documentOptions?.operationIdFactory) {
this.operationIdFactory =
this.swaggerConfig.documentOptions.operationIdFactory;
}

// 设置 auth 类型
if (Array.isArray(this.swaggerConfig?.auth)) {
for (const a of this.swaggerConfig?.auth) {
Expand Down Expand Up @@ -292,7 +302,7 @@ export class SwaggerExplorer {
operMeta?.metadata?.description,
webRouter.description
),
// operationId: `${webRouter.requestMethod}_${(operMeta?.metadata?.operationId || webRouter.method)}`,
operationId: this.getOperationId(target.name, webRouter),
tags: operMeta?.metadata?.tags || [],
};
/**
Expand Down Expand Up @@ -503,6 +513,10 @@ export class SwaggerExplorer {
paths[url] = opts;
}

getOperationId(controllerKey: string, webRouter: RouterOption) {
return this.operationIdFactory(controllerKey, webRouter);
}

private expandSchemaRef(p: any, name?: string) {
let schemaName = name;
if (p.schema['$ref']) {
Expand Down
6 changes: 6 additions & 0 deletions packages/swagger/test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ exports[`/test/index.test.ts should fix issue1976 1`] = `
"paths": {
"/cats/login": {
"post": {
"operationId": "usercontroller_login",
"parameters": [],
"requestBody": {
"content": {
Expand All @@ -92,6 +93,7 @@ exports[`/test/index.test.ts should fix issue1976 1`] = `
},
"/cats/smsLogin": {
"post": {
"operationId": "usercontroller_smslogin",
"parameters": [],
"requestBody": {
"content": {
Expand Down Expand Up @@ -170,6 +172,7 @@ exports[`/test/index.test.ts should fix issue2603 1`] = `
"paths": {
"/api/get_role": {
"get": {
"operationId": "apicontroller_getrole",
"parameters": [],
"responses": {
"default": {
Expand All @@ -190,6 +193,7 @@ exports[`/test/index.test.ts should fix issue2603 1`] = `
},
"/api/get_user": {
"get": {
"operationId": "apicontroller_getuser",
"parameters": [
{
"in": "query",
Expand Down Expand Up @@ -397,6 +401,7 @@ exports[`/test/index.test.ts test swagger should get swagger json 5`] = `
exports[`/test/index.test.ts test swagger should get swagger json 6`] = `
{
"get": {
"operationId": "cats_findone",
"parameters": [
{
"in": "header",
Expand Down Expand Up @@ -486,6 +491,7 @@ exports[`/test/index.test.ts test swagger should get swagger json 6`] = `
},
},
"post": {
"operationId": "cats_create",
"parameters": [
{
"in": "header",
Expand Down
3 changes: 3 additions & 0 deletions packages/swagger/test/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ exports[`/test/parser.test.ts should fix issue#2286 with array example 1`] = `
"/api/update_user": {
"post": {
"description": undefined,
"operationId": "catcontroller_updateuser",
"parameters": [],
"responses": {
"200": {
Expand Down Expand Up @@ -299,6 +300,7 @@ exports[`/test/parser.test.ts should test ref path generate 1`] = `
"/api/update_user": {
"post": {
"description": undefined,
"operationId": "apicontroller_updateuser",
"parameters": [],
"responses": {
"200": {
Expand Down Expand Up @@ -358,6 +360,7 @@ exports[`/test/parser.test.ts should test simple case with example 1`] = `
"/api/update_user": {
"post": {
"description": undefined,
"operationId": "catcontroller_updateuser",
"parameters": [],
"responses": {
"200": {
Expand Down
19 changes: 16 additions & 3 deletions packages/swagger/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ import * as koa from '@midwayjs/koa';
import { join } from 'path';

describe('/test/index.test.ts', () => {

describe('test swagger', () => {

let app;
beforeAll(async () => {
try {
app = await createApp(join(__dirname, 'fixtures/cats'), {}, koa);
app = await createApp(
join(__dirname, 'fixtures/cats'),
{
globalConfig: {
swagger: {
documentOptions: {
operationIdFactory: (c, r) =>
`${c.replace('Controller', '')}_${r.method}`.toLowerCase(),
},
},
},
},
koa
);
} catch (e) {
console.log(e);
console.log('beforeAll: ' + e.stack);
Expand Down
26 changes: 18 additions & 8 deletions site/docs/extensions/swagger.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

相关信息:

| 描述 | |
| ----------------- | ---- |
| 可用于标准项目 | |
| 可用于 Serverless | |
| 可用于一体化 | |
| 包含独立主框架 | |
| 包含独立日志 | |
| 描述 | |
| ----------------- | --- |
| 可用于标准项目 ||
| 可用于 Serverless ||
| 可用于一体化 ||
| 包含独立主框架 ||
| 包含独立日志 ||



Expand Down Expand Up @@ -970,7 +970,17 @@ export interface SwaggerOptions {
useUnsafeMarkdown?: boolean;
tryItOutEnabled?: boolean;
};
}

documentOptions?: {
/**
* 自定义 operationIdFactory,用于生成 operationId
* @default () => controllerKey_webRouter.methodKey
*/
operationIdFactory?: (
controllerKey: string,
webRouter: RouterOption
) => string;
};}
/**
* 继承自 https://swagger.io/specification/#security-scheme-object
*/
Expand Down

0 comments on commit 4db39a6

Please sign in to comment.