Skip to content

Commit bc0d0c2

Browse files
committed
feat: 支持动态对象
1 parent d86eca6 commit bc0d0c2

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/lib/parse-schema.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ export const parseSchema = (
3636
const schemaObj = refToObject(docs, schema);
3737
return `${generateComments(schemaObj)}"${key}"${requiredProperties.includes(key) ? '' : '?'}: ${parseSchema(docs, schemaObj)}`;
3838
});
39+
40+
if (typeof parsed.additionalProperties === 'object') {
41+
properties.push(
42+
`[key: string]: ${parseSchema(docs, refToObject(docs, parsed.additionalProperties))}`,
43+
);
44+
}
45+
3946
return `({ ${properties.join(';')} }${nullable})`;
4047
}
4148
default:

test/lib/parse-schema.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ describe('常规', () => {
8787
const type = parseSchema(docs, { type: 'integer', format: 'int64' });
8888
expect(type).toMatchInlineSnapshot(`"(string)"`);
8989
});
90+
91+
test('动态对象属性', () => {
92+
expect(
93+
parseSchema(docs, { type: 'object', additionalProperties: { type: 'string' } }),
94+
).toMatchInlineSnapshot(`"({ [key: string]: (string) })"`);
95+
96+
expect(
97+
parseSchema(docs, {
98+
type: 'object',
99+
additionalProperties: {
100+
type: 'object',
101+
properties: { foo: { type: 'string' }, bar: { type: 'number' } },
102+
},
103+
}),
104+
).toMatchInlineSnapshot(
105+
`"({ [key: string]: ({ "foo"?: (string);"bar"?: (number) }) })"`,
106+
);
107+
});
90108
});
91109

92110
describe('oneOf', () => {

0 commit comments

Comments
 (0)