Skip to content

Commit d90b723

Browse files
committed
🐞 fix: AYPI Schema 中 $$ref、$ref 字段导致报错
1 parent b6df1ae commit d90b723

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "lowcode",
44
"description": "lowcode tool, support ChatGPT",
55
"author": "wjkang <ruoxieme@gmail.com>",
6-
"version": "1.7.2",
6+
"version": "1.7.3",
77
"icon": "asset/icon.png",
88
"publisher": "wjkang",
99
"repository": "https://github.com/lowcoding/lowcode-vscode",

src/genCode/genCodeByYapi.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export const genTemplateModelByYapi = async (
9494
const reqBodyScheme = JSON.parse(
9595
stripJsonComments(res.data.data.req_body_other),
9696
);
97+
fixSchema(reqBodyScheme);
9798
delete reqBodyScheme.title;
9899
requestBodyType = await compile(
99100
reqBodyScheme,
@@ -121,6 +122,7 @@ export const genTemplateModelByYapi = async (
121122
// const ts = await jsonToTs(selectInfo.typeName, res.data.data.res_body);
122123
const resBodyJson = JSON.parse(stripJsonComments(res.data.data.res_body));
123124
const schema = GenerateSchema.json(typeName || 'Schema', resBodyJson);
125+
fixSchema(schema);
124126
let ts = await compile(schema, typeName, {
125127
bannerComment: '',
126128
});
@@ -131,6 +133,7 @@ export const genTemplateModelByYapi = async (
131133
const reqBodyScheme = JSON.parse(
132134
stripJsonComments(res.data.data.req_body_other),
133135
);
136+
fixSchema(reqBodyScheme);
134137
delete reqBodyScheme.title;
135138
requestBodyType = await compile(
136139
reqBodyScheme,
@@ -156,18 +159,34 @@ export const genTemplateModelByYapi = async (
156159
return model;
157160
};
158161

159-
const fixSchema = (obj: object) => {
162+
function fixSchema(obj: object, fieldNames: string[] = ['$ref', '$$ref']) {
160163
// eslint-disable-next-line no-restricted-syntax
161164
for (const key in obj) {
162-
// @ts-ignore
163-
if (typeof obj[key] === 'object' && obj[key] !== null) {
164-
// @ts-ignore
165+
if (Array.isArray(obj[key])) {
166+
obj[key].forEach((item: object) => {
167+
if (typeof item === 'object' && item !== null) {
168+
fixSchema(item, fieldNames);
169+
} else {
170+
// eslint-disable-next-line no-restricted-syntax
171+
for (const fieldName of fieldNames) {
172+
if (item && item[fieldName]) {
173+
delete item[fieldName];
174+
}
175+
}
176+
}
177+
});
178+
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
165179
if (obj[key].type === 'object' && !obj[key].properties) {
166-
// @ts-ignore
167180
delete obj[key];
168181
}
169-
// @ts-ignore
170-
fixSchema(obj[key]); // 递归处理
182+
fixSchema(obj[key], fieldNames);
183+
} else {
184+
// eslint-disable-next-line no-restricted-syntax
185+
for (const fieldName of fieldNames) {
186+
if (key === fieldName) {
187+
delete obj[key];
188+
}
189+
}
171190
}
172191
}
173-
};
192+
}

tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
"target": "ES5",
66
"outDir": "build",
77
"allowSyntheticDefaultImports": true,
8+
"suppressImplicitAnyIndexErrors": true,
89
"lib": [
910
"ES5"
1011
],
1112
"sourceMap": true,
1213
"rootDir": "src",
14+
"ignoreDeprecations": "5.0",
1315
"strict": true /* enable all strict type-checking options */
1416
/* Additional Checks */
1517
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */

0 commit comments

Comments
 (0)