From e0e419e21a3ad4b7855bc2e1933e0809c9d391c5 Mon Sep 17 00:00:00 2001 From: HUxiaoAlinNG Date: Tue, 22 Mar 2022 17:45:43 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=E5=88=A0=E6=8E=89default?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From a7fc2721988e50a653e2cfa12f09c10cde4cb59d Mon Sep 17 00:00:00 2001 From: HUxiaoAlinNG Date: Thu, 12 May 2022 18:49:40 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=9C=AC?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=9F=A5=E6=89=BE=E5=BC=95=E7=94=A8=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=EF=BC=8C=E5=88=A0=E9=99=A4=E6=9E=9A=E4=B8=BE=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=97=B6=E7=9A=84=E7=A9=BAdefinitions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/__snapshots__/tstype.test.ts.snap | 1 - src/get-jsonschema-from-data.ts | 7 ++++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/__tests__/__snapshots__/tstype.test.ts.snap b/src/__tests__/__snapshots__/tstype.test.ts.snap index 3afbabd..5582a84 100644 --- a/src/__tests__/__snapshots__/tstype.test.ts.snap +++ b/src/__tests__/__snapshots__/tstype.test.ts.snap @@ -178,7 +178,6 @@ Object { exports[`type类型_10 1`] = ` Object { - "definitions": Object {}, "enum": Array [ 1, 2, diff --git a/src/get-jsonschema-from-data.ts b/src/get-jsonschema-from-data.ts index ff31061..453ce24 100644 --- a/src/get-jsonschema-from-data.ts +++ b/src/get-jsonschema-from-data.ts @@ -326,7 +326,8 @@ export default class genTypeSchema extends typescriptToFileDatas { item.$ref = `#/definitions/${$refKey}`; - if (!$refJson.$ref) { + // fileJson[$refJson.$ref.replace(/#(\/definitions\/)?/, '')] 表示在本文件就能找到 + if (!$refJson.$ref || fileJson[$refJson.$ref.replace(/#(\/definitions\/)?/, '')]) { // 有继承 if ($refJson.extends) { $refJson = handleExtends($refJson); @@ -625,6 +626,10 @@ export default class genTypeSchema extends typescriptToFileDatas { const res = definitions[key] || { definitions: {} }; delete typeJson_.definitions[key]; res.definitions = { ...res.definitions, ...typeJson_.definitions }; + // 枚举类型且 definitions 为空,需删掉 definitions + if (res.enum && !Object.keys(res.definitions).length) { + delete res.definitions; + } typeJson = res; } };