From ffb5d2d2ebe606733fb8031c5b4018eccca67a71 Mon Sep 17 00:00:00 2001 From: HUxiaoAlinNG Date: Mon, 21 Mar 2022 17:18:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E6=B3=9B=E5=9E=8B=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC=EF=BC=8C=E8=BF=9B=E8=A1=8C=E6=B7=B1=E5=BA=A6=E5=85=8B?= =?UTF-8?q?=E9=9A=86=EF=BC=8C=E9=81=BF=E5=85=8D=E8=A2=AB=E8=A6=86=E7=9B=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest/generic.ts | 4 +++- .../__snapshots__/generic.test.ts.snap | 19 +++++++++++++++++++ src/__tests__/generic.test.ts | 4 ++++ src/get-jsonschema-from-data.ts | 10 ++-------- src/typescript-to-file-datas.ts | 2 +- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/jest/generic.ts b/jest/generic.ts index 54b7651..5b17f3a 100644 --- a/jest/generic.ts +++ b/jest/generic.ts @@ -45,4 +45,6 @@ interface Generic_11> { value: T[]; } -type Generic_12 = Generic_11 \ No newline at end of file +type Generic_12 = Generic_11 + +type Generic_13 = Generic_11 \ No newline at end of file diff --git a/src/__tests__/__snapshots__/generic.test.ts.snap b/src/__tests__/__snapshots__/generic.test.ts.snap index 14f0fd0..5df3711 100644 --- a/src/__tests__/__snapshots__/generic.test.ts.snap +++ b/src/__tests__/__snapshots__/generic.test.ts.snap @@ -239,3 +239,22 @@ Object { "type": "object", } `; + +exports[`Generic默认值_类型_3 1`] = ` +Object { + "additionalProperties": false, + "definitions": Object {}, + "properties": Object { + "value": Object { + "items": Object { + "type": "string", + }, + "type": "array", + }, + }, + "required": Array [ + "value", + ], + "type": "object", +} +`; diff --git a/src/__tests__/generic.test.ts b/src/__tests__/generic.test.ts index 17f802d..44797f5 100644 --- a/src/__tests__/generic.test.ts +++ b/src/__tests__/generic.test.ts @@ -30,6 +30,10 @@ test('Generic默认值_类型_2', () => { expect(getSchema('Generic_12')).toMatchSnapshot(); }); +test('Generic默认值_类型_3', () => { + expect(getSchema('Generic_13')).toMatchSnapshot(); +}); + test('Generic多层对象_类型_1', () => { expect(getSchema('Generic_9')).toMatchSnapshot(); }); diff --git a/src/get-jsonschema-from-data.ts b/src/get-jsonschema-from-data.ts index 979a068..fbf1206 100644 --- a/src/get-jsonschema-from-data.ts +++ b/src/get-jsonschema-from-data.ts @@ -232,7 +232,7 @@ export default class genTypeSchema extends typescriptToFileDatas { if (file) { this.extendJsonData(file, name, realRef); } - fileJson[name] = _.cloneDeep(realRef); + fileJson[name] = fileJson[name] || _.cloneDeep(realRef); } // 对象类型 if (result.properties && Object.keys(result.properties)) { @@ -311,6 +311,7 @@ export default class genTypeSchema extends typescriptToFileDatas { // 兼容import外部引入与内部引用两种方式 let $refJson = fileJson[firstKey] || fileJson[$refKey] || {}; + $refJson = _.cloneDeep($refJson) if ((entry as any).keySet.has($refKey)) { (entry as any).refKeyTime[$refKey] = ((entry as any).refKeyTime[$refKey] || 0) + 1; return; @@ -606,13 +607,6 @@ export default class genTypeSchema extends typescriptToFileDatas { typeJson = handleExtends(typeJson); } - // 处理默认值 - if (typeJson.typeParams) { - const defaultNames = handleGenericDefaultType(typeJson.properties, typeJson.typeParams); - if (defaultNames.length) { - delete typeJson.typeParams; - } - } // 对象类型 if (typeJson.properties && Object.keys(typeJson.properties)) { diff --git a/src/typescript-to-file-datas.ts b/src/typescript-to-file-datas.ts index 2088b1c..0bdebe9 100644 --- a/src/typescript-to-file-datas.ts +++ b/src/typescript-to-file-datas.ts @@ -54,7 +54,7 @@ export default class typescriptToFileDatas { extendJsonData(fileName: string, key: string, data: AnyOption) { this.jsonData[fileName] = this.jsonData[fileName] || {}; - this.jsonData[fileName][key] = data; + this.jsonData[fileName][key] = this.jsonData[fileName][key] || data; } /** From 46f2a9e220eff2fb2afdc2b6b65fd5371ec2205c Mon Sep 17 00:00:00 2001 From: HUxiaoAlinNG Date: Mon, 21 Mar 2022 18:00:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0Pick/Omit?= =?UTF-8?q?=E5=AF=B9type=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest/toolfn.ts | 5 +++ .../__snapshots__/toolfn.test.ts.snap | 34 +++++++++++++++++++ src/__tests__/toolfn.test.ts | 8 +++++ src/get-jsonschema-from-data.ts | 3 ++ 4 files changed, 50 insertions(+) diff --git a/jest/toolfn.ts b/jest/toolfn.ts index 61c364d..64cc8d0 100644 --- a/jest/toolfn.ts +++ b/jest/toolfn.ts @@ -66,3 +66,8 @@ type ToolFn_15 = Record; // interface ToolFn_18 { // name: Record // } + +type Filter = 'a' | 'b'; +type ToolFn_16 = Pick; + +type ToolFn_17 = Omit; \ No newline at end of file diff --git a/src/__tests__/__snapshots__/toolfn.test.ts.snap b/src/__tests__/__snapshots__/toolfn.test.ts.snap index 31f319c..0cf0afa 100644 --- a/src/__tests__/__snapshots__/toolfn.test.ts.snap +++ b/src/__tests__/__snapshots__/toolfn.test.ts.snap @@ -51,6 +51,21 @@ Object { } `; +exports[`工具函数Omit_4 1`] = ` +Object { + "additionalProperties": false, + "properties": Object { + "c": Object { + "type": "boolean", + }, + }, + "required": Array [ + "c", + ], + "type": "object", +} +`; + exports[`工具函数Pick_1 1`] = ` Object { "properties": Object { @@ -80,6 +95,25 @@ Object { } `; +exports[`工具函数Pick_3 1`] = ` +Object { + "additionalProperties": false, + "properties": Object { + "a": Object { + "type": "number", + }, + "b": Object { + "type": "string", + }, + }, + "required": Array [ + "a", + "b", + ], + "type": "object", +} +`; + exports[`工具函数Record_1 1`] = ` Object { "additionalProperties": false, diff --git a/src/__tests__/toolfn.test.ts b/src/__tests__/toolfn.test.ts index 3495efb..fa72ff9 100644 --- a/src/__tests__/toolfn.test.ts +++ b/src/__tests__/toolfn.test.ts @@ -22,6 +22,10 @@ test('工具函数Omit_3', () => { expect(getSchema('ToolFn_3')).toMatchSnapshot(); }); +test('工具函数Omit_4', () => { + expect(getSchema('ToolFn_17')).toMatchSnapshot(); +}); + // test('工具函数Omit_4', () => { // expect(getSchema('ToolFn_4')).toMatchSnapshot(); // }); @@ -50,6 +54,10 @@ test('工具函数Pick_2', () => { expect(getSchema('ToolFn_10')).toMatchSnapshot(); }); +test('工具函数Pick_3', () => { + expect(getSchema('ToolFn_16')).toMatchSnapshot(); +}); + // test('工具函数Pick_3', () => { // expect(getSchema('ToolFn_11')).toMatchSnapshot(); // }); diff --git a/src/get-jsonschema-from-data.ts b/src/get-jsonschema-from-data.ts index fbf1206..585c724 100644 --- a/src/get-jsonschema-from-data.ts +++ b/src/get-jsonschema-from-data.ts @@ -449,6 +449,9 @@ export default class genTypeSchema extends typescriptToFileDatas { if (!extra) return resType; if (resType) { + if (extra && extra.$ref) { + extra = _.cloneDeep(attrCommonHandle(extra, false) as AnyOption); + } const extraKeys = extra.enum || []; if (key === 'Omit') { const res = deleteJsonSchemaKeys(resType, extraKeys);