Skip to content

Commit 135c407

Browse files
perf: support schemas array[enum]
1 parent 5c75f65 commit 135c407

File tree

5 files changed

+102
-6
lines changed

5 files changed

+102
-6
lines changed

.changeset/odd-fireants-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': patch
3+
---
4+
5+
perf: support schemas array[enum]

src/generator/serviceGenarator.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ export default class ServiceGenerator {
328328
});
329329
});
330330

331-
const typeName = this.getTypeName({
331+
const typeName = this.getFunctionParamsTypeName({
332332
...operationObject,
333333
method,
334334
path: pathKey,
@@ -350,6 +350,7 @@ export default class ServiceGenerator {
350350
const result = this.resolveObject(schema) as Dictionary<
351351
string | boolean | IPropObject[][]
352352
>;
353+
console.log('result: ', result);
353354

354355
const getDefinesType = (): string => {
355356
if (result?.type) {
@@ -541,7 +542,7 @@ export default class ServiceGenerator {
541542
functionName: this.config.isCamelCase
542543
? camelCase(functionName)
543544
: functionName,
544-
typeName: this.getTypeName(newApi),
545+
typeName: this.getFunctionParamsTypeName(newApi),
545546
path: getPrefixPath(),
546547
pathInComment: formattedPath.replace(/\*/g, '&#42;'),
547548
apifoxRunLink: newApi?.['x-run-in-apifox'],
@@ -666,7 +667,7 @@ export default class ServiceGenerator {
666667
return getDefaultType(schemaObject, namespace, schemas);
667668
}
668669

669-
private getTypeName(data: APIDataType) {
670+
private getFunctionParamsTypeName(data: APIDataType) {
670671
const namespace = this.config.namespace ? `${this.config.namespace}.` : '';
671672
const typeName =
672673
this.config?.hook?.customTypeName?.(data) || this.getFunctionName(data);
@@ -946,6 +947,10 @@ export default class ServiceGenerator {
946947
return {
947948
type: `${refName}[]`,
948949
};
950+
} else if (schemaObject.items?.enum) {
951+
return {
952+
type: this.getType(schemaObject, this.config.namespace),
953+
};
949954
}
950955

951956
// 这里需要解析出具体属性,但由于 parser 层还不确定,所以暂时先返回 unknown[]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"openapi": "3.0.2",
3+
"info": {
4+
"title": "Swagger Petstore - OpenAPI 3.0",
5+
"description": "This is a sample Pet Store Server based on the OpenAPI 3.0 specification. You can find out more about\nSwagger at [http://swagger.io](http://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!\nYou can now help us improve the API whether it's by making changes to the definition itself or to the code.\nThat way, with time, we can improve the API in general, and expose some of the new features in OAS3.\n\nSome useful links:\n- [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)\n- [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)",
6+
"termsOfService": "http://swagger.io/terms/",
7+
"contact": {
8+
"email": "apiteam@swagger.io"
9+
},
10+
"license": {
11+
"name": "Apache 2.0",
12+
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
13+
},
14+
"version": "1.0.19"
15+
},
16+
"externalDocs": {
17+
"description": "Find out more about Swagger",
18+
"url": "http://swagger.io"
19+
},
20+
"servers": [
21+
{
22+
"url": "/api/v3"
23+
}
24+
],
25+
"tags": [
26+
{
27+
"name": "user",
28+
"description": "Operations about user"
29+
}
30+
],
31+
"paths": {
32+
"/user/{username}": {
33+
"delete": {
34+
"tags": ["user"],
35+
"summary": "Delete user",
36+
"description": "This can only be done by the logged in user.",
37+
"operationId": "deleteUser",
38+
"parameters": [
39+
{
40+
"name": "username",
41+
"in": "path",
42+
"description": "The name that needs to be deleted",
43+
"required": true,
44+
"schema": {
45+
"type": "string"
46+
}
47+
},
48+
{
49+
"name": "status",
50+
"in": "query",
51+
"description": "The status that needs to be deleted",
52+
"required": true,
53+
"schema": {
54+
"$ref": "#/components/schemas/StatusEnumArray"
55+
}
56+
}
57+
],
58+
"responses": {
59+
"400": {
60+
"description": "Invalid username supplied"
61+
},
62+
"404": {
63+
"description": "User not found"
64+
}
65+
}
66+
}
67+
}
68+
},
69+
"components": {
70+
"schemas": {
71+
"StatusEnumArray": {
72+
"type": "array",
73+
"items": {
74+
"type": "string",
75+
"enum": ["value1", "value2", "value3"]
76+
},
77+
"description": "An array of enumerated strings"
78+
}
79+
}
80+
}
81+
}

test/genOpenapi.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { generateService } = require('../dist/index');
22

33
generateService({
4-
schemaPath: `${__dirname}/example-files/openapi-ref-encode-character.json`,
5-
serversPath: './apis/ref-encode-character',
6-
isTranslateToEnglishTag: true
4+
schemaPath: `${__dirname}/example-files/openapi-schemas-enum-array.json`,
5+
serversPath: './apis/schemas-enum-array',
76
});

test/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ const gen = async () => {
152152
serversPath: './apis/x-run-in-apifox'
153153
});
154154

155+
// 测试 schemas 包含枚举数组
156+
await openAPI.generateService({
157+
schemaPath: `${__dirname}/example-files/openapi-schemas-enum-array.json`,
158+
serversPath: './apis/schemas-enum-array'
159+
});
160+
155161
// check 文件生成
156162
const fileControllerStr = fs.readFileSync(
157163
path.join(__dirname, 'apis/file/fileController.ts'),

0 commit comments

Comments
 (0)