Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support chinese for enum #1913

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/openApi/v2/parser/getEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '_')
Copy link

@PetrusAsikainen PetrusAsikainen Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the name is used as an identifier, the correct characters to include would be the ones with the ID_Start or ID_Continue Unicode property. The CJK characters in the BMP with ID_Start are currently \u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAD9.

It could also make sense to already allow all characters with the correct Unicode properties, though that would probably require a dependency to pull off.

.replace(/^(\d+)/g, '_$1')
.replace(/([a-z])([A-Z]+)/g, '$1_$2')
.toUpperCase(),
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v2/parser/getType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '_');
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/parser/getEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/openApi/v3/parser/getType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '_');
};

/**
Expand Down
7 changes: 6 additions & 1 deletion test/spec/v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -965,7 +969,8 @@
"Warning",
"Error",
"'Single Quote'",
"\"Double Quotes\""
"\"Double Quotes\"",
"你好"
]
},
"EnumWithNumbers": {
Expand Down
7 changes: 6 additions & 1 deletion test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -1561,7 +1565,8 @@
"Warning",
"Error",
"'Single Quote'",
"\"Double Quotes\""
"\"Double Quotes\"",
"你好"
]
},
"EnumWithNumbers": {
Expand Down