Skip to content

Commit

Permalink
fix(类型引用): 修复引用根级类型报错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Mar 14, 2022
1 parent 467c7a7 commit d8e93d4
Show file tree
Hide file tree
Showing 4 changed files with 547 additions and 528 deletions.
14 changes: 8 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,14 @@ export function jsonSchemaToJSTTJsonSchema(
.replace(/^[a-z]+:/i, ''),
)
const typeAbsolutePathArr = typeAbsolutePath.split('/').filter(Boolean)
const tsType = `${repeat(
'NonNullable<',
typeAbsolutePathArr.length,
)}${typeName}[${typeAbsolutePathArr
.map(v => JSON.stringify(v))
.join(']>[')}]>`
const tsType = !typeAbsolutePathArr.length
? typeName
: `${repeat(
'NonNullable<',
typeAbsolutePathArr.length,
)}${typeName}[${typeAbsolutePathArr
.map(v => JSON.stringify(v))
.join(']>[')}]>`
jsonSchema.tsType = tsType
}

Expand Down
10 changes: 5 additions & 5 deletions tests/__mocks__/got.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ const mockData: Record<string, () => any> = {
},
{
query_path: {
path: '/recursion',
path: '/ref_type',
params: [],
},
edit_uid: 0,
Expand All @@ -1074,22 +1074,22 @@ const mockData: Record<string, () => any> = {
_id: 1832,
method: 'GET',
catid: 20,
title: '递归',
path: '/recursion',
title: '类型引用',
path: '/ref_type',
project_id: 11,
req_params: [],
res_body_type: 'json',
uid: 11,
add_time: 1645844560,
up_time: 1645849095,
up_time: 1647247632,
req_query: [],
req_headers: [],
req_body_form: [],
__v: 0,
desc: '',
markdown: '',
res_body:
'{"type":"object","title":"title","properties":{"list":{"type":"array","items":{"type":"object","properties":{"label":{"type":"string"},"value":{"type":"string"},"children":{"type":"array","items":{"type":"object","properties":{}},"title":"&.."},"id":{"type":"number"},"parent":{"type":"string","title":"&."}},"required":["label","value","id"]}},"list2":{"type":"array","items":{"type":"object","properties":{}},"title":"&./list"},"obj":{"type":"array","items":{"type":"object","properties":{"list3":{"type":"array","items":{"type":"object","properties":{}},"title":"&/list"},"name":{"type":"number"},"name2":{"type":"string","title":"&./name"},"id2":{"type":"string","title":"&/list/0/id"}},"required":["list3","name","name2","id2"]}}},"required":["list","list2","obj"]}',
'{"type":"object","title":"title","properties":{"list":{"type":"array","items":{"type":"object","properties":{"label":{"type":"string"},"value":{"type":"string"},"children":{"type":"array","items":{"type":"object","properties":{}},"title":"&.."},"id":{"type":"number"},"parent":{"type":"string","title":"&."}},"required":["label","value","id"]}},"list2":{"type":"array","items":{"type":"object","properties":{}},"title":"&./list"},"obj":{"type":"array","items":{"type":"object","properties":{"list3":{"type":"array","items":{"type":"object","properties":{}},"title":"&/list"},"name":{"type":"number"},"name2":{"type":"string","title":"&./name"},"id2":{"type":"string","title":"&/list/0/id"}},"required":["list3","name","name2","id2"]}},"root":{"type":"string","title":"&/"}},"required":["list","list2","obj","root"]}',
},
{
query_path: {
Expand Down
Loading

0 comments on commit d8e93d4

Please sign in to comment.