From e65de8bd80e34e507dbf3b783748ec73983ffe86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E8=B6=85?= <408493323@qq.com> Date: Tue, 28 Nov 2023 15:16:53 +0800 Subject: [PATCH 1/2] fix: support chinese for enum --- src/openApi/v2/parser/getEnum.ts | 2 +- src/openApi/v3/parser/getEnum.ts | 2 +- test/spec/v2.json | 3 ++- test/spec/v3.json | 4 +++- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/openApi/v2/parser/getEnum.ts b/src/openApi/v2/parser/getEnum.ts index 64c7ca8b5..23a286a6e 100644 --- a/src/openApi/v2/parser/getEnum.ts +++ b/src/openApi/v2/parser/getEnum.ts @@ -20,7 +20,7 @@ export const getEnum = (values?: (string | number)[]): Enum[] => { } return { name: String(value) - .replace(/\W+/g, '_') + .replace(/[^a-zA-Z0-9_\u4e00-\u9fa5]+/g, '_') .replace(/^(\d+)/g, '_$1') .replace(/([a-z])([A-Z]+)/g, '$1_$2') .toUpperCase(), diff --git a/src/openApi/v3/parser/getEnum.ts b/src/openApi/v3/parser/getEnum.ts index 64c7ca8b5..23a286a6e 100644 --- a/src/openApi/v3/parser/getEnum.ts +++ b/src/openApi/v3/parser/getEnum.ts @@ -20,7 +20,7 @@ export const getEnum = (values?: (string | number)[]): Enum[] => { } return { name: String(value) - .replace(/\W+/g, '_') + .replace(/[^a-zA-Z0-9_\u4e00-\u9fa5]+/g, '_') .replace(/^(\d+)/g, '_$1') .replace(/([a-z])([A-Z]+)/g, '$1_$2') .toUpperCase(), diff --git a/test/spec/v2.json b/test/spec/v2.json index e8eb19b51..a54f0004a 100644 --- a/test/spec/v2.json +++ b/test/spec/v2.json @@ -965,7 +965,8 @@ "Warning", "Error", "'Single Quote'", - "\"Double Quotes\"" + "\"Double Quotes\"", + "你好" ] }, "EnumWithNumbers": { diff --git a/test/spec/v3.json b/test/spec/v3.json index cb590d0b7..6c4d14206 100644 --- a/test/spec/v3.json +++ b/test/spec/v3.json @@ -1561,9 +1561,11 @@ "Warning", "Error", "'Single Quote'", - "\"Double Quotes\"" + "\"Double Quotes\"", + "你好" ] }, + "EnumWithNumbers": { "description": "This is a simple enum with numbers", "enum": [ From 1c6a729884e7827108742303d2537bdf7fbd6c50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E8=B6=85?= <408493323@qq.com> Date: Wed, 6 Dec 2023 15:33:47 +0800 Subject: [PATCH 2/2] fix: support chinese for model name --- src/openApi/v2/parser/getType.ts | 2 +- src/openApi/v3/parser/getType.ts | 2 +- test/spec/v2.json | 4 ++++ test/spec/v3.json | 5 ++++- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/openApi/v2/parser/getType.ts b/src/openApi/v2/parser/getType.ts index 6caa1e015..c925b2921 100644 --- a/src/openApi/v2/parser/getType.ts +++ b/src/openApi/v2/parser/getType.ts @@ -3,7 +3,7 @@ import { getMappedType } from './getMappedType'; import { stripNamespace } from './stripNamespace'; const encode = (value: string): string => { - return value.replace(/^[^a-zA-Z_$]+/g, '').replace(/[^\w$]+/g, '_'); + return value.replace(/^[^a-zA-Z_$]+/g, '').replace(/[^a-zA-Z0-9_\u4e00-\u9fa5$]+/g, '_'); }; /** diff --git a/src/openApi/v3/parser/getType.ts b/src/openApi/v3/parser/getType.ts index e8ef4733d..1922bafc7 100644 --- a/src/openApi/v3/parser/getType.ts +++ b/src/openApi/v3/parser/getType.ts @@ -4,7 +4,7 @@ import { getMappedType } from './getMappedType'; import { stripNamespace } from './stripNamespace'; const encode = (value: string): string => { - return value.replace(/^[^a-zA-Z_$]+/g, '').replace(/[^\w$]+/g, '_'); + return value.replace(/^[^a-zA-Z_$]+/g, '').replace(/[^a-zA-Z0-9_\u4e00-\u9fa5$]+/g, '_'); }; /** diff --git a/test/spec/v2.json b/test/spec/v2.json index a54f0004a..54b6ce8e3 100644 --- a/test/spec/v2.json +++ b/test/spec/v2.json @@ -944,6 +944,10 @@ "description": "This is a simple string", "type": "string" }, + "SimpleString_字符串": { + "description": "This is a simple string with chinese", + "type": "string" + }, "SimpleFile": { "description": "This is a simple file", "type": "file" diff --git a/test/spec/v3.json b/test/spec/v3.json index 6c4d14206..d37a484a8 100644 --- a/test/spec/v3.json +++ b/test/spec/v3.json @@ -1539,6 +1539,10 @@ "description": "This is a simple string", "type": "string" }, + "SimpleString_字符串": { + "description": "This is a simple string with chinese", + "type": "string" + }, "SimpleFile": { "description": "This is a simple file", "type": "file" @@ -1565,7 +1569,6 @@ "你好" ] }, - "EnumWithNumbers": { "description": "This is a simple enum with numbers", "enum": [