@midwayjs/swagger 3.19.2 → 4.0.0-alpha.1
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/dist/common/enum.utils.js +1 -1
- package/dist/common/httpStatus.js +1 -1
- package/dist/configuration.js +2 -2
- package/dist/decorators/api-body.decorator.js +1 -1
- package/dist/decorators/api-exclude-controller.decorator.js +1 -1
- package/dist/decorators/helpers.js +3 -3
- package/dist/documentBuilder.js +3 -4
- package/dist/swaggerExplorer.js +95 -102
- package/dist/swaggerMiddleware.js +2 -2
- package/dist/ui/render.js +2 -2
- package/package.json +9 -8
|
@@ -49,6 +49,6 @@ const isEnumArray = (obj) => obj.isArray && obj.enum;
|
|
|
49
49
|
exports.isEnumArray = isEnumArray;
|
|
50
50
|
const isEnumDefined = (obj) => obj.enum;
|
|
51
51
|
exports.isEnumDefined = isEnumDefined;
|
|
52
|
-
const isEnumMetadata = (metadata) =>
|
|
52
|
+
const isEnumMetadata = (metadata) => metadata.enum || (metadata.isArray && metadata.items?.['enum']);
|
|
53
53
|
exports.isEnumMetadata = isEnumMetadata;
|
|
54
54
|
//# sourceMappingURL=enum.utils.js.map
|
|
@@ -51,5 +51,5 @@ var HttpStatus;
|
|
|
51
51
|
HttpStatus[HttpStatus["SERVICE_UNAVAILABLE"] = 503] = "SERVICE_UNAVAILABLE";
|
|
52
52
|
HttpStatus[HttpStatus["GATEWAY_TIMEOUT"] = 504] = "GATEWAY_TIMEOUT";
|
|
53
53
|
HttpStatus[HttpStatus["HTTP_VERSION_NOT_SUPPORTED"] = 505] = "HTTP_VERSION_NOT_SUPPORTED";
|
|
54
|
-
})(HttpStatus
|
|
54
|
+
})(HttpStatus || (exports.HttpStatus = HttpStatus = {}));
|
|
55
55
|
//# sourceMappingURL=httpStatus.js.map
|
package/dist/configuration.js
CHANGED
|
@@ -41,6 +41,7 @@ let SwaggerConfiguration = class SwaggerConfiguration {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
};
|
|
44
|
+
exports.SwaggerConfiguration = SwaggerConfiguration;
|
|
44
45
|
__decorate([
|
|
45
46
|
(0, core_1.Inject)(),
|
|
46
47
|
__metadata("design:type", core_1.MidwayApplicationManager)
|
|
@@ -49,7 +50,7 @@ __decorate([
|
|
|
49
50
|
(0, core_1.Inject)(),
|
|
50
51
|
__metadata("design:type", core_1.MidwayConfigService)
|
|
51
52
|
], SwaggerConfiguration.prototype, "configService", void 0);
|
|
52
|
-
SwaggerConfiguration = __decorate([
|
|
53
|
+
exports.SwaggerConfiguration = SwaggerConfiguration = __decorate([
|
|
53
54
|
(0, core_1.Configuration)({
|
|
54
55
|
importConfigs: [
|
|
55
56
|
{
|
|
@@ -59,5 +60,4 @@ SwaggerConfiguration = __decorate([
|
|
|
59
60
|
namespace: 'swagger',
|
|
60
61
|
})
|
|
61
62
|
], SwaggerConfiguration);
|
|
62
|
-
exports.SwaggerConfiguration = SwaggerConfiguration;
|
|
63
63
|
//# sourceMappingURL=configuration.js.map
|
|
@@ -10,7 +10,7 @@ var BodyContentType;
|
|
|
10
10
|
BodyContentType["Multipart"] = "multipart/form-data";
|
|
11
11
|
BodyContentType["MultipartMixed"] = "multipart/mixed";
|
|
12
12
|
BodyContentType["OctetStream"] = "application/octet-stream";
|
|
13
|
-
})(BodyContentType
|
|
13
|
+
})(BodyContentType || (exports.BodyContentType = BodyContentType = {}));
|
|
14
14
|
const defaultBodyMetadata = {
|
|
15
15
|
type: String,
|
|
16
16
|
required: true,
|
|
@@ -5,7 +5,7 @@ const core_1 = require("@midwayjs/core");
|
|
|
5
5
|
const constants_1 = require("../constants");
|
|
6
6
|
function ApiExcludeController(disable = true) {
|
|
7
7
|
return (target) => {
|
|
8
|
-
|
|
8
|
+
core_1.MetadataManager.defineMetadata(constants_1.DECORATORS.API_EXCLUDE_CONTROLLER, { disable }, target);
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
11
|
exports.ApiExcludeController = ApiExcludeController;
|
|
@@ -4,20 +4,20 @@ exports.getSchemaPath = exports.getTypeIsArrayTuple = exports.createParamDecorat
|
|
|
4
4
|
const core_1 = require("@midwayjs/core");
|
|
5
5
|
const constants_1 = require("../constants");
|
|
6
6
|
function createPropertyDecorator(metakey, metadata) {
|
|
7
|
-
return
|
|
7
|
+
return core_1.DecoratorManager.createCustomPropertyDecorator(metakey, metadata, false);
|
|
8
8
|
}
|
|
9
9
|
exports.createPropertyDecorator = createPropertyDecorator;
|
|
10
10
|
function createMixedDecorator(metakey, metadata) {
|
|
11
11
|
return (target, methodName) => {
|
|
12
12
|
if (methodName) {
|
|
13
|
-
|
|
13
|
+
core_1.MetadataManager.attachMetadata(constants_1.DECORATORS_METHOD_METADATA, {
|
|
14
14
|
key: metakey,
|
|
15
15
|
propertyName: methodName,
|
|
16
16
|
metadata,
|
|
17
17
|
}, target, methodName);
|
|
18
18
|
}
|
|
19
19
|
else {
|
|
20
|
-
|
|
20
|
+
core_1.MetadataManager.attachMetadata(constants_1.DECORATORS_CLASS_METADATA, {
|
|
21
21
|
key: metakey,
|
|
22
22
|
metadata,
|
|
23
23
|
}, target);
|
package/dist/documentBuilder.js
CHANGED
|
@@ -68,9 +68,8 @@ class DocumentBuilder {
|
|
|
68
68
|
return this;
|
|
69
69
|
}
|
|
70
70
|
getSchema(name) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return (_b = this.document.components) === null || _b === void 0 ? void 0 : _b.schemas[name];
|
|
71
|
+
if (this.document.components?.schemas) {
|
|
72
|
+
return this.document.components?.schemas[name];
|
|
74
73
|
}
|
|
75
74
|
return undefined;
|
|
76
75
|
}
|
|
@@ -151,7 +150,7 @@ class DocumentBuilder {
|
|
|
151
150
|
this.addSecurity(name, {
|
|
152
151
|
type: 'oauth2',
|
|
153
152
|
flows: {
|
|
154
|
-
...options
|
|
153
|
+
...options?.flows,
|
|
155
154
|
},
|
|
156
155
|
});
|
|
157
156
|
return this;
|
package/dist/swaggerExplorer.js
CHANGED
|
@@ -22,48 +22,47 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
22
22
|
this.operationIdFactory = (controllerKey, webRouter) => `${controllerKey.toLowerCase()}_${webRouter.method.toLocaleLowerCase()}`;
|
|
23
23
|
}
|
|
24
24
|
async init() {
|
|
25
|
-
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;
|
|
26
25
|
this.documentBuilder.setTitle(this.swaggerConfig.title);
|
|
27
26
|
this.documentBuilder.setVersion(this.swaggerConfig.version);
|
|
28
27
|
this.documentBuilder.setDescription(this.swaggerConfig.description);
|
|
29
|
-
if (
|
|
30
|
-
typeof
|
|
31
|
-
this.documentBuilder.setContact(
|
|
28
|
+
if (this.swaggerConfig?.contact &&
|
|
29
|
+
typeof this.swaggerConfig?.contact === 'object') {
|
|
30
|
+
this.documentBuilder.setContact(this.swaggerConfig?.contact?.name, this.swaggerConfig?.contact?.url, this.swaggerConfig?.contact?.email);
|
|
32
31
|
}
|
|
33
|
-
if (
|
|
34
|
-
typeof
|
|
35
|
-
this.documentBuilder.setLicense(
|
|
32
|
+
if (this.swaggerConfig?.license &&
|
|
33
|
+
typeof this.swaggerConfig?.license === 'object') {
|
|
34
|
+
this.documentBuilder.setLicense(this.swaggerConfig?.license?.name, this.swaggerConfig?.license?.url);
|
|
36
35
|
}
|
|
37
36
|
if (this.swaggerConfig.termsOfService) {
|
|
38
37
|
this.documentBuilder.setTermsOfService(this.swaggerConfig.termsOfService);
|
|
39
38
|
}
|
|
40
|
-
if (
|
|
41
|
-
typeof
|
|
42
|
-
this.documentBuilder.setExternalDoc(
|
|
39
|
+
if (this.swaggerConfig?.externalDocs &&
|
|
40
|
+
typeof this.swaggerConfig?.externalDocs === 'object') {
|
|
41
|
+
this.documentBuilder.setExternalDoc(this.swaggerConfig?.externalDocs?.description, this.swaggerConfig?.externalDocs?.url);
|
|
43
42
|
}
|
|
44
|
-
if (
|
|
45
|
-
Array.isArray(
|
|
46
|
-
for (const serv of
|
|
47
|
-
this.documentBuilder.addServer(serv
|
|
43
|
+
if (this.swaggerConfig?.servers &&
|
|
44
|
+
Array.isArray(this.swaggerConfig?.servers)) {
|
|
45
|
+
for (const serv of this.swaggerConfig?.servers) {
|
|
46
|
+
this.documentBuilder.addServer(serv?.url, serv?.description, serv?.variables);
|
|
48
47
|
}
|
|
49
48
|
}
|
|
50
|
-
if (
|
|
51
|
-
for (const t of
|
|
49
|
+
if (this.swaggerConfig?.tags && Array.isArray(this.swaggerConfig?.tags)) {
|
|
50
|
+
for (const t of this.swaggerConfig?.tags) {
|
|
52
51
|
this.documentBuilder.addTag(t.name, t.description, t.externalDocs);
|
|
53
52
|
}
|
|
54
53
|
}
|
|
55
|
-
if (
|
|
54
|
+
if (this.swaggerConfig?.documentOptions?.operationIdFactory) {
|
|
56
55
|
this.operationIdFactory =
|
|
57
56
|
this.swaggerConfig.documentOptions.operationIdFactory;
|
|
58
57
|
}
|
|
59
58
|
// 设置 auth 类型
|
|
60
|
-
if (Array.isArray(
|
|
61
|
-
for (const a of
|
|
59
|
+
if (Array.isArray(this.swaggerConfig?.auth)) {
|
|
60
|
+
for (const a of this.swaggerConfig?.auth) {
|
|
62
61
|
this.setAuth(a);
|
|
63
62
|
}
|
|
64
63
|
}
|
|
65
64
|
else {
|
|
66
|
-
this.setAuth(
|
|
65
|
+
this.setAuth(this.swaggerConfig?.auth);
|
|
67
66
|
}
|
|
68
67
|
}
|
|
69
68
|
addGlobalPrefix(globalPrefix) {
|
|
@@ -85,12 +84,11 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
85
84
|
this.documentBuilder.setPaths(newPaths);
|
|
86
85
|
}
|
|
87
86
|
scanApp() {
|
|
88
|
-
|
|
89
|
-
const routes = (0, core_1.listModule)(core_1.CONTROLLER_KEY);
|
|
87
|
+
const routes = core_1.DecoratorManager.listModule(core_1.CONTROLLER_KEY);
|
|
90
88
|
for (const route of routes) {
|
|
91
89
|
this.generatePath(route);
|
|
92
90
|
}
|
|
93
|
-
if (
|
|
91
|
+
if (this.swaggerConfig?.tagSortable) {
|
|
94
92
|
this.documentBuilder.sortTags();
|
|
95
93
|
}
|
|
96
94
|
}
|
|
@@ -101,21 +99,20 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
101
99
|
return this.documentBuilder;
|
|
102
100
|
}
|
|
103
101
|
generatePath(target) {
|
|
104
|
-
var _a;
|
|
105
102
|
// 获取控制器元数据
|
|
106
|
-
const excludeClassMeta =
|
|
103
|
+
const excludeClassMeta = core_1.MetadataManager.getOwnMetadata(constants_1.DECORATORS.API_EXCLUDE_CONTROLLER, target);
|
|
107
104
|
if (excludeClassMeta && excludeClassMeta.disable) {
|
|
108
105
|
// 如果存在需要排除的控制器,则直接返回
|
|
109
106
|
return;
|
|
110
107
|
}
|
|
111
|
-
const isGenerateTagForController =
|
|
108
|
+
const isGenerateTagForController = this.swaggerConfig.isGenerateTagForController ?? true;
|
|
112
109
|
// 解析额外的模型
|
|
113
110
|
this.parseExtraModel(target);
|
|
114
|
-
const metaForClass =
|
|
111
|
+
const metaForClass = core_1.MetadataManager.getOwnMetadata(constants_1.DECORATORS_CLASS_METADATA, target) || [];
|
|
115
112
|
// 获取参数的元数据
|
|
116
|
-
const metaForParams =
|
|
113
|
+
const metaForParams = core_1.MetadataManager.getPropertiesWithMetadata(core_1.CUSTOM_PARAM_INJECT_KEY, target) || {};
|
|
117
114
|
// 获取控制器选项
|
|
118
|
-
const controllerOption =
|
|
115
|
+
const controllerOption = core_1.MetadataManager.getOwnMetadata(core_1.CONTROLLER_KEY, target);
|
|
119
116
|
// 获取前缀
|
|
120
117
|
const prefix = controllerOption.prefix;
|
|
121
118
|
// 过滤出标签
|
|
@@ -135,15 +132,15 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
135
132
|
const tag = { name: '', description: '' };
|
|
136
133
|
if (prefix !== '/') {
|
|
137
134
|
tag.name =
|
|
138
|
-
|
|
135
|
+
controllerOption?.routerOptions.tagName ||
|
|
139
136
|
(/^\//.test(prefix) ? prefix.split('/')[1] : prefix);
|
|
140
137
|
tag.description =
|
|
141
|
-
|
|
138
|
+
controllerOption?.routerOptions.description || tag.name;
|
|
142
139
|
}
|
|
143
140
|
else {
|
|
144
|
-
tag.name = controllerOption
|
|
141
|
+
tag.name = controllerOption?.routerOptions.tagName;
|
|
145
142
|
tag.description =
|
|
146
|
-
|
|
143
|
+
controllerOption?.routerOptions.description || tag.name;
|
|
147
144
|
}
|
|
148
145
|
// 如果标签名存在,则将其添加到文档构建器中
|
|
149
146
|
if (tag.name) {
|
|
@@ -156,7 +153,7 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
156
153
|
}
|
|
157
154
|
}
|
|
158
155
|
// 获取路由信息
|
|
159
|
-
const webRouterInfo =
|
|
156
|
+
const webRouterInfo = core_1.MetadataManager.getMetadata(core_1.WEB_ROUTER_KEY, target);
|
|
160
157
|
// 过滤出头部信息
|
|
161
158
|
let headers = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_HEADERS);
|
|
162
159
|
if (headers.length > 0) {
|
|
@@ -173,7 +170,7 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
173
170
|
let url = (prefix + webRouter.path).replace('//', '/');
|
|
174
171
|
url = replaceUrl(url, parseParamsInPath(url));
|
|
175
172
|
// 方法元数据
|
|
176
|
-
const metaForMethods =
|
|
173
|
+
const metaForMethods = core_1.MetadataManager.getMetadata(constants_1.DECORATORS_METHOD_METADATA, target, webRouter.method) || [];
|
|
177
174
|
// 判断是否忽略当前路由
|
|
178
175
|
const endpoints = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXCLUDE_ENDPOINT &&
|
|
179
176
|
item.propertyName === webRouter.method);
|
|
@@ -191,11 +188,8 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
191
188
|
// 获取路由参数
|
|
192
189
|
const routerArgs = metaForParams[webRouter.method] || [];
|
|
193
190
|
// 过滤出主体参数
|
|
194
|
-
const bds = routerArgs.filter(item =>
|
|
195
|
-
|
|
196
|
-
return item.key === core_1.WEB_ROUTER_PARAM_KEY &&
|
|
197
|
-
((_a = item === null || item === void 0 ? void 0 : item.metadata) === null || _a === void 0 ? void 0 : _a.type) === core_1.RouteParamTypes.BODY;
|
|
198
|
-
});
|
|
191
|
+
const bds = routerArgs.filter(item => item.key === core_1.WEB_ROUTER_PARAM_KEY &&
|
|
192
|
+
item?.metadata?.type === core_1.RouteParamTypes.BODY);
|
|
199
193
|
// 如果存在多个主体参数,则跳过当前循环,因为swagger不支持多个@Body
|
|
200
194
|
if (bds.length > 1) {
|
|
201
195
|
continue;
|
|
@@ -256,7 +250,6 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
256
250
|
* 构造 router 提取方法
|
|
257
251
|
*/
|
|
258
252
|
generateRouteMethod(url, webRouter, paths, metaForMethods, routerArgs, headers, target) {
|
|
259
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
|
|
260
253
|
const operMeta = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_OPERATION &&
|
|
261
254
|
item.propertyName === webRouter.method)[0];
|
|
262
255
|
const routerTagsMeta = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_TAGS &&
|
|
@@ -268,27 +261,24 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
268
261
|
}
|
|
269
262
|
const parameters = [];
|
|
270
263
|
opts[webRouter.requestMethod] = {
|
|
271
|
-
summary: getNotEmptyValue(
|
|
272
|
-
description: getNotEmptyValue(
|
|
273
|
-
operationId:
|
|
264
|
+
summary: getNotEmptyValue(operMeta?.metadata?.summary, webRouter.summary),
|
|
265
|
+
description: getNotEmptyValue(operMeta?.metadata?.description, webRouter.description),
|
|
266
|
+
operationId: operMeta?.metadata?.operationId ||
|
|
274
267
|
this.getOperationId(target.name, webRouter),
|
|
275
|
-
tags: routerTags.length ? routerTags :
|
|
268
|
+
tags: routerTags.length ? routerTags : operMeta?.metadata?.tags ?? [],
|
|
276
269
|
};
|
|
277
|
-
if (
|
|
270
|
+
if (operMeta?.metadata?.deprecated != null) {
|
|
278
271
|
opts[webRouter.requestMethod].deprecated =
|
|
279
|
-
!!
|
|
272
|
+
!!operMeta?.metadata?.deprecated;
|
|
280
273
|
}
|
|
281
274
|
/**
|
|
282
275
|
* [{"key":"web:router_param","parameterIndex":1,"propertyName":"create","metadata":{"type":2}},
|
|
283
276
|
* {"key":"web:router_param","parameterIndex":0,"propertyName":"create","metadata":{"type":1,"propertyData":"createCatDto"}}]
|
|
284
277
|
*/
|
|
285
278
|
// WEB_ROUTER_PARAM_KEY
|
|
286
|
-
const args = routerArgs.filter(item =>
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
((_a = item === null || item === void 0 ? void 0 : item.metadata) === null || _a === void 0 ? void 0 : _a.type) !== core_1.RouteParamTypes.CUSTOM;
|
|
290
|
-
});
|
|
291
|
-
const types = (0, core_1.getMethodParamTypes)(target, webRouter.method);
|
|
279
|
+
const args = routerArgs.filter(item => item.key === core_1.WEB_ROUTER_PARAM_KEY &&
|
|
280
|
+
item?.metadata?.type !== core_1.RouteParamTypes.CUSTOM);
|
|
281
|
+
const types = core_1.MetadataManager.getMethodParamTypes(target, webRouter.method);
|
|
292
282
|
const params = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_PARAMETERS &&
|
|
293
283
|
item.propertyName === webRouter.method);
|
|
294
284
|
// set params information from @ApiQuery() to parameters
|
|
@@ -324,9 +314,9 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
324
314
|
parameters.push(p);
|
|
325
315
|
}
|
|
326
316
|
else if (p.in === 'body') {
|
|
327
|
-
p.content =
|
|
317
|
+
p.content = p.content ?? {};
|
|
328
318
|
if (Object.keys(p.content).length === 0) {
|
|
329
|
-
p.content[p.contentType || 'application/json'] =
|
|
319
|
+
p.content[p.contentType || 'application/json'] = p.content[p.contentType || 'application/json'] ?? {
|
|
330
320
|
schema: p.schema,
|
|
331
321
|
};
|
|
332
322
|
}
|
|
@@ -336,25 +326,25 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
336
326
|
}
|
|
337
327
|
// if requestBody is already set, skip
|
|
338
328
|
opts[webRouter.requestMethod].requestBody =
|
|
339
|
-
|
|
329
|
+
opts[webRouter.requestMethod].requestBody ?? {};
|
|
340
330
|
opts[webRouter.requestMethod].requestBody.description =
|
|
341
|
-
|
|
331
|
+
opts[webRouter.requestMethod].requestBody.description ??
|
|
332
|
+
p.description;
|
|
342
333
|
opts[webRouter.requestMethod].requestBody.content =
|
|
343
|
-
|
|
334
|
+
opts[webRouter.requestMethod].requestBody.content ?? p.content;
|
|
344
335
|
opts[webRouter.requestMethod].requestBody.required =
|
|
345
|
-
|
|
336
|
+
opts[webRouter.requestMethod].requestBody.required ?? p.required;
|
|
346
337
|
}
|
|
347
338
|
}
|
|
348
339
|
for (const arg of args) {
|
|
349
340
|
const currentType = types[arg.parameterIndex];
|
|
350
341
|
const p = {
|
|
351
|
-
name:
|
|
352
|
-
in: convertTypeToString(
|
|
342
|
+
name: arg?.metadata?.propertyData,
|
|
343
|
+
in: convertTypeToString(arg.metadata?.type),
|
|
353
344
|
required: false,
|
|
354
345
|
};
|
|
355
346
|
const existsParam = parameters.find(item => {
|
|
356
|
-
|
|
357
|
-
return item.name === ((_a = arg === null || arg === void 0 ? void 0 : arg.metadata) === null || _a === void 0 ? void 0 : _a.propertyData) && item.in === p.in;
|
|
347
|
+
return item.name === arg?.metadata?.propertyData && item.in === p.in;
|
|
358
348
|
});
|
|
359
349
|
// if exists same param from @ApiQuery and other decorator, just skip
|
|
360
350
|
if (existsParam) {
|
|
@@ -374,13 +364,13 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
374
364
|
// 如果@Query()装饰的 是一个对象,则把该对象的子属性作为多个@Query参数
|
|
375
365
|
const schema = this.documentBuilder.getSchema(currentType.name);
|
|
376
366
|
Object.keys(schema.properties).forEach(pName => {
|
|
377
|
-
var _a, _b;
|
|
378
367
|
const pp = {
|
|
379
368
|
name: pName,
|
|
380
369
|
in: p.in,
|
|
381
|
-
description:
|
|
370
|
+
description: schema.properties[pName]
|
|
371
|
+
?.description,
|
|
382
372
|
schema: schema.properties[pName],
|
|
383
|
-
required:
|
|
373
|
+
required: schema.required?.includes(pName) || false,
|
|
384
374
|
};
|
|
385
375
|
parameters.push(pp);
|
|
386
376
|
});
|
|
@@ -391,7 +381,7 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
391
381
|
continue;
|
|
392
382
|
}
|
|
393
383
|
p.schema = {
|
|
394
|
-
type: convertSchemaType(
|
|
384
|
+
type: convertSchemaType(currentType?.name ?? currentType),
|
|
395
385
|
};
|
|
396
386
|
}
|
|
397
387
|
}
|
|
@@ -404,7 +394,7 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
404
394
|
continue;
|
|
405
395
|
}
|
|
406
396
|
// 这里兼容一下 @File()、@Files()、@Fields() 装饰器
|
|
407
|
-
if (
|
|
397
|
+
if (arg.metadata?.type === core_1.RouteParamTypes.FILESSTREAM) {
|
|
408
398
|
p.content = {};
|
|
409
399
|
p.content[_1.BodyContentType.Multipart] = {
|
|
410
400
|
schema: {
|
|
@@ -422,7 +412,7 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
422
412
|
},
|
|
423
413
|
};
|
|
424
414
|
}
|
|
425
|
-
else if (
|
|
415
|
+
else if (arg.metadata?.type === core_1.RouteParamTypes.FILESTREAM) {
|
|
426
416
|
p.content = {};
|
|
427
417
|
p.content[_1.BodyContentType.Multipart] = {
|
|
428
418
|
schema: {
|
|
@@ -452,7 +442,7 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
452
442
|
p.content = {
|
|
453
443
|
'text/plain': {
|
|
454
444
|
schema: {
|
|
455
|
-
type: convertSchemaType(
|
|
445
|
+
type: convertSchemaType(currentType?.name ?? currentType),
|
|
456
446
|
},
|
|
457
447
|
},
|
|
458
448
|
};
|
|
@@ -558,7 +548,7 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
558
548
|
* @param clzz
|
|
559
549
|
*/
|
|
560
550
|
parseExtraModel(clzz) {
|
|
561
|
-
const metaForClass =
|
|
551
|
+
const metaForClass = core_1.MetadataManager.getOwnMetadata(constants_1.DECORATORS_CLASS_METADATA, clzz) || [];
|
|
562
552
|
const extraModels = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_EXTRA_MODEL);
|
|
563
553
|
for (const m of extraModels) {
|
|
564
554
|
if (Array.isArray(m.metadata)) {
|
|
@@ -572,46 +562,45 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
572
562
|
}
|
|
573
563
|
}
|
|
574
564
|
parseSubPropertyType(metadata) {
|
|
575
|
-
var _a, _b, _c;
|
|
576
565
|
let typeMeta;
|
|
577
|
-
if (metadata
|
|
566
|
+
if (metadata?.enum) {
|
|
578
567
|
typeMeta = {
|
|
579
|
-
type: metadata
|
|
580
|
-
enum: metadata
|
|
581
|
-
default: metadata
|
|
568
|
+
type: metadata?.type,
|
|
569
|
+
enum: metadata?.enum,
|
|
570
|
+
default: metadata?.default,
|
|
582
571
|
};
|
|
583
|
-
if (metadata
|
|
584
|
-
typeMeta.description = metadata
|
|
572
|
+
if (metadata?.description) {
|
|
573
|
+
typeMeta.description = metadata?.description;
|
|
585
574
|
}
|
|
586
575
|
return typeMeta;
|
|
587
576
|
}
|
|
588
|
-
if (
|
|
577
|
+
if (metadata?.items?.enum) {
|
|
589
578
|
typeMeta = {
|
|
590
|
-
type: metadata
|
|
591
|
-
items: metadata
|
|
592
|
-
default: metadata
|
|
579
|
+
type: metadata?.type,
|
|
580
|
+
items: metadata?.items,
|
|
581
|
+
default: metadata?.default,
|
|
593
582
|
};
|
|
594
|
-
if (metadata
|
|
595
|
-
typeMeta.description = metadata
|
|
583
|
+
if (metadata?.description) {
|
|
584
|
+
typeMeta.description = metadata?.description;
|
|
596
585
|
}
|
|
597
586
|
return typeMeta;
|
|
598
587
|
}
|
|
599
588
|
let isArray = false;
|
|
600
|
-
let currentType = parseTypeSchema(metadata
|
|
601
|
-
|
|
589
|
+
let currentType = parseTypeSchema(metadata?.type);
|
|
590
|
+
delete metadata?.type;
|
|
602
591
|
if (currentType === 'array') {
|
|
603
592
|
isArray = true;
|
|
604
|
-
currentType = parseTypeSchema(
|
|
605
|
-
|
|
593
|
+
currentType = parseTypeSchema(metadata?.items?.type);
|
|
594
|
+
delete metadata?.items.type;
|
|
606
595
|
}
|
|
607
|
-
if (metadata
|
|
596
|
+
if (metadata?.oneOf) {
|
|
608
597
|
typeMeta = {
|
|
609
598
|
oneOf: [],
|
|
610
599
|
};
|
|
611
|
-
metadata
|
|
600
|
+
metadata?.oneOf.forEach((item) => {
|
|
612
601
|
typeMeta.push(this.parseSubPropertyType(item));
|
|
613
602
|
});
|
|
614
|
-
|
|
603
|
+
delete metadata?.oneOf;
|
|
615
604
|
}
|
|
616
605
|
if (core_1.Types.isClass(currentType)) {
|
|
617
606
|
this.parseClzz(currentType);
|
|
@@ -619,13 +608,13 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
619
608
|
typeMeta = {
|
|
620
609
|
type: 'array',
|
|
621
610
|
items: {
|
|
622
|
-
$ref: '#/components/schemas/' +
|
|
611
|
+
$ref: '#/components/schemas/' + currentType?.name,
|
|
623
612
|
},
|
|
624
613
|
};
|
|
625
614
|
}
|
|
626
615
|
else {
|
|
627
616
|
typeMeta = {
|
|
628
|
-
$ref: '#/components/schemas/' +
|
|
617
|
+
$ref: '#/components/schemas/' + currentType?.name,
|
|
629
618
|
};
|
|
630
619
|
}
|
|
631
620
|
delete metadata.items;
|
|
@@ -634,19 +623,19 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
634
623
|
if (isArray) {
|
|
635
624
|
// 没有配置类型则认为自己配置了 items 内容
|
|
636
625
|
if (!currentType) {
|
|
637
|
-
if (
|
|
626
|
+
if (metadata?.items?.['$ref']) {
|
|
638
627
|
metadata.items['$ref'] = parseTypeSchema(metadata.items['$ref']);
|
|
639
628
|
}
|
|
640
629
|
typeMeta = {
|
|
641
630
|
type: 'array',
|
|
642
|
-
items: metadata
|
|
631
|
+
items: metadata?.items,
|
|
643
632
|
};
|
|
644
633
|
}
|
|
645
634
|
else {
|
|
646
635
|
typeMeta = {
|
|
647
636
|
type: 'array',
|
|
648
637
|
items: {
|
|
649
|
-
type: convertSchemaType(
|
|
638
|
+
type: convertSchemaType(currentType?.name || currentType),
|
|
650
639
|
},
|
|
651
640
|
};
|
|
652
641
|
}
|
|
@@ -655,7 +644,7 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
655
644
|
else {
|
|
656
645
|
typeMeta = {
|
|
657
646
|
type: currentType,
|
|
658
|
-
format: metadata
|
|
647
|
+
format: metadata?.format,
|
|
659
648
|
};
|
|
660
649
|
// Date 类型支持
|
|
661
650
|
if (typeMeta.type === 'Date') {
|
|
@@ -773,7 +762,11 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
773
762
|
this.parseExtraModel(clzz);
|
|
774
763
|
// 解析类上的 ApiProperty
|
|
775
764
|
// TODO 这里后面不能用这个方法
|
|
776
|
-
const props =
|
|
765
|
+
const props = core_1.MetadataManager.getPropertiesWithMetadata(core_1.CUSTOM_PROPERTY_INJECT_KEY, clzz) || {};
|
|
766
|
+
// 这里属性值唯一,取数组最后一个
|
|
767
|
+
for (const key in props) {
|
|
768
|
+
props[key] = props[key][props[key].length - 1];
|
|
769
|
+
}
|
|
777
770
|
const tt = {
|
|
778
771
|
type: 'object',
|
|
779
772
|
properties: {},
|
|
@@ -787,7 +780,7 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
787
780
|
const metadata = props[key].metadata || {};
|
|
788
781
|
if (!metadata.type) {
|
|
789
782
|
// 推导类型
|
|
790
|
-
metadata.type = (
|
|
783
|
+
metadata.type = core_1.MetadataManager.transformTypeFromTSDesign(core_1.MetadataManager.getPropertyType(clzz.prototype, key)).name;
|
|
791
784
|
}
|
|
792
785
|
tt.properties[key] = tt.properties[key] || {};
|
|
793
786
|
// loop metadata
|
|
@@ -897,12 +890,13 @@ let SwaggerExplorer = class SwaggerExplorer {
|
|
|
897
890
|
break;
|
|
898
891
|
case 'custom':
|
|
899
892
|
{
|
|
900
|
-
this.documentBuilder.addSecurity(opts
|
|
893
|
+
this.documentBuilder.addSecurity(opts?.name, opts);
|
|
901
894
|
}
|
|
902
895
|
break;
|
|
903
896
|
}
|
|
904
897
|
}
|
|
905
898
|
};
|
|
899
|
+
exports.SwaggerExplorer = SwaggerExplorer;
|
|
906
900
|
__decorate([
|
|
907
901
|
(0, core_1.Config)('swagger'),
|
|
908
902
|
__metadata("design:type", Object)
|
|
@@ -913,11 +907,10 @@ __decorate([
|
|
|
913
907
|
__metadata("design:paramtypes", []),
|
|
914
908
|
__metadata("design:returntype", Promise)
|
|
915
909
|
], SwaggerExplorer.prototype, "init", null);
|
|
916
|
-
SwaggerExplorer = __decorate([
|
|
910
|
+
exports.SwaggerExplorer = SwaggerExplorer = __decorate([
|
|
917
911
|
(0, core_1.Provide)(),
|
|
918
912
|
(0, core_1.Scope)(core_1.ScopeEnum.Singleton)
|
|
919
913
|
], SwaggerExplorer);
|
|
920
|
-
exports.SwaggerExplorer = SwaggerExplorer;
|
|
921
914
|
/**
|
|
922
915
|
* 解释路由上的参数
|
|
923
916
|
* @param url
|
|
@@ -75,6 +75,7 @@ let SwaggerMiddleware = class SwaggerMiddleware {
|
|
|
75
75
|
return 'swagger';
|
|
76
76
|
}
|
|
77
77
|
};
|
|
78
|
+
exports.SwaggerMiddleware = SwaggerMiddleware;
|
|
78
79
|
__decorate([
|
|
79
80
|
(0, core_1.Config)('swagger'),
|
|
80
81
|
__metadata("design:type", Object)
|
|
@@ -93,9 +94,8 @@ __decorate([
|
|
|
93
94
|
__metadata("design:paramtypes", []),
|
|
94
95
|
__metadata("design:returntype", Promise)
|
|
95
96
|
], SwaggerMiddleware.prototype, "init", null);
|
|
96
|
-
SwaggerMiddleware = __decorate([
|
|
97
|
+
exports.SwaggerMiddleware = SwaggerMiddleware = __decorate([
|
|
97
98
|
(0, core_1.Provide)(),
|
|
98
99
|
(0, core_1.Scope)(core_1.ScopeEnum.Singleton)
|
|
99
100
|
], SwaggerMiddleware);
|
|
100
|
-
exports.SwaggerMiddleware = SwaggerMiddleware;
|
|
101
101
|
//# sourceMappingURL=swaggerMiddleware.js.map
|
package/dist/ui/render.js
CHANGED
|
@@ -43,7 +43,7 @@ function renderSwaggerUIDist(swaggerConfig, swaggerExplorer) {
|
|
|
43
43
|
}
|
|
44
44
|
let content = (0, fs_1.readFileSync)((0, path_1.join)(swaggerUiAssetPath, lastName));
|
|
45
45
|
if (lastName === 'swagger-initializer.js' &&
|
|
46
|
-
|
|
46
|
+
swaggerRenderOptions?.customInitializer) {
|
|
47
47
|
return { ext: 'js', content: swaggerRenderOptions.customInitializer };
|
|
48
48
|
}
|
|
49
49
|
if (lastName === 'index.html' || lastName === 'swagger-initializer.js') {
|
|
@@ -71,7 +71,7 @@ function renderJSON(swaggerConfig, swaggerExplorer) {
|
|
|
71
71
|
exports.renderJSON = renderJSON;
|
|
72
72
|
function renderSwaggerUIRemote(swaggerConfig, swaggerExplorer) {
|
|
73
73
|
const swaggerRenderOptions = swaggerConfig.swaggerUIRenderOptions || {};
|
|
74
|
-
const indexPagePath =
|
|
74
|
+
const indexPagePath = swaggerRenderOptions?.indexPagePath || (0, path_1.join)(__dirname, '../../index.html');
|
|
75
75
|
return async (pathname) => {
|
|
76
76
|
if (pathname.indexOf(swaggerConfig.swaggerPath) === -1) {
|
|
77
77
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/swagger",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-alpha.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"typings": "index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -10,22 +10,23 @@
|
|
|
10
10
|
"index.html"
|
|
11
11
|
],
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"@midwayjs/core": "^
|
|
14
|
-
"@midwayjs/koa": "^
|
|
15
|
-
"@midwayjs/mock": "^
|
|
16
|
-
"@midwayjs/validate": "^
|
|
13
|
+
"@midwayjs/core": "^4.0.0-alpha.1",
|
|
14
|
+
"@midwayjs/koa": "^4.0.0-alpha.1",
|
|
15
|
+
"@midwayjs/mock": "^4.0.0-alpha.1",
|
|
16
|
+
"@midwayjs/validate": "^4.0.0-alpha.1",
|
|
17
17
|
"swagger-ui-dist": "5.18.2"
|
|
18
18
|
},
|
|
19
19
|
"author": "Kurten Chan <chinkurten@gmail.com>",
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "tsc",
|
|
23
|
-
"test": "../../node_modules
|
|
24
|
-
"cov": "../../node_modules
|
|
23
|
+
"test": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand",
|
|
24
|
+
"cov": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand --coverage --forceExit",
|
|
25
|
+
"ci": "npm run test"
|
|
25
26
|
},
|
|
26
27
|
"repository": {
|
|
27
28
|
"type": "git",
|
|
28
29
|
"url": "https://github.com/midwayjs/midway.git"
|
|
29
30
|
},
|
|
30
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "14bb4da91805a1cf52f190c0d37a74b395dd6372"
|
|
31
32
|
}
|