Skip to content

Commit bfed5cb

Browse files
committed
feat: 移除字段名称首尾空格
1 parent 23e3e35 commit bfed5cb

File tree

4 files changed

+1073
-62
lines changed

4 files changed

+1073
-62
lines changed

src/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ export function processJsonSchema<T extends JSONSchema4>(jsonSchema: T): T {
7979
)
8080
}
8181

82+
// 移除字段名称首尾空格
83+
if (jsonSchema.properties) {
84+
forOwn(jsonSchema.properties, (_, prop) => {
85+
const propDef = jsonSchema.properties![prop]
86+
delete jsonSchema.properties![prop]
87+
jsonSchema.properties![(prop as string).trim()] = propDef
88+
})
89+
jsonSchema.required = jsonSchema.required && jsonSchema.required.map(prop => prop.trim())
90+
}
91+
8292
// 继续处理对象的子元素
8393
if (jsonSchema.properties) {
8494
forOwn(jsonSchema.properties, processJsonSchema)
@@ -140,15 +150,15 @@ export function propDefinitionsToJsonSchema(propDefinitions: PropDefinitions): J
140150
required: propDefinitions.reduce<string[]>(
141151
(res, prop) => {
142152
if (prop.required) {
143-
res.push(prop.name.trim())
153+
res.push(prop.name)
144154
}
145155
return res
146156
},
147157
[],
148158
),
149159
properties: propDefinitions.reduce<Exclude<JSONSchema4['properties'], undefined>>(
150160
(res, prop) => {
151-
res[prop.name.trim()] = {
161+
res[prop.name] = {
152162
type: prop.type,
153163
description: prop.comment,
154164
...(prop.type === 'file' as any ? {tsType: FileData.name} : {}),

tests/__mocks__/request-promise-native.ts

Lines changed: 229 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,7 @@ const mockData: Record<string, any> = {
7070
path: '/getMethod',
7171
project_id: 27,
7272
res_body_type: 'json',
73-
req_body_form: [{
74-
required: '0',
75-
_id: '5cbdd1c293bc6574a2de33dd',
76-
name: ' trim ',
77-
example: '2',
78-
desc: '测试 trim 的值',
79-
}],
73+
req_body_form: [],
8074
req_params: [],
8175
req_headers: [],
8276
req_query: [
@@ -94,13 +88,6 @@ const mockData: Record<string, any> = {
9488
example: '2',
9589
desc: 'Y 值',
9690
},
97-
{
98-
required: '0',
99-
_id: '5cbdd1c293bc6574a2de33dc',
100-
name: ' trim ',
101-
example: '2',
102-
desc: '测试 trim 的值',
103-
},
10491
],
10592
markdown: '',
10693
desc: '',
@@ -713,6 +700,234 @@ const mockData: Record<string, any> = {
713700
markdown: '[https://github.com/fjc0k/yapi-to-typescript/issues/17](https://github.com/fjc0k/yapi-to-typescript/issues/17)',
714701
res_body: '{"type":"object","title":"empty object","properties":{}}',
715702
},
703+
{
704+
query_path: {
705+
path: '/28/get',
706+
params: [],
707+
},
708+
edit_uid: 0,
709+
status: 'undone',
710+
type: 'static',
711+
req_body_is_json_schema: true,
712+
res_body_is_json_schema: true,
713+
api_opened: false,
714+
index: 0,
715+
tag: [],
716+
_id: 972,
717+
method: 'GET',
718+
catid: 151,
719+
title: '28: 字段名称含空格 GET',
720+
path: '/28/get',
721+
project_id: 27,
722+
req_params: [],
723+
res_body_type: 'json',
724+
uid: 11,
725+
add_time: 1589249514,
726+
up_time: 1589249547,
727+
req_query: [
728+
{
729+
required: '1',
730+
_id: '5eba060b3a90e2c457f435a8',
731+
name: ' a',
732+
desc: '',
733+
},
734+
{
735+
required: '1',
736+
_id: '5eba060b3a90e2a675f435a7',
737+
name: ' b',
738+
example: '',
739+
desc: '',
740+
},
741+
{
742+
required: '1',
743+
_id: '5eba060b3a90e23035f435a6',
744+
name: 'c ',
745+
example: '',
746+
desc: '',
747+
},
748+
{
749+
required: '1',
750+
_id: '5eba060b3a90e24fc3f435a5',
751+
name: ' d e ',
752+
example: '',
753+
desc: '',
754+
},
755+
],
756+
req_headers: [],
757+
req_body_form: [],
758+
__v: 0,
759+
desc: '',
760+
markdown: '',
761+
res_body: '{"type":"object","title":"title","properties":{}}',
762+
},
763+
{
764+
query_path: {
765+
path: '/28/post/form',
766+
params: [],
767+
},
768+
edit_uid: 0,
769+
status: 'undone',
770+
type: 'static',
771+
req_body_is_json_schema: true,
772+
res_body_is_json_schema: true,
773+
api_opened: false,
774+
index: 0,
775+
tag: [],
776+
_id: 978,
777+
method: 'POST',
778+
catid: 151,
779+
title: '28: 字段名称含空格 POST form',
780+
path: '/28/post/form',
781+
project_id: 27,
782+
req_params: [],
783+
res_body_type: 'json',
784+
req_query: [
785+
{
786+
required: '1',
787+
_id: '5eba069e3a90e203f9f435b9',
788+
name: ' a',
789+
desc: '',
790+
},
791+
{
792+
required: '1',
793+
_id: '5eba069e3a90e2235af435b8',
794+
name: ' b',
795+
example: '',
796+
desc: '',
797+
},
798+
{
799+
required: '1',
800+
_id: '5eba069e3a90e24aabf435b7',
801+
name: 'c ',
802+
example: '',
803+
desc: '',
804+
},
805+
{
806+
required: '1',
807+
_id: '5eba069e3a90e2c36ef435b6',
808+
name: ' d e ',
809+
example: '',
810+
desc: '',
811+
},
812+
],
813+
req_headers: [
814+
{
815+
required: '1',
816+
_id: '5eba069e3a90e2ea69f435ba',
817+
name: 'Content-Type',
818+
value: 'application/x-www-form-urlencoded',
819+
},
820+
],
821+
req_body_form: [
822+
{
823+
required: '1',
824+
_id: '5eba069e3a90e22bd5f435be',
825+
name: 'a ',
826+
type: 'text',
827+
},
828+
{
829+
required: '1',
830+
_id: '5eba069e3a90e27bdef435bd',
831+
name: ' b ',
832+
type: 'text',
833+
example: '',
834+
desc: '',
835+
},
836+
{
837+
required: '1',
838+
_id: '5eba069e3a90e2697bf435bc',
839+
name: ' c',
840+
type: 'text',
841+
example: '',
842+
desc: '',
843+
},
844+
{
845+
required: '1',
846+
_id: '5eba069e3a90e2269ef435bb',
847+
name: 'd e ',
848+
type: 'text',
849+
example: '',
850+
desc: '',
851+
},
852+
],
853+
desc: '',
854+
markdown: '',
855+
res_body: '{"type":"object","title":"title","properties":{}}',
856+
uid: 11,
857+
add_time: 1589249571,
858+
up_time: 1589249694,
859+
__v: 0,
860+
req_body_type: 'form',
861+
},
862+
{
863+
query_path: {
864+
path: '/28/post/json',
865+
params: [],
866+
},
867+
edit_uid: 0,
868+
status: 'undone',
869+
type: 'static',
870+
req_body_is_json_schema: true,
871+
res_body_is_json_schema: true,
872+
api_opened: false,
873+
index: 0,
874+
tag: [],
875+
_id: 984,
876+
method: 'POST',
877+
catid: 151,
878+
title: '28: 字段名称含空格 POST json',
879+
path: '/28/post/json',
880+
project_id: 27,
881+
req_params: [],
882+
res_body_type: 'json',
883+
req_query: [
884+
{
885+
required: '1',
886+
_id: '5eba07063a90e2a0aff435cb',
887+
name: ' a',
888+
desc: '',
889+
},
890+
{
891+
required: '1',
892+
_id: '5eba07063a90e25183f435ca',
893+
name: ' b',
894+
example: '',
895+
desc: '',
896+
},
897+
{
898+
required: '1',
899+
_id: '5eba07063a90e22770f435c9',
900+
name: 'c ',
901+
example: '',
902+
desc: '',
903+
},
904+
{
905+
required: '1',
906+
_id: '5eba07063a90e2377cf435c8',
907+
name: ' d e ',
908+
example: '',
909+
desc: '',
910+
},
911+
],
912+
req_headers: [
913+
{
914+
required: '1',
915+
_id: '5eba07063a90e29219f435cc',
916+
name: 'Content-Type',
917+
value: 'application/json',
918+
},
919+
],
920+
req_body_form: [],
921+
desc: '',
922+
markdown: '',
923+
res_body: '{"$schema":"http://json-schema.org/draft-04/schema#","type":"object","properties":{" a":{"type":"number"},"b ":{"type":"string"},"c":{"type":"object","properties":{"d ":{"type":"string"}}}}}',
924+
req_body_type: 'json',
925+
uid: 11,
926+
add_time: 1589249698,
927+
up_time: 1589249798,
928+
__v: 0,
929+
req_body_other: '{"type":"object","title":"title","properties":{" a":{"type":"string"},"b ":{"type":"string"},"c d ":{"type":"string"},"e":{"type":"object","properties":{"f ":{"type":"string"}},"required":["f "]}},"required":[" a","b ","c d ","e"]}',
930+
},
716931
],
717932
},
718933
],

0 commit comments

Comments
 (0)