From 64d2ff626dbb0b33593b6e0c890a8d1026c155a0 Mon Sep 17 00:00:00 2001 From: HUxiaoAlinNG Date: Sun, 13 Mar 2022 01:15:03 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E9=81=BF=E5=85=8D=E5=8E=9F=E6=9C=89?= =?UTF-8?q?definitions=E8=A2=AB=E8=A6=86=E7=9B=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/get-jsonschema-from-data.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/get-jsonschema-from-data.ts b/src/get-jsonschema-from-data.ts index 1904658..8cb9694 100644 --- a/src/get-jsonschema-from-data.ts +++ b/src/get-jsonschema-from-data.ts @@ -240,7 +240,7 @@ export default class genTypeSchema extends typescriptToFileDatas { const item = result.properties[key] || {}; if (item.$ref) { const name = item.$ref.replace('#', ''); - const ref = fileJson[name]; + let ref = fileJson[name]; if (ref && ref.$realRef && ref.$ref && ref.items && ref.items.extraParams) { item.$ref = ref.$ref; item.$realRef = ref.$realRef; @@ -534,7 +534,12 @@ export default class genTypeSchema extends typescriptToFileDatas { res && Object.assign(item, res); } else if (typeof item === 'object') { delete item.items; - attrCommonHandle(item); + let handle = true; + const $refKey = item.$ref.replace(/#(\/definitions\/)?/, ''); + if (typeJson.definitions && Object.keys(typeJson.definitions[$refKey] || {}).length) { + handle = false; + } + attrCommonHandle(item, handle); } }; @@ -567,7 +572,7 @@ export default class genTypeSchema extends typescriptToFileDatas { const { definitions, $ref } = typeJson_; if ($ref && definitions) { const key = $ref.replace(/#\/definitions\//, ''); - const res = definitions[key]; + const res = definitions[key] || { definitions: {} }; delete typeJson_.definitions[key]; res.definitions = { ...res.definitions, ...typeJson_.definitions }; typeJson = res; From 265ad1e029d96c5ae16257e35e5f68bf54aca6d1 Mon Sep 17 00:00:00 2001 From: HUxiaoAlinNG Date: Sun, 13 Mar 2022 03:14:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E5=88=A0=E9=99=A4=E9=A2=9D?= =?UTF-8?q?=E5=A4=96=E5=B1=9E=E6=80=A7,=E5=A4=84=E7=90=86type=E4=B8=BA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E6=83=85=E5=86=B5=EF=BC=8C=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest/tstype.ts | 2 + .../__snapshots__/generic.test.ts.snap | 24 ----------- .../__snapshots__/tstype.test.ts.snap | 42 +++++++++++++++++++ src/__tests__/tstype.test.ts | 5 +++ src/get-jsonschema-from-data.ts | 13 +++++- 5 files changed, 61 insertions(+), 25 deletions(-) diff --git a/jest/tstype.ts b/jest/tstype.ts index 2ef514a..7c6113b 100644 --- a/jest/tstype.ts +++ b/jest/tstype.ts @@ -30,3 +30,5 @@ type Type_11 = { name: Label }; type Type_12 = AAA[]; type Type_13 = {}; + +type Type_14 = { id: string; name: string; children: AAA[] }[]; \ 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 5817b1f..49862af 100644 --- a/src/__tests__/__snapshots__/generic.test.ts.snap +++ b/src/__tests__/__snapshots__/generic.test.ts.snap @@ -202,14 +202,6 @@ Object { "value", ], "type": "object", - "typeParams": Object { - "0": Object { - "default": Object { - "type": "string", - }, - "name": "T", - }, - }, }, }, "properties": Object { @@ -243,21 +235,5 @@ Object { "value", ], "type": "object", - "typeParams": Object { - "0": Object { - "default": Object { - "$realRef": "Generic_8", - "$ref": "#Generic_8", - "items": Object { - "extraParams": Array [ - Object { - "type": "string", - }, - ], - }, - }, - "name": "T", - }, - }, } `; diff --git a/src/__tests__/__snapshots__/tstype.test.ts.snap b/src/__tests__/__snapshots__/tstype.test.ts.snap index f9741d1..3afbabd 100644 --- a/src/__tests__/__snapshots__/tstype.test.ts.snap +++ b/src/__tests__/__snapshots__/tstype.test.ts.snap @@ -235,6 +235,48 @@ Object { } `; +exports[`type类型_数组_2 1`] = ` +Object { + "definitions": Object { + "AAA": Object { + "additionalProperties": false, + "properties": Object { + "other1": Object { + "type": "string", + }, + }, + "required": Array [ + "other1", + ], + "type": "object", + }, + }, + "items": Object { + "properties": Object { + "children": Object { + "items": Object { + "$ref": "#/definitions/AAA", + }, + "type": "array", + }, + "id": Object { + "type": "string", + }, + "name": Object { + "type": "string", + }, + }, + "required": Array [ + "id", + "name", + "children", + ], + "type": "object", + }, + "type": "array", +} +`; + exports[`type类型_空对象_1 1`] = ` Object { "type": "object", diff --git a/src/__tests__/tstype.test.ts b/src/__tests__/tstype.test.ts index 23c671d..5c87606 100644 --- a/src/__tests__/tstype.test.ts +++ b/src/__tests__/tstype.test.ts @@ -49,3 +49,8 @@ test('type类型_数组_1', () => { test('type类型_空对象_1', () => { expect(getSchema('Type_13')).toMatchSnapshot(); }); + +test('type类型_数组_2', () => { + expect(getSchema('Type_14')).toMatchSnapshot(); +}); + diff --git a/src/get-jsonschema-from-data.ts b/src/get-jsonschema-from-data.ts index 8cb9694..65d61de 100644 --- a/src/get-jsonschema-from-data.ts +++ b/src/get-jsonschema-from-data.ts @@ -125,6 +125,7 @@ export default class genTypeSchema extends typescriptToFileDatas { if (!result) return; const definitions_ = _.get(result, 'definitions') || {}; delete result.definitions; + delete result.typeParams; const definitions = { [$refKey]: result, ...definitions_ }; typeJson.definitions = { ...typeJson.definitions, ...definitions }; }; @@ -358,6 +359,13 @@ export default class genTypeSchema extends typescriptToFileDatas { const commonArrayHandle = (typeJson: AnyOption) => { if (_.get(typeJson, 'items') && _.get(typeJson, 'items.$ref')) { commonRefHandle(typeJson.items); + } else if (_.get(typeJson, 'items') && _.get(typeJson, 'items.properties')) { + const properties = typeJson.items.properties || {}; + for (const key in properties) { + if (properties[key].type === 'array') { + commonArrayHandle(properties[key]); + } + } } else { const itemArr = _.get(typeJson, 'allOf') || @@ -600,7 +608,10 @@ export default class genTypeSchema extends typescriptToFileDatas { // 处理默认值 if (typeJson.typeParams) { - handleGenericDefaultType(typeJson.properties, typeJson.typeParams); + const defaultNames = handleGenericDefaultType(typeJson.properties, typeJson.typeParams); + if (defaultNames.length) { + delete typeJson.typeParams; + } } // 对象类型