swagger2api-v3 1.1.3 → 1.1.4
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/README.md +1 -1
- package/dist/core/parser.d.ts +5 -0
- package/dist/core/parser.js +17 -4
- package/dist/utils/index.d.ts +2 -1
- package/dist/utils/index.js +10 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -236,7 +236,7 @@ const config = {
|
|
|
236
236
|
|
|
237
237
|
## 🤝 Contributing
|
|
238
238
|
|
|
239
|
-
|
|
239
|
+
If you encounter any problems or have suggestions, please feel free to [submit an issue](https://github.com/xiaoyang33/swagger2api-v3/issues) on GitHub. Pull Requests are also welcome!
|
|
240
240
|
|
|
241
241
|
## 📄 License
|
|
242
242
|
|
package/dist/core/parser.d.ts
CHANGED
|
@@ -6,6 +6,11 @@ export declare class SwaggerParser {
|
|
|
6
6
|
private document;
|
|
7
7
|
private config;
|
|
8
8
|
constructor(document: SwaggerDocument, config: SwaggerConfig);
|
|
9
|
+
/**
|
|
10
|
+
* 获取所有 schemas (包括 definitions 和 components.schemas)
|
|
11
|
+
* @returns schemas 对象
|
|
12
|
+
*/
|
|
13
|
+
private getAllSchemas;
|
|
9
14
|
/**
|
|
10
15
|
* 解析所有API接口
|
|
11
16
|
* @returns API接口信息数组
|
package/dist/core/parser.js
CHANGED
|
@@ -10,6 +10,16 @@ class SwaggerParser {
|
|
|
10
10
|
this.document = document;
|
|
11
11
|
this.config = config;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* 获取所有 schemas (包括 definitions 和 components.schemas)
|
|
15
|
+
* @returns schemas 对象
|
|
16
|
+
*/
|
|
17
|
+
getAllSchemas() {
|
|
18
|
+
return {
|
|
19
|
+
...(this.document.definitions || {}),
|
|
20
|
+
...(this.document.components?.schemas || {})
|
|
21
|
+
};
|
|
22
|
+
}
|
|
13
23
|
/**
|
|
14
24
|
* 解析所有API接口
|
|
15
25
|
* @returns API接口信息数组
|
|
@@ -163,12 +173,14 @@ class SwaggerParser {
|
|
|
163
173
|
parseTypeDefinition(name, schema) {
|
|
164
174
|
const typeName = (0, utils_1.toPascalCase)(name);
|
|
165
175
|
let definition;
|
|
176
|
+
// 获取所有 schemas 用于类型解析
|
|
177
|
+
const allSchemas = this.getAllSchemas();
|
|
166
178
|
if (schema.type === 'object' && schema.properties) {
|
|
167
179
|
// 对象类型
|
|
168
180
|
const properties = Object.entries(schema.properties)
|
|
169
181
|
.map(([key, value]) => {
|
|
170
182
|
const optional = schema.required?.includes(key) ? '' : '?';
|
|
171
|
-
const type = (0, utils_1.swaggerTypeToTsType)(value);
|
|
183
|
+
const type = (0, utils_1.swaggerTypeToTsType)(value, allSchemas);
|
|
172
184
|
const comment = value.description
|
|
173
185
|
? ` /** ${value.description} */`
|
|
174
186
|
: '';
|
|
@@ -178,9 +190,10 @@ class SwaggerParser {
|
|
|
178
190
|
definition = `export interface ${typeName} {\n${properties}\n}`;
|
|
179
191
|
}
|
|
180
192
|
else if (schema.type === 'array') {
|
|
181
|
-
// 数组类型
|
|
182
|
-
|
|
183
|
-
|
|
193
|
+
// 数组类型 - 生成指向 items 类型的别名(不带 [])
|
|
194
|
+
// 在引用时会自动添加 []
|
|
195
|
+
const itemType = (0, utils_1.swaggerTypeToTsType)(schema.items, allSchemas);
|
|
196
|
+
definition = `export type ${typeName} = ${itemType};`;
|
|
184
197
|
}
|
|
185
198
|
else if (schema.enum) {
|
|
186
199
|
// 枚举类型
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -44,9 +44,10 @@ export declare function removeMethodSuffix(functionName: string, method: string)
|
|
|
44
44
|
/**
|
|
45
45
|
* 将Swagger类型转换为TypeScript类型
|
|
46
46
|
* @param schema Swagger模式
|
|
47
|
+
* @param schemas 可选的 schemas 上下文,用于查找被引用的类型定义
|
|
47
48
|
* @returns TypeScript类型字符串
|
|
48
49
|
*/
|
|
49
|
-
export declare function swaggerTypeToTsType(schema: any): string;
|
|
50
|
+
export declare function swaggerTypeToTsType(schema: any, schemas?: any): string;
|
|
50
51
|
/**
|
|
51
52
|
* 从Swagger参数生成TypeScript参数类型
|
|
52
53
|
* @param parameters Swagger参数数组
|
package/dist/utils/index.js
CHANGED
|
@@ -168,9 +168,10 @@ function removeMethodSuffix(functionName, method) {
|
|
|
168
168
|
/**
|
|
169
169
|
* 将Swagger类型转换为TypeScript类型
|
|
170
170
|
* @param schema Swagger模式
|
|
171
|
+
* @param schemas 可选的 schemas 上下文,用于查找被引用的类型定义
|
|
171
172
|
* @returns TypeScript类型字符串
|
|
172
173
|
*/
|
|
173
|
-
function swaggerTypeToTsType(schema) {
|
|
174
|
+
function swaggerTypeToTsType(schema, schemas) {
|
|
174
175
|
if (!schema)
|
|
175
176
|
return 'any';
|
|
176
177
|
let baseType = 'any';
|
|
@@ -246,6 +247,14 @@ function swaggerTypeToTsType(schema) {
|
|
|
246
247
|
else if (schema.$ref) {
|
|
247
248
|
const refName = schema.$ref.split('/').pop();
|
|
248
249
|
baseType = sanitizeTypeName(refName || 'any');
|
|
250
|
+
// 如果提供了 schemas 上下文,检查被引用的 schema 是否是数组类型
|
|
251
|
+
if (schemas && refName) {
|
|
252
|
+
const referencedSchema = schemas[refName];
|
|
253
|
+
if (referencedSchema && referencedSchema.type === 'array') {
|
|
254
|
+
// 被引用的 schema 是数组类型,添加 []
|
|
255
|
+
baseType = `${baseType}[]`;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
249
258
|
}
|
|
250
259
|
// 处理数组类型
|
|
251
260
|
else if (schema.type === 'array') {
|
package/package.json
CHANGED