Skip to content

Commit 323ece5

Browse files
authored
Merge pull request #10 from wangweianger/master
feat: 1.tags字段注释优先 2.operationId字段规则更新为 controller+方法名+method+path route
2 parents 7da5249 + d3a7cad commit 323ece5

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

openapi/openapiv3.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"openapi":"3.0.3","info":{"title":"routing-controllers-to-openapi","description":"Buildtime OpenAPI v3 spec generation for routing-controllers","version":"1.0.0"},"tags":[{"name":"ExampleController","description":"测试案例controller"}],"paths":{"/example/test/{id}/{name}":{"post":{"tags":["ExampleController"],"summary":"do something","description":"ExampleController.getTest 测试案例controller ","operationId":"ExampleController.post.getTest.zJtCBwDnjR","responses":{"200":{"description":"","content":{"application/json":{"schema":{"type":"object","properties":{"code":{"type":"number","description":"code返回码"},"message":{"type":"string","description":"返回信息"}},"required":["code","message"],"additionalProperties":false}}}}},"parameters":[{"name":"id","in":"path","description":"@Param, 类型:string","required":true,"schema":{"type":"string"}},{"name":"name","in":"path","description":"姓名 (@Param, 类型:string)","required":true,"schema":{"type":"string"}}],"requestBody":{"description":"","content":{"application/json":{"schema":{"type":"object","properties":{"params":{"type":"object","properties":{"name":{"type":"string"},"age":{"$ref":"#/components/schemas/Aaa"}},"required":["name","age"],"description":"@BodyParam"}},"required":["params"]}}}}}}},"components":{"schemas":{"Aaa":{"type":"object","properties":{"code":{"type":"string","enum":["a","b"]}},"required":["code"],"additionalProperties":false}}}}
1+
{"openapi":"3.0.3","info":{"title":"routing-controllers-to-openapi","description":"Buildtime OpenAPI v3 spec generation for routing-controllers","version":"1.0.0"},"tags":[{"name":"测试案例controller","description":"ExampleController"}],"paths":{"/example/test/{id}/{name}":{"post":{"tags":["测试案例controller"],"summary":"do something","description":"ExampleController.getTest 测试案例controller ","operationId":"ExampleController_getTest_post_example_test_{id}_{name}","responses":{"200":{"description":"","content":{"application/json":{"schema":{"type":"object","properties":{"code":{"type":"number","description":"code返回码"},"message":{"type":"string","description":"返回信息"}},"required":["code","message"],"additionalProperties":false}}}}},"parameters":[{"name":"id","in":"path","description":"@Param, 类型:string","required":true,"schema":{"type":"string"}},{"name":"name","in":"path","description":"姓名 (@Param, 类型:string)","required":true,"schema":{"type":"string"}}],"requestBody":{"description":"","content":{"application/json":{"schema":{"type":"object","properties":{"params":{"type":"object","properties":{"name":{"type":"string"},"age":{"$ref":"#/components/schemas/Aaa"}},"required":["name","age"],"description":"@BodyParam"}},"required":["params"]}}}}}}},"components":{"schemas":{"Aaa":{"type":"object","properties":{"code":{"type":"string","enum":["a","b"]}},"required":["code"],"additionalProperties":false}}}}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "routing-controllers-to-openapi",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"main": "./dist/index.js",
55
"typings": "dist/index.d.ts",
66
"repository": "https://github.com/yunke-yunfly/routing-controllers-to-openapi",
@@ -83,4 +83,4 @@
8383
"glob": "^7.2.0",
8484
"lodash": "^4.17.21"
8585
}
86-
}
86+
}

src/gen-openapi.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as path from 'path';
22
import * as _ from 'lodash';
3-
import { randomString } from './util';
43
// types
54
import type {
65
AnyOpt,
@@ -111,13 +110,14 @@ export function genTags(paths: TraverseAstConfig[]): Openapiv3Tags[] {
111110
const cache: Record<string, number> = {};
112111

113112
return paths.reduce((prev: Openapiv3Tags[], path: TraverseAstConfig) => {
114-
if (!cache[path.className] && path.className) {
115-
cache[path.className] = 1;
113+
const tag = path.description || path.className;
114+
if (!cache[tag] && tag) {
115+
cache[tag] = 1;
116116
return [
117117
...prev,
118118
{
119-
name: path.className,
120-
description: path.description,
119+
name: tag,
120+
description: path.className,
121121
},
122122
];
123123
}
@@ -154,11 +154,12 @@ export function genPaths(
154154
// 处理路由
155155
const res = (itemPath.paths || []).reduce((childPrevPath: AnyOpt, childItemPath: AnyOpt) => {
156156
const method = (childItemPath.method || '').toLowerCase();
157+
const tag = next.description || next.className;
157158
const methodJson: MethodConfig = {
158-
tags: [next.className],
159+
tags: [tag],
159160
summary: itemPath.description || childItemPath.operationId || '',
160161
description: itemPath.description_ || `${next.className}.${childItemPath.operationId} ${next.description} `,
161-
operationId: `${next.className}.${method}.${childItemPath.operationId}.${randomString(10)}`,
162+
operationId: `${next.className}_${childItemPath.operationId}_${method}${childItemPath.routerPath.replace(/\//g, '_')}`,
162163
responses,
163164
};
164165
if (parameters.length) methodJson.parameters = parameters;

test/SimpleController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default class {
1919
@QueryParam('simple3') simple3: boolean,
2020
@QueryParam('simple4') simple4: any,
2121
@QueryParam('simple5', { required: true }) simple5: never,
22-
@QueryParam('simple6') simple6: {name: string},
22+
@QueryParam('simple6') simple6: { name: string },
2323
@Param('id', { required: true }) id: number,
2424
@UseMetadata() metadata: any,
2525
): Promise<{ name: string }> {
@@ -44,6 +44,7 @@ export default class {
4444

4545
/**
4646
* do something
47+
* @description 测试函数备注信息
4748
*
4849
* @param {string} orgcode 租户号
4950
* @param {string} name 姓名

0 commit comments

Comments
 (0)