Skip to content

Commit a1c661b

Browse files
committed
feat: 增加Partial、Required对联合类型的处理
1 parent 486e1ea commit a1c661b

File tree

4 files changed

+148
-4
lines changed

4 files changed

+148
-4
lines changed

jest/toolfn.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,8 @@ type ToolFn_21 = Required<CCC>;
102102

103103
type ToolFn_22 = Required<DDD>;
104104

105-
type ToolFn_23 = Required<{ a?: string, b?: DDD }>;
105+
type ToolFn_23 = Required<{ a?: string, b?: DDD }>;
106+
107+
type ToolFn_24 = Partial<BBB | AAA>;
108+
109+
type ToolFn_25 = Required<DDD | CCC>;

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

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,65 @@ Object {
179179
}
180180
`;
181181

182+
exports[`工具函数Partial_联合类型_1 1`] = `
183+
Object {
184+
"anyOf": Array [
185+
Object {
186+
"additionalProperties": false,
187+
"definitions": Object {
188+
"AAA": Object {
189+
"additionalProperties": false,
190+
"properties": Object {
191+
"a": Object {
192+
"type": "number",
193+
},
194+
"b": Object {
195+
"type": "string",
196+
},
197+
"c": Object {
198+
"type": "boolean",
199+
},
200+
},
201+
"required": Array [
202+
"a",
203+
"b",
204+
"c",
205+
],
206+
"type": "object",
207+
},
208+
},
209+
"properties": Object {
210+
"a": Object {
211+
"type": "number",
212+
},
213+
"b": Object {
214+
"type": "string",
215+
},
216+
"c": Object {
217+
"$ref": "#/definitions/AAA",
218+
},
219+
},
220+
"type": "object",
221+
},
222+
Object {
223+
"additionalProperties": false,
224+
"properties": Object {
225+
"a": Object {
226+
"type": "number",
227+
},
228+
"b": Object {
229+
"type": "string",
230+
},
231+
"c": Object {
232+
"type": "boolean",
233+
},
234+
},
235+
"type": "object",
236+
},
237+
],
238+
}
239+
`;
240+
182241
exports[`工具函数Pick_1 1`] = `
183242
Object {
184243
"properties": Object {
@@ -406,3 +465,67 @@ Object {
406465
"type": "object",
407466
}
408467
`;
468+
469+
exports[`工具函数Required_联合类型_1 1`] = `
470+
Object {
471+
"anyOf": Array [
472+
Object {
473+
"additionalProperties": false,
474+
"definitions": Object {
475+
"CCC": Object {
476+
"additionalProperties": false,
477+
"properties": Object {
478+
"a": Object {
479+
"type": "number",
480+
},
481+
"b": Object {
482+
"type": "string",
483+
},
484+
"c": Object {
485+
"type": "boolean",
486+
},
487+
},
488+
"type": "object",
489+
},
490+
},
491+
"properties": Object {
492+
"a": Object {
493+
"type": "number",
494+
},
495+
"b": Object {
496+
"type": "string",
497+
},
498+
"c": Object {
499+
"$ref": "#/definitions/CCC",
500+
},
501+
},
502+
"required": Array [
503+
"a",
504+
"b",
505+
"c",
506+
],
507+
"type": "object",
508+
},
509+
Object {
510+
"additionalProperties": false,
511+
"properties": Object {
512+
"a": Object {
513+
"type": "number",
514+
},
515+
"b": Object {
516+
"type": "string",
517+
},
518+
"c": Object {
519+
"type": "boolean",
520+
},
521+
},
522+
"required": Array [
523+
"a",
524+
"b",
525+
"c",
526+
],
527+
"type": "object",
528+
},
529+
],
530+
}
531+
`;

src/__tests__/toolfn.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ test('工具函数Partial_3', () => {
103103
expect(getSchema('ToolFn_20')).toMatchSnapshot();
104104
});
105105

106+
test('工具函数Partial_联合类型_1', () => {
107+
expect(getSchema('ToolFn_24')).toMatchSnapshot();
108+
});
109+
106110
test('工具函数Required_1', () => {
107111
expect(getSchema('ToolFn_21')).toMatchSnapshot();
108112
});
@@ -113,4 +117,8 @@ test('工具函数Required_2', () => {
113117

114118
test('工具函数Required_3', () => {
115119
expect(getSchema('ToolFn_23')).toMatchSnapshot();
116-
});
120+
});
121+
122+
test('工具函数Required_联合类型_1', () => {
123+
expect(getSchema('ToolFn_25')).toMatchSnapshot();
124+
})

src/get-jsonschema-from-data.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ export default class genTypeSchema extends typescriptToFileDatas {
314314
$refJson = _.cloneDeep($refJson)
315315
if ((entry as any).keySet.has($refKey)) {
316316
(entry as any).refKeyTime[$refKey] = ((entry as any).refKeyTime[$refKey] || 0) + 1;
317-
return;
317+
return $refJson;
318318
}
319319

320320
(entry as any).keySet.add($refKey);
@@ -472,9 +472,18 @@ export default class genTypeSchema extends typescriptToFileDatas {
472472
resType = _.cloneDeep(this.genJsonschema(fileJson, type, entry, file) as AnyOption);
473473
} else if (type.$ref) {
474474
resType = _.cloneDeep(attrCommonHandle(type, false) as AnyOption);
475+
} else if (type.anyOf) {
476+
resType = { anyOf: [] };
477+
resType.anyOf = type.anyOf.map((item: AnyOption) => {
478+
return PartialRequiredHandle(key, item);
479+
})
475480
}
476481

477-
if (resType) {
482+
if (resType && Object.keys(resType).length) {
483+
if (resType.anyOf) {
484+
return resType;
485+
}
486+
478487
if (key === 'Partial') {
479488
delete resType.required;
480489
return resType;

0 commit comments

Comments
 (0)