Skip to content

Commit

Permalink
fix: 对于数组类型的值, 应始终取其第一个条目的类型作为该数组的类型
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Feb 15, 2022
1 parent 4f0d885 commit e84d523
Show file tree
Hide file tree
Showing 4 changed files with 1,587 additions and 28 deletions.
14 changes: 14 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ export function processJsonSchema(
delete jsonSchema.$ref
delete jsonSchema.$$ref

// 删除 Mockjs.toJSONSchema 引入的键
delete jsonSchema.template
delete jsonSchema.rule
delete jsonSchema.path

// 数组只取第一个判断类型
if (
jsonSchema.type === 'array' &&
Array.isArray(jsonSchema.items) &&
jsonSchema.items.length
) {
jsonSchema.items = jsonSchema.items[0]
}

// 处理类型名称为标准的 JSONSchema 类型名称
if (jsonSchema.type) {
// 类型映射表,键都为小写
Expand Down
39 changes: 35 additions & 4 deletions tests/__mocks__/got.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ const mockData: Record<string, () => any> = {
req_body_other:
'{"type":"object","title":"empty object","properties":{"page":{"type":"number","description":"页码"},"limit":{"type":"number","description":"每页数量"},"keyword":{"type":"string","description":"关键词"}},"required":["page","limit"]}',
markdown: '**备注。。。**\n\n`额鹅鹅鹅`',
desc:
'<p><strong>备注。。。</strong></p>\n<p><code data-backticks="1">额鹅鹅鹅</code></p>\n',
desc: '<p><strong>备注。。。</strong></p>\n<p><code data-backticks="1">额鹅鹅鹅</code></p>\n',
res_body:
'{"type":"object","title":"empty object","properties":{"list":{"type":"array","items":{"type":"object","properties":{"name":{"type":"string","description":"姓名"}},"required":["name"]},"description":"列表"}},"required":["list"]}',
req_body_type: 'json',
Expand Down Expand Up @@ -993,6 +992,39 @@ const mockData: Record<string, () => any> = {
up_time: 1610557430,
__v: 0,
},
{
query_path: {
path: '/mockjs_json5',
params: [],
},
edit_uid: 0,
status: 'undone',
type: 'static',
req_body_is_json_schema: false,
res_body_is_json_schema: false,
api_opened: false,
index: 0,
tag: [],
_id: 1830,
method: 'GET',
catid: 20,
title: 'mockjs+json5',
path: '/mockjs_json5',
project_id: 11,
req_params: [],
res_body_type: 'json',
uid: 11,
add_time: 1644917567,
up_time: 1644917600,
req_query: [],
req_headers: [],
req_body_form: [],
__v: 0,
desc: '',
markdown: '',
res_body:
'{\n "code": 200,\n "data|1-10": [\n {\n "id": "@id", // id\n "name": "@cname", // 名字\n "template|1-10": "*", // 模板\n "string|3": "***", // 遍历三次\n "number|1-100": 200,\n "boolean": "@boolean",\n "image": "@image"\n }\n ]\n}',
},
{
query_path: {
path: '/default_value',
Expand Down Expand Up @@ -1059,8 +1091,7 @@ const mockData: Record<string, () => any> = {
req_query: [],
req_headers: [],
req_body_form: [],
desc:
'<p><a href="https://github.com/fjc0k/yapi-to-typescript/issues/17">https://github.com/fjc0k/yapi-to-typescript/issues/17</a></p>\n',
desc: '<p><a href="https://github.com/fjc0k/yapi-to-typescript/issues/17">https://github.com/fjc0k/yapi-to-typescript/issues/17</a></p>\n',
markdown:
'[https://github.com/fjc0k/yapi-to-typescript/issues/17](https://github.com/fjc0k/yapi-to-typescript/issues/17)',
res_body: '{"type":"object","title":"empty object","properties":{}}',
Expand Down
Loading

0 comments on commit e84d523

Please sign in to comment.