Skip to content

Commit e9d0749

Browse files
committed
fix: 对象结构中可能包含特殊字符,需要使用引号包裹
1 parent 3863cd0 commit e9d0749

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

src/lib/parse-schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const parseSchema = (
3030
const requiredProperties = parsed.required || [];
3131
const properties = Object.entries(parsed.properties || {}).map(([key, schema]) => {
3232
const schemaObj = refToObject(docs, schema);
33-
return `${generateComments(schemaObj)}${key}${requiredProperties.includes(key) ? '' : '?'}: ${parseSchema(docs, schemaObj)}`;
33+
return `${generateComments(schemaObj)}"${key}"${requiredProperties.includes(key) ? '' : '?'}: ${parseSchema(docs, schemaObj)}`;
3434
});
3535
return `({ ${properties.join(';')} }${nullable})`;
3636
}

test/lib/parse-request-body.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test('从requestBody字段获取', () => {
2424
"body": {
2525
"optional": false,
2626
"types": [
27-
"({ foo?: (string) })",
27+
"({ "foo"?: (string) })",
2828
],
2929
},
3030
"contentTypes": [
@@ -57,7 +57,7 @@ test('多个返回类型', () => {
5757
"body": {
5858
"optional": false,
5959
"types": [
60-
"({ foo?: (string) })",
60+
"({ "foo"?: (string) })",
6161
"(string)",
6262
],
6363
},
@@ -138,7 +138,7 @@ test('*/* 类型被忽略', () => {
138138
"body": {
139139
"optional": false,
140140
"types": [
141-
"({ foo?: (string) })",
141+
"({ "foo"?: (string) })",
142142
],
143143
},
144144
"contentTypes": [],

test/lib/parse-response.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test('从response字段获取', () => {
2424
{
2525
"response": {
2626
"types": [
27-
"({ foo?: (string) })",
27+
"({ "foo"?: (string) })",
2828
],
2929
},
3030
"responseTypes": [
@@ -72,8 +72,8 @@ test('只保留2xx状态', () => {
7272
{
7373
"response": {
7474
"types": [
75-
"({ foo?: (string) })",
76-
"({ foo1?: (string) })",
75+
"({ "foo"?: (string) })",
76+
"({ "foo1"?: (string) })",
7777
],
7878
},
7979
"responseTypes": [
@@ -105,7 +105,7 @@ test('*/* 类型被忽略', () => {
105105
{
106106
"response": {
107107
"types": [
108-
"({ foo?: (string) })",
108+
"({ "foo"?: (string) })",
109109
],
110110
},
111111
"responseTypes": [],

test/lib/parse-schema.test.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ describe('常规', () => {
3434
required: ['foo'],
3535
},
3636
});
37-
expect(type).toMatchInlineSnapshot(`"((({ foo: (string) }))[])"`);
37+
expect(type).toMatchInlineSnapshot(`"((({ "foo": (string) }))[])"`);
3838
});
3939

4040
test('对象', () => {
4141
const type = parseSchema(docs, {
4242
type: 'object',
4343
properties: { foo: { type: 'string' } },
4444
});
45-
expect(type).toMatchInlineSnapshot(`"({ foo?: (string) })"`);
45+
expect(type).toMatchInlineSnapshot(`"({ "foo"?: (string) })"`);
4646
});
4747

4848
test('对象属性包含描述', () => {
@@ -55,10 +55,18 @@ describe('常规', () => {
5555
/**
5656
* foo=bar
5757
*/
58-
foo?: (string) })"
58+
"foo"?: (string) })"
5959
`);
6060
});
6161

62+
test('对象属性包含特殊字符', () => {
63+
const type = parseSchema(docs, {
64+
type: 'object',
65+
properties: { '123foo': { type: 'string' } },
66+
});
67+
expect(type).toMatchInlineSnapshot(`"({ "123foo"?: (string) })"`);
68+
});
69+
6270
test('空对象', () => {
6371
const type = parseSchema(docs, { type: 'object' });
6472
expect(type).toMatchInlineSnapshot(`"({ })"`);
@@ -98,7 +106,7 @@ describe('oneOf', () => {
98106
],
99107
});
100108
expect(type).toMatchInlineSnapshot(
101-
`"((((({ foo?: (string) }) | ({ bar: (string) })))))"`,
109+
`"((((({ "foo"?: (string) }) | ({ "bar": (string) })))))"`,
102110
);
103111
});
104112

@@ -132,7 +140,7 @@ describe('anyOf', () => {
132140
],
133141
});
134142
expect(type).toMatchInlineSnapshot(
135-
`"((((({ foo?: (string) }) | ({ bar: (string) })))))"`,
143+
`"((((({ "foo"?: (string) }) | ({ "bar": (string) })))))"`,
136144
);
137145
});
138146

@@ -166,7 +174,7 @@ describe('allOf', () => {
166174
],
167175
});
168176
expect(type).toMatchInlineSnapshot(
169-
`"(((({ foo?: (string) }) & ({ bar: (string) }))))"`,
177+
`"(((({ "foo"?: (string) }) & ({ "bar": (string) }))))"`,
170178
);
171179
});
172180

@@ -196,7 +204,7 @@ describe('allOf + anyOf + oneOf', () => {
196204
],
197205
});
198206
expect(type).toMatchInlineSnapshot(
199-
`"((((({ foo2?: (string) }) | ({ bar2?: (string) })) | (({ foo1?: (string) }) | ({ bar1?: (string) }))) & (({ foo: (string) }) & ({ bar?: (string) }))))"`,
207+
`"((((({ "foo2"?: (string) }) | ({ "bar2"?: (string) })) | (({ "foo1"?: (string) }) | ({ "bar1"?: (string) }))) & (({ "foo": (string) }) & ({ "bar"?: (string) }))))"`,
200208
);
201209
});
202210

@@ -208,7 +216,7 @@ describe('allOf + anyOf + oneOf', () => {
208216
oneOf: [{ properties: { foo: { type: 'string' } }, required: ['foo'] }],
209217
});
210218
expect(type).toMatchInlineSnapshot(
211-
`"((((({ foo: (string) }))) & (({ foo2?: (string) }))))"`,
219+
`"((((({ "foo": (string) }))) & (({ "foo2"?: (string) }))))"`,
212220
);
213221
});
214222

@@ -219,7 +227,7 @@ describe('allOf + anyOf + oneOf', () => {
219227
anyOf: [{ properties: { foo2: { type: 'string' } }, required: ['foo'] }],
220228
oneOf: [{ properties: { foo: { type: 'string' } }, required: ['foo'] }],
221229
});
222-
expect(type).toMatchInlineSnapshot(`"((((({ foo: (string) })))))"`);
230+
expect(type).toMatchInlineSnapshot(`"((((({ "foo": (string) })))))"`);
223231
});
224232

225233
test('anyOf = allOf', () => {
@@ -229,7 +237,7 @@ describe('allOf + anyOf + oneOf', () => {
229237
anyOf: [{ properties: { foo: { type: 'string' } }, required: ['foo'] }],
230238
oneOf: [{ properties: { foo2: { type: 'string' } }, required: ['foo'] }],
231239
});
232-
expect(type).toMatchInlineSnapshot(`"((((({ foo: (string) })))))"`);
240+
expect(type).toMatchInlineSnapshot(`"((((({ "foo": (string) })))))"`);
233241
});
234242

235243
test('oneOf = anyOf = allOf', () => {
@@ -239,7 +247,7 @@ describe('allOf + anyOf + oneOf', () => {
239247
anyOf: [{ properties: { foo: { type: 'string' } }, required: ['foo'] }],
240248
oneOf: [{ properties: { foo: { type: 'string' } }, required: ['foo'] }],
241249
});
242-
expect(type).toMatchInlineSnapshot(`"((((({ foo: (string) })))))"`);
250+
expect(type).toMatchInlineSnapshot(`"((((({ "foo": (string) })))))"`);
243251
});
244252
});
245253

@@ -286,7 +294,7 @@ describe('nullable', () => {
286294
type: 'object',
287295
properties: { foo: { type: 'string', nullable: true } },
288296
}),
289-
).toMatchInlineSnapshot(`"({ foo?: (string | null) })"`);
297+
).toMatchInlineSnapshot(`"({ "foo"?: (string | null) })"`);
290298
});
291299

292300
test('二进制', () => {

0 commit comments

Comments
 (0)