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; } };