@midwayjs/swagger 3.0.0-beta.14 → 3.0.0-beta.15

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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [3.0.0-beta.15](https://github.com/midwayjs/midway/compare/v3.0.0-beta.14...v3.0.0-beta.15) (2022-01-07)
7
+
8
+
9
+ ### Features
10
+
11
+ * compatible with @File/@Files/@Fields ([#1527](https://github.com/midwayjs/midway/issues/1527)) ([3fe983f](https://github.com/midwayjs/midway/commit/3fe983f2a67c366af370c41df98510adf5dab289))
12
+
13
+
14
+
15
+
16
+
6
17
  # [3.0.0-beta.14](https://github.com/midwayjs/midway/compare/v3.0.0-beta.13...v3.0.0-beta.14) (2022-01-04)
7
18
 
8
19
  **Note:** Version bump only for package @midwayjs/swagger
@@ -12,6 +12,7 @@ export declare class DocumentBuilder {
12
12
  setBasePath(path: string | string[]): this;
13
13
  addPaths(paths: Record<string, PathItemObject>): this;
14
14
  addSchema(schema: Record<string, SchemaObject>): this;
15
+ getSchema(name: string): SchemaObject;
15
16
  addTag(name: string, description?: string, externalDocs?: ExternalDocumentationObject): this;
16
17
  addSecurity(name: string, options: SecuritySchemeObject): this;
17
18
  addSecurityRequirements(name: string | SecurityRequirementObject, requirements?: string[]): this;
@@ -77,6 +77,9 @@ class DocumentBuilder {
77
77
  Object.assign(this.document.components.schemas, schema);
78
78
  return this;
79
79
  }
80
+ getSchema(name) {
81
+ return this.document.components.schemas[name];
82
+ }
80
83
  addTag(name, description = '', externalDocs) {
81
84
  if (Array.isArray(name)) {
82
85
  const arr = name;
@@ -13,12 +13,13 @@ exports.SwaggerExplorer = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
14
  const constants_1 = require("./constants");
15
15
  const documentBuilder_1 = require("./documentBuilder");
16
+ const _1 = require(".");
16
17
  let SwaggerExplorer = class SwaggerExplorer {
17
18
  constructor() {
18
19
  this.documentBuilder = new documentBuilder_1.DocumentBuilder();
19
20
  }
20
21
  async init() {
21
- 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, _1, _2, _3, _4;
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;
22
23
  this.documentBuilder.setTitle(this.swaggerConfig.title);
23
24
  this.documentBuilder.setVersion(this.swaggerConfig.version);
24
25
  this.documentBuilder.setDescription(this.swaggerConfig.description);
@@ -47,18 +48,18 @@ let SwaggerExplorer = class SwaggerExplorer {
47
48
  }
48
49
  }
49
50
  if (((_z = this.swaggerConfig) === null || _z === void 0 ? void 0 : _z.tags) && Array.isArray((_0 = this.swaggerConfig) === null || _0 === void 0 ? void 0 : _0.tags)) {
50
- for (const t of (_1 = this.swaggerConfig) === null || _1 === void 0 ? void 0 : _1.tags) {
51
+ for (const t of (_2 = this.swaggerConfig) === null || _2 === void 0 ? void 0 : _2.tags) {
51
52
  this.documentBuilder.addTag(t.name, t.description, t.externalDocs);
52
53
  }
53
54
  }
54
55
  // 设置 auth 类型
55
- if (Array.isArray((_2 = this.swaggerConfig) === null || _2 === void 0 ? void 0 : _2.auth)) {
56
- for (const a of (_3 = this.swaggerConfig) === null || _3 === void 0 ? void 0 : _3.auth) {
56
+ if (Array.isArray((_3 = this.swaggerConfig) === null || _3 === void 0 ? void 0 : _3.auth)) {
57
+ for (const a of (_4 = this.swaggerConfig) === null || _4 === void 0 ? void 0 : _4.auth) {
57
58
  this.setAuth(a);
58
59
  }
59
60
  }
60
61
  else {
61
- this.setAuth((_4 = this.swaggerConfig) === null || _4 === void 0 ? void 0 : _4.auth);
62
+ this.setAuth((_5 = this.swaggerConfig) === null || _5 === void 0 ? void 0 : _5.auth);
62
63
  }
63
64
  }
64
65
  scanApp() {
@@ -158,7 +159,7 @@ let SwaggerExplorer = class SwaggerExplorer {
158
159
  * 构造 router 提取方法
159
160
  */
160
161
  generateRouteMethod(url, webRouter, paths, metaForMethods, metaForParams, header, target) {
161
- var _a, _b, _c, _d, _e, _f, _g, _h;
162
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
162
163
  const operMeta = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_OPERATION &&
163
164
  item.propertyName === webRouter.method)[0];
164
165
  let opts = paths[url];
@@ -209,18 +210,63 @@ let SwaggerExplorer = class SwaggerExplorer {
209
210
  }
210
211
  this.parseFromParamsToP(params[params.length - 1 - arg.parameterIndex], p);
211
212
  if (p.in === 'body') {
213
+ // 这里兼容一下 @File()、@Files()、@Fields() 装饰器
214
+ if (((_j = arg.metadata) === null || _j === void 0 ? void 0 : _j.type) === decorator_1.RouteParamTypes.FILESSTREAM) {
215
+ p.schema = {
216
+ properties: {
217
+ files: {
218
+ type: 'array',
219
+ items: {
220
+ type: 'string',
221
+ format: 'binary',
222
+ },
223
+ description: p.description,
224
+ },
225
+ },
226
+ };
227
+ p.contentType = _1.BodyContentType.Multipart;
228
+ }
229
+ if (((_k = arg.metadata) === null || _k === void 0 ? void 0 : _k.type) === decorator_1.RouteParamTypes.FILESTREAM) {
230
+ p.schema = {
231
+ properties: {
232
+ file: {
233
+ type: 'string',
234
+ format: 'binary',
235
+ description: p.description,
236
+ },
237
+ },
238
+ };
239
+ p.contentType = _1.BodyContentType.Multipart;
240
+ }
241
+ if (((_l = arg.metadata) === null || _l === void 0 ? void 0 : _l.type) === decorator_1.RouteParamTypes.FIELDS) {
242
+ if (p.schema['$ref']) {
243
+ // 展开各个字段属性
244
+ const name = p.schema['$ref'].replace('#/components/schemas/', '');
245
+ const schema = this.documentBuilder.getSchema(name);
246
+ delete p.schema['$ref'];
247
+ p.schema = JSON.parse(JSON.stringify(schema));
248
+ }
249
+ p.contentType = _1.BodyContentType.Multipart;
250
+ }
212
251
  if (!p.content) {
213
252
  p.content = {};
214
253
  p.content[p.contentType || 'application/json'] = {
215
254
  schema: p.schema,
216
255
  };
217
256
  }
218
- const requestBody = {
219
- required: true,
220
- description: p.description || p.name,
221
- content: p.content,
222
- };
223
- opts[webRouter.requestMethod].requestBody = requestBody;
257
+ if (!opts[webRouter.requestMethod].requestBody) {
258
+ const requestBody = {
259
+ required: true,
260
+ description: p.description || p.name,
261
+ content: p.content,
262
+ };
263
+ opts[webRouter.requestMethod].requestBody = requestBody;
264
+ }
265
+ else {
266
+ // 这里拼 schema properties 时肯定存在
267
+ Object.assign(opts[webRouter.requestMethod].requestBody.content[p.contentType]
268
+ .schema.properties, p.schema.properties);
269
+ }
224
270
  delete p.contentType;
225
271
  delete p.content;
226
272
  // in body 不需要处理
@@ -579,6 +625,9 @@ function convertTypeToString(type) {
579
625
  case decorator_1.RouteParamTypes.PARAM:
580
626
  return 'path';
581
627
  case decorator_1.RouteParamTypes.BODY:
628
+ case decorator_1.RouteParamTypes.FIELDS:
629
+ case decorator_1.RouteParamTypes.FILESSTREAM:
630
+ case decorator_1.RouteParamTypes.FILESTREAM:
582
631
  return 'body';
583
632
  default:
584
633
  return 'header';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/swagger",
3
- "version": "3.0.0-beta.14",
3
+ "version": "3.0.0-beta.15",
4
4
  "main": "dist/index",
5
5
  "typings": "index.d.ts",
6
6
  "files": [
@@ -9,10 +9,10 @@
9
9
  "index.d.ts"
10
10
  ],
11
11
  "devDependencies": {
12
- "@midwayjs/core": "^3.0.0-beta.14",
13
- "@midwayjs/decorator": "^3.0.0-beta.14",
14
- "@midwayjs/koa": "^3.0.0-beta.14",
15
- "@midwayjs/mock": "^3.0.0-beta.14",
12
+ "@midwayjs/core": "^3.0.0-beta.15",
13
+ "@midwayjs/decorator": "^3.0.0-beta.15",
14
+ "@midwayjs/koa": "^3.0.0-beta.15",
15
+ "@midwayjs/mock": "^3.0.0-beta.15",
16
16
  "swagger-ui-dist": "4.1.3"
17
17
  },
18
18
  "author": "Kurten Chan <chinkurten@gmail.com>",
@@ -27,5 +27,5 @@
27
27
  "type": "git",
28
28
  "url": "https://github.com/midwayjs/midway.git"
29
29
  },
30
- "gitHead": "95695ada36190cf376e93a1a8866b3fb6dde06bc"
30
+ "gitHead": "6b9778557289d9e8b562c7ce6127736db1248973"
31
31
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2013 - Now midwayjs
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.