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/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/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/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 e8eb19b51..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" @@ -965,7 +969,8 @@ "Warning", "Error", "'Single Quote'", - "\"Double Quotes\"" + "\"Double Quotes\"", + "你好" ] }, "EnumWithNumbers": { diff --git a/test/spec/v3.json b/test/spec/v3.json index cb590d0b7..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" @@ -1561,7 +1565,8 @@ "Warning", "Error", "'Single Quote'", - "\"Double Quotes\"" + "\"Double Quotes\"", + "你好" ] }, "EnumWithNumbers": {