Skip to content

Commit 46f2a9e

Browse files
committed
feat: 增加Pick/Omit对type的支持
1 parent ffb5d2d commit 46f2a9e

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

jest/toolfn.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ type ToolFn_15 = Record<Page, string>;
6666
// interface ToolFn_18 {
6767
// name: Record<Page, PageInfo>
6868
// }
69+
70+
type Filter = 'a' | 'b';
71+
type ToolFn_16 = Pick<AAA, Filter>;
72+
73+
type ToolFn_17 = Omit<AAA, Filter>;

src/__tests__/__snapshots__/toolfn.test.ts.snap

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ Object {
5151
}
5252
`;
5353

54+
exports[`工具函数Omit_4 1`] = `
55+
Object {
56+
"additionalProperties": false,
57+
"properties": Object {
58+
"c": Object {
59+
"type": "boolean",
60+
},
61+
},
62+
"required": Array [
63+
"c",
64+
],
65+
"type": "object",
66+
}
67+
`;
68+
5469
exports[`工具函数Pick_1 1`] = `
5570
Object {
5671
"properties": Object {
@@ -80,6 +95,25 @@ Object {
8095
}
8196
`;
8297

98+
exports[`工具函数Pick_3 1`] = `
99+
Object {
100+
"additionalProperties": false,
101+
"properties": Object {
102+
"a": Object {
103+
"type": "number",
104+
},
105+
"b": Object {
106+
"type": "string",
107+
},
108+
},
109+
"required": Array [
110+
"a",
111+
"b",
112+
],
113+
"type": "object",
114+
}
115+
`;
116+
83117
exports[`工具函数Record_1 1`] = `
84118
Object {
85119
"additionalProperties": false,

src/__tests__/toolfn.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ test('工具函数Omit_3', () => {
2222
expect(getSchema('ToolFn_3')).toMatchSnapshot();
2323
});
2424

25+
test('工具函数Omit_4', () => {
26+
expect(getSchema('ToolFn_17')).toMatchSnapshot();
27+
});
28+
2529
// test('工具函数Omit_4', () => {
2630
// expect(getSchema('ToolFn_4')).toMatchSnapshot();
2731
// });
@@ -50,6 +54,10 @@ test('工具函数Pick_2', () => {
5054
expect(getSchema('ToolFn_10')).toMatchSnapshot();
5155
});
5256

57+
test('工具函数Pick_3', () => {
58+
expect(getSchema('ToolFn_16')).toMatchSnapshot();
59+
});
60+
5361
// test('工具函数Pick_3', () => {
5462
// expect(getSchema('ToolFn_11')).toMatchSnapshot();
5563
// });

src/get-jsonschema-from-data.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,9 @@ export default class genTypeSchema extends typescriptToFileDatas {
449449
if (!extra) return resType;
450450

451451
if (resType) {
452+
if (extra && extra.$ref) {
453+
extra = _.cloneDeep(attrCommonHandle(extra, false) as AnyOption);
454+
}
452455
const extraKeys = extra.enum || [];
453456
if (key === 'Omit') {
454457
const res = deleteJsonSchemaKeys(resType, extraKeys);

0 commit comments

Comments
 (0)