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] 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": [