@midwayjs/swagger 3.2.0 → 3.3.0

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.
@@ -6,6 +6,7 @@ export declare const DECORATORS: {
6
6
  API_PARAMETERS: string;
7
7
  API_HEADERS: string;
8
8
  API_MODEL_PROPERTIES: string;
9
+ API_EXTRA_MODEL: string;
9
10
  API_MODEL_PROPERTIES_ARRAY: string;
10
11
  API_SECURITY: string;
11
12
  API_EXCLUDE_ENDPOINT: string;
package/dist/constants.js CHANGED
@@ -9,6 +9,7 @@ exports.DECORATORS = {
9
9
  API_PARAMETERS: `${exports.DECORATORS_PREFIX}/apiParameters`,
10
10
  API_HEADERS: `${exports.DECORATORS_PREFIX}/apiHeaders`,
11
11
  API_MODEL_PROPERTIES: `${exports.DECORATORS_PREFIX}/apiModelProperties`,
12
+ API_EXTRA_MODEL: `${exports.DECORATORS_PREFIX}/apiExtraModel`,
12
13
  API_MODEL_PROPERTIES_ARRAY: `${exports.DECORATORS_PREFIX}/apiModelPropertiesArray`,
13
14
  API_SECURITY: `${exports.DECORATORS_PREFIX}/apiSecurity`,
14
15
  API_EXCLUDE_ENDPOINT: `${exports.DECORATORS_PREFIX}/apiExcludeEndpoint`,
@@ -0,0 +1,3 @@
1
+ import { Type } from '../interfaces';
2
+ export declare function ApiExtraModel(models: Type | Type[]): any;
3
+ //# sourceMappingURL=api-extra-model.decorator.d.ts.map
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiExtraModel = void 0;
4
+ const constants_1 = require("../constants");
5
+ const helpers_1 = require("./helpers");
6
+ function ApiExtraModel(models) {
7
+ return (0, helpers_1.createMixedDecorator)(constants_1.DECORATORS.API_EXTRA_MODEL, models);
8
+ }
9
+ exports.ApiExtraModel = ApiExtraModel;
10
+ //# sourceMappingURL=api-extra-model.decorator.js.map
@@ -14,5 +14,6 @@ export * from './api-bearer.decorator';
14
14
  export * from './api-cookie.decorator';
15
15
  export * from './api-oauth2.decorator';
16
16
  export * from './api-security.decorator';
17
+ export * from './api-extra-model.decorator';
17
18
  export { getSchemaPath } from './helpers';
18
19
  //# sourceMappingURL=index.d.ts.map
@@ -34,6 +34,7 @@ __exportStar(require("./api-bearer.decorator"), exports);
34
34
  __exportStar(require("./api-cookie.decorator"), exports);
35
35
  __exportStar(require("./api-oauth2.decorator"), exports);
36
36
  __exportStar(require("./api-security.decorator"), exports);
37
+ __exportStar(require("./api-extra-model.decorator"), exports);
37
38
  var helpers_1 = require("./helpers");
38
39
  Object.defineProperty(exports, "getSchemaPath", { enumerable: true, get: function () { return helpers_1.getSchemaPath; } });
39
40
  //# sourceMappingURL=index.js.map
@@ -17,6 +17,11 @@ export declare class SwaggerExplorer {
17
17
  * @param p
18
18
  */
19
19
  private parseFromParamsToP;
20
+ /**
21
+ * 解析 ApiExtraModel
22
+ * @param clzz
23
+ */
24
+ private parseExtraModel;
20
25
  /**
21
26
  * 解析类型的 ApiProperty
22
27
  * @param clzz
@@ -79,6 +79,7 @@ let SwaggerExplorer = class SwaggerExplorer {
79
79
  return this.documentBuilder.build();
80
80
  }
81
81
  generatePath(target) {
82
+ this.parseExtraModel(target);
82
83
  const metaForMethods = (0, decorator_1.getClassMetadata)(decorator_1.INJECT_CUSTOM_METHOD, target) || [];
83
84
  const exs = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXCLUDE_CONTROLLER);
84
85
  if (exs[0]) {
@@ -455,6 +456,24 @@ let SwaggerExplorer = class SwaggerExplorer {
455
456
  }
456
457
  }
457
458
  }
459
+ /**
460
+ * 解析 ApiExtraModel
461
+ * @param clzz
462
+ */
463
+ parseExtraModel(clzz) {
464
+ const metaForMethods = (0, decorator_1.getClassMetadata)(decorator_1.INJECT_CUSTOM_METHOD, clzz) || [];
465
+ const extraModels = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXTRA_MODEL);
466
+ for (const m of extraModels) {
467
+ if (Array.isArray(m.metadata)) {
468
+ for (const sclz of m.metadata) {
469
+ this.parseClzz(sclz);
470
+ }
471
+ }
472
+ else {
473
+ this.parseClzz(m.metadata);
474
+ }
475
+ }
476
+ }
458
477
  /**
459
478
  * 解析类型的 ApiProperty
460
479
  * @param clzz
@@ -463,6 +482,7 @@ let SwaggerExplorer = class SwaggerExplorer {
463
482
  if (this.documentBuilder.getSchema(clzz.name)) {
464
483
  return;
465
484
  }
485
+ this.parseExtraModel(clzz);
466
486
  const props = (0, decorator_1.getClassMetadata)(decorator_1.INJECT_CUSTOM_PROPERTY, clzz);
467
487
  const tt = {
468
488
  type: 'object',
@@ -479,11 +499,13 @@ let SwaggerExplorer = class SwaggerExplorer {
479
499
  tt.example[key] = metadata === null || metadata === void 0 ? void 0 : metadata.example;
480
500
  delete metadata.example;
481
501
  }
482
- if ((metadata === null || metadata === void 0 ? void 0 : metadata.required) !== false) {
483
- if (!tt.required) {
484
- tt.required = [];
502
+ if (typeof (metadata === null || metadata === void 0 ? void 0 : metadata.required) !== undefined) {
503
+ if (metadata === null || metadata === void 0 ? void 0 : metadata.required) {
504
+ if (!tt.required) {
505
+ tt.required = [];
506
+ }
507
+ tt.required.push(key);
485
508
  }
486
- tt.required.push(key);
487
509
  delete metadata.required;
488
510
  }
489
511
  if (metadata === null || metadata === void 0 ? void 0 : metadata.enum) {
@@ -514,7 +536,7 @@ let SwaggerExplorer = class SwaggerExplorer {
514
536
  if (currentType === 'array') {
515
537
  isArray = true;
516
538
  currentType = (_b = metadata === null || metadata === void 0 ? void 0 : metadata.items) === null || _b === void 0 ? void 0 : _b.type;
517
- delete metadata.items;
539
+ metadata === null || metadata === void 0 ? true : delete metadata.items.type;
518
540
  }
519
541
  if (decorator_1.Types.isClass(currentType)) {
520
542
  this.parseClzz(currentType);
@@ -534,12 +556,22 @@ let SwaggerExplorer = class SwaggerExplorer {
534
556
  }
535
557
  else {
536
558
  if (isArray) {
537
- tt.properties[key] = {
538
- type: 'array',
539
- items: {
540
- type: convertSchemaType((currentType === null || currentType === void 0 ? void 0 : currentType.name) || currentType),
541
- },
542
- };
559
+ // 没有配置类型则认为自己配置了 items 内容
560
+ if (!currentType) {
561
+ tt.properties[key] = {
562
+ type: 'array',
563
+ items: metadata === null || metadata === void 0 ? void 0 : metadata.items,
564
+ };
565
+ delete metadata.items;
566
+ }
567
+ else {
568
+ tt.properties[key] = {
569
+ type: 'array',
570
+ items: {
571
+ type: convertSchemaType((currentType === null || currentType === void 0 ? void 0 : currentType.name) || currentType),
572
+ },
573
+ };
574
+ }
543
575
  }
544
576
  else {
545
577
  tt.properties[key] = {
@@ -42,7 +42,8 @@ let SwaggerMiddleware = class SwaggerMiddleware {
42
42
  let content = (0, fs_1.readFileSync)((0, path_1.join)(this.swaggerUiAssetPath, lastName), {
43
43
  encoding: 'utf-8',
44
44
  });
45
- if (lastName === 'index.html') {
45
+ if (lastName === 'index.html' ||
46
+ lastName === 'swagger-initializer.js') {
46
47
  content = this.replaceInfo(content);
47
48
  }
48
49
  const ext = (0, path_1.extname)(lastName);
@@ -80,7 +81,8 @@ let SwaggerMiddleware = class SwaggerMiddleware {
80
81
  let content = (0, fs_1.readFileSync)((0, path_1.join)(this.swaggerUiAssetPath, lastName), {
81
82
  encoding: 'utf-8',
82
83
  });
83
- if (lastName === 'index.html') {
84
+ if (lastName === 'index.html' ||
85
+ lastName === 'swagger-initializer.js') {
84
86
  content = this.replaceInfo(content);
85
87
  }
86
88
  const ext = (0, path_1.extname)(lastName);
package/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { SwaggerOptions } from './dist/index';
2
2
  export * from './dist/index';
3
3
 
4
- declare module '@midwayjs/core' {
4
+ declare module '@midwayjs/core/dist/interface' {
5
5
  interface MidwayConfig {
6
6
  swagger?: Partial<SwaggerOptions>;
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/swagger",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
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.2.0",
12
+ "@midwayjs/core": "^3.3.0",
13
13
  "@midwayjs/decorator": "^3.1.6",
14
- "@midwayjs/koa": "^3.2.0",
15
- "@midwayjs/mock": "^3.2.0",
14
+ "@midwayjs/koa": "^3.3.0",
15
+ "@midwayjs/mock": "^3.3.0",
16
16
  "swagger-ui-dist": "4.6.2"
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": "cee3e5ddbb4f0cc5f80176a7b1e271e809686a97"
30
+ "gitHead": "b12b41188879296f9863265baa5bbdb4158bddc5"
31
31
  }