Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

生成 open api 时,能自定义 Request 对象的名称,且能输出字段的 description #17

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ export function convertToOpenAPI(
if (validateType === 'string') {
return {
type: 'string',
description: validateType.description,
};
}
if (validateType === 'int' || validateType === 'number') {
return {
type: 'number',
description: validateType.description,
};
}
if (validateType.type === 'object' && validateType.rule) {
Expand All @@ -96,9 +98,10 @@ export function convertToOpenAPI(
}
});

const typeName = `GenType_${typeCount++}`;
const typeName = validateType.schemaName || `GenType_${typeCount++}`;
builder.addSchema(typeName, {
type: validateType.type,
description: validateType.description,
required: required,
properties,
});
Expand All @@ -110,14 +113,16 @@ export function convertToOpenAPI(
if (validateType.type === 'enum') {
return {
type: 'string',
description: validateType.description,
};
}

return {
type: validateType.type,
description: validateType.description,
items: validateType.itemType
? validateType.itemType === 'object'
? convertValidateToSchema({ type: 'object', rule: validateType.rule })
? convertValidateToSchema({ type: 'object', rule: validateType.rule, schemaName: validateType.schemaName })
: { type: validateType.itemType }
: undefined,
enum: Array.isArray(validateType.values)
Expand All @@ -140,6 +145,7 @@ export function convertToOpenAPI(
// TODO complex type process
return {
type: isSimpleType ? type.toLowerCase() : 'object',
description: p.validateType ? p.validateType.description : undefined,
items:
type === 'Array'
? {
Expand Down Expand Up @@ -227,6 +233,8 @@ export function convertToOpenAPI(
return {
name: p.paramName,
in: source,
default: p.validateType ? p.validateType.default : undefined,
description: p.validateType ? p.validateType.description : undefined,
required:
source === 'path' || p.required || getValue(() => p.validateType.required),
schema: getTypeSchema(p),
Expand Down
2 changes: 2 additions & 0 deletions lib/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface RouteMetadataType<ExtType = any> {
type: string | any;
required?: boolean;
default?: any;
schemaName?: string;
description?: string;
[other: string]: any;
};
}[];
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"commander": "^2.17.1",
"egg-aop": "^0.5.2",
"jsencrypt": "^3.0.0-rc.1",
"openapi-generator": "^0.1.9",
"openapi-generator": "^0.1.34",
"openapi3-ts": "^1.0.2",
"parameter": "^3.0.0",
"request": "^2.88.0",
Expand All @@ -55,11 +55,11 @@
"@types/node": "^10.7.1",
"@types/supertest": "^2.0.4",
"autod": "^3.0.1",
"egg": "^2.7.1",
"egg-bin": "^4.7.0",
"egg-ci": "^1.8.0",
"egg-mock": "^3.17.0",
"egg-ts-helper": "^1.6.0",
"egg": "^2.7.1",
"rimraf": "^2.6.2",
"supertest": "^3.0.0",
"tslint": "^5.9.1",
Expand All @@ -78,6 +78,7 @@
"autod": "autod",
"ts": "rimraf app/**/*.js lib/**/*.js app/**/*.d.ts lib/**/*.d.ts && tsc",
"debug": "tsc -w -p tsconfig.debug.json",
"dev": "tsc -w",
"prepublish": "npm run test && npm run ts",
"postpublish": "node scripts/published.js"
},
Expand Down
36 changes: 34 additions & 2 deletions test/fixtures/openapi/app/controller/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ export class HomeController extends Controller {
return 'ok';
}

@route('/api/query', { name: 'get with query: q' })
@route('/api/query', {
name: 'get with query: q',
validateMetaInfo: [
{
name: 'q',
rule: {
type: 'string',
description: 'query'
}
}
]
})
getQuery(q: string) {
return 'ok';
}
Expand All @@ -22,8 +33,29 @@ export class HomeController extends Controller {
return 'ok';
}

@route('POST /api/body', { name: 'post with body: q' })
@route('POST /api/body', {
name: 'post with body: q',
validateMetaInfo: [
{
name: 'q',
rule: {
schemaName: 'ReqData',
type: 'object',
rule: {
a: {
type: 'string'
},
b: {
type: 'number'
}
}
}
}
]
})
post(q: { a: string, b: number }) {
return 'ok';
}


}
25 changes: 21 additions & 4 deletions test/fixtures/openapi/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"name": "q",
"in": "query",
"schema": {
"type": "string"
"type": "string",
"description": "query"
}
}
],
Expand Down Expand Up @@ -132,7 +133,7 @@
"type": "object",
"properties": {
"q": {
"type": "object"
"$ref": "#/components/schemas/ReqData"
}
}
}
Expand All @@ -153,7 +154,23 @@
}
},
"components": {
"schemas": {},
"schemas": {
"ReqData": {
"type": "object",
"required": [
"a",
"b"
],
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "number"
}
}
}
},
"responses": {},
"parameters": {},
"examples": {},
Expand All @@ -169,4 +186,4 @@
}
],
"servers": []
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"inlineSourceMap": false,
"importHelpers": true
},
"exclude": ["test"],
"exclude": ["test", "node_modules*"],
"compileOnSave": true
}
Loading