@midwayjs/swagger 3.11.15 → 3.11.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@
2
2
  * inspired by https://github.com/metadevpro/openapi3-ts
3
3
  * @see https://github.com/OAI/OpenAPI-Specification/blob/3.0.0-rc0/versions/3.0.md
4
4
  */
5
+ import type { RouterOption } from '@midwayjs/core';
5
6
  export interface OpenAPIObject {
6
7
  openapi: string;
7
8
  info: InfoObject;
@@ -355,5 +356,12 @@ export interface SwaggerOptions {
355
356
  useUnsafeMarkdown?: boolean;
356
357
  tryItOutEnabled?: boolean;
357
358
  };
359
+ documentOptions?: {
360
+ /**
361
+ * 自定义 operationIdFactory,用于生成 operationId
362
+ * @default () => controllerKey_webRouter.methodKey
363
+ */
364
+ operationIdFactory?: (controllerKey: string, webRouter: RouterOption) => string;
365
+ };
358
366
  }
359
367
  //# sourceMappingURL=index.d.ts.map
@@ -1,7 +1,9 @@
1
+ import { RouterOption } from '@midwayjs/core';
1
2
  import { Type } from './interfaces';
2
3
  export declare class SwaggerExplorer {
3
4
  private swaggerConfig;
4
5
  private documentBuilder;
6
+ private operationIdFactory;
5
7
  init(): Promise<void>;
6
8
  addGlobalPrefix(prefix: string): void;
7
9
  scanApp(): void;
@@ -11,6 +13,7 @@ export declare class SwaggerExplorer {
11
13
  * 构造 router 提取方法
12
14
  */
13
15
  private generateRouteMethod;
16
+ getOperationId(controllerKey: string, webRouter: RouterOption): string;
14
17
  private expandSchemaRef;
15
18
  /**
16
19
  * 提取参数
@@ -17,9 +17,10 @@ const _1 = require(".");
17
17
  let SwaggerExplorer = class SwaggerExplorer {
18
18
  constructor() {
19
19
  this.documentBuilder = new documentBuilder_1.DocumentBuilder();
20
+ this.operationIdFactory = (controllerKey, webRouter) => `${controllerKey.toLowerCase()}_${webRouter.method.toLocaleLowerCase()}`;
20
21
  }
21
22
  async init() {
22
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _2, _3, _4, _5;
23
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _2, _3, _4, _5, _6, _7;
23
24
  this.documentBuilder.setTitle(this.swaggerConfig.title);
24
25
  this.documentBuilder.setVersion(this.swaggerConfig.version);
25
26
  this.documentBuilder.setDescription(this.swaggerConfig.description);
@@ -49,14 +50,18 @@ let SwaggerExplorer = class SwaggerExplorer {
49
50
  this.documentBuilder.addTag(t.name, t.description, t.externalDocs);
50
51
  }
51
52
  }
53
+ if ((_4 = (_3 = this.swaggerConfig) === null || _3 === void 0 ? void 0 : _3.documentOptions) === null || _4 === void 0 ? void 0 : _4.operationIdFactory) {
54
+ this.operationIdFactory =
55
+ this.swaggerConfig.documentOptions.operationIdFactory;
56
+ }
52
57
  // 设置 auth 类型
53
- if (Array.isArray((_3 = this.swaggerConfig) === null || _3 === void 0 ? void 0 : _3.auth)) {
54
- for (const a of (_4 = this.swaggerConfig) === null || _4 === void 0 ? void 0 : _4.auth) {
58
+ if (Array.isArray((_5 = this.swaggerConfig) === null || _5 === void 0 ? void 0 : _5.auth)) {
59
+ for (const a of (_6 = this.swaggerConfig) === null || _6 === void 0 ? void 0 : _6.auth) {
55
60
  this.setAuth(a);
56
61
  }
57
62
  }
58
63
  else {
59
- this.setAuth((_5 = this.swaggerConfig) === null || _5 === void 0 ? void 0 : _5.auth);
64
+ this.setAuth((_7 = this.swaggerConfig) === null || _7 === void 0 ? void 0 : _7.auth);
60
65
  }
61
66
  }
62
67
  addGlobalPrefix(prefix) {
@@ -119,10 +124,9 @@ let SwaggerExplorer = class SwaggerExplorer {
119
124
  // const globalMiddleware = controllerOption.routerOptions.middleware;
120
125
  // get router info
121
126
  const webRouterInfo = (0, core_1.getClassMetadata)(core_1.WEB_ROUTER_KEY, target);
122
- let header = null;
123
- const headers = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_HEADERS);
127
+ let headers = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_HEADERS);
124
128
  if (headers.length > 0) {
125
- header = headers[0].metadata;
129
+ headers = headers.map(item => item.metadata);
126
130
  }
127
131
  const security = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_SECURITY);
128
132
  const paths = {};
@@ -146,7 +150,7 @@ let SwaggerExplorer = class SwaggerExplorer {
146
150
  // swagger not support more than one @Body
147
151
  continue;
148
152
  }
149
- this.generateRouteMethod(url, webRouter, paths, metaForMethods, routerArgs, header, target);
153
+ this.generateRouteMethod(url, webRouter, paths, metaForMethods, routerArgs, headers, target);
150
154
  // 这里赋值 tags
151
155
  if (paths[url][webRouter.requestMethod].tags.length === 0) {
152
156
  paths[url][webRouter.requestMethod].tags = strTags;
@@ -177,8 +181,8 @@ let SwaggerExplorer = class SwaggerExplorer {
177
181
  /**
178
182
  * 构造 router 提取方法
179
183
  */
180
- generateRouteMethod(url, webRouter, paths, metaForMethods, routerArgs, header, target) {
181
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
184
+ generateRouteMethod(url, webRouter, paths, metaForMethods, routerArgs, headers, target) {
185
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
182
186
  const operMeta = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_OPERATION &&
183
187
  item.propertyName === webRouter.method)[0];
184
188
  let opts = paths[url];
@@ -189,7 +193,7 @@ let SwaggerExplorer = class SwaggerExplorer {
189
193
  opts[webRouter.requestMethod] = {
190
194
  summary: getNotEmptyValue((_a = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _a === void 0 ? void 0 : _a.summary, webRouter.summary),
191
195
  description: getNotEmptyValue((_b = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _b === void 0 ? void 0 : _b.description, webRouter.description),
192
- // operationId: `${webRouter.requestMethod}_${(operMeta?.metadata?.operationId || webRouter.method)}`,
196
+ operationId: this.getOperationId(target.name, webRouter),
193
197
  tags: ((_c = operMeta === null || operMeta === void 0 ? void 0 : operMeta.metadata) === null || _c === void 0 ? void 0 : _c.tags) || [],
194
198
  };
195
199
  /**
@@ -296,8 +300,8 @@ let SwaggerExplorer = class SwaggerExplorer {
296
300
  }
297
301
  else {
298
302
  // 这里拼 schema properties 时肯定存在
299
- Object.assign(opts[webRouter.requestMethod].requestBody.content[p.contentType]
300
- .schema.properties, p.schema.properties);
303
+ Object.assign({}, (_l = opts[webRouter.requestMethod].requestBody.content[p.contentType]
304
+ .schema) === null || _l === void 0 ? void 0 : _l.properties, p.schema.properties);
301
305
  }
302
306
  delete p.contentType;
303
307
  delete p.content;
@@ -307,8 +311,8 @@ let SwaggerExplorer = class SwaggerExplorer {
307
311
  parameters.push(p);
308
312
  }
309
313
  // class header 需要使用 ApiHeader 装饰器
310
- if (header) {
311
- parameters.unshift(header);
314
+ if (headers) {
315
+ headers.forEach(header => parameters.unshift(header));
312
316
  }
313
317
  opts[webRouter.requestMethod].parameters = parameters;
314
318
  const responses = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_RESPONSE &&
@@ -374,6 +378,9 @@ let SwaggerExplorer = class SwaggerExplorer {
374
378
  }
375
379
  paths[url] = opts;
376
380
  }
381
+ getOperationId(controllerKey, webRouter) {
382
+ return this.operationIdFactory(controllerKey, webRouter);
383
+ }
377
384
  expandSchemaRef(p, name) {
378
385
  let schemaName = name;
379
386
  if (p.schema['$ref']) {
@@ -381,13 +388,15 @@ let SwaggerExplorer = class SwaggerExplorer {
381
388
  schemaName = p.schema['$ref'].replace('#/components/schemas/', '');
382
389
  delete p.schema['$ref'];
383
390
  }
384
- const schema = this.documentBuilder.getSchema(schemaName);
385
- const ss = JSON.parse(JSON.stringify(schema));
386
- if (p.schema.properties) {
387
- Object.assign(p.schema.properties, ss.properties);
388
- }
389
- else {
390
- p.schema = JSON.parse(JSON.stringify(schema));
391
+ if (schemaName) {
392
+ const schema = this.documentBuilder.getSchema(schemaName);
393
+ const ss = JSON.parse(JSON.stringify(schema));
394
+ if (p.schema.properties) {
395
+ Object.assign(p.schema.properties, ss.properties);
396
+ }
397
+ else {
398
+ p.schema = JSON.parse(JSON.stringify(schema));
399
+ }
391
400
  }
392
401
  return p;
393
402
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/swagger",
3
- "version": "3.11.15",
3
+ "version": "3.11.16",
4
4
  "main": "dist/index",
5
5
  "typings": "index.d.ts",
6
6
  "files": [
@@ -27,5 +27,5 @@
27
27
  "type": "git",
28
28
  "url": "https://github.com/midwayjs/midway.git"
29
29
  },
30
- "gitHead": "a4054247f3b9f4fc8ba51684c002606d849e0bd3"
30
+ "gitHead": "ae8b35ec53d10f9c3b19bb74a9f0b235dcf5b030"
31
31
  }