swagger-api / swagger-api/swagger-codegen

allowUnicodeIdentifiers option not supported for Java enum var names

Open
#6,621 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Having enum strings that contain only not-latin characters results in a broken java code where all enum names are made of a single underscore character. There is an allowUnicodeIdentifiers option, but it is not working in this case.

Part of swagger.json:

        "form": {
          "type": "string",
          "enum": [
            "Государственная",
            "Муниципальная",
            "Иная"
          ],
          "description": "Форма собственности"
        },

Generated Java code:

  public enum FormEnum {
    _ ("Государственная"),    
    _ ("Муниципальная"),    
    _ ("Иная");
Swagger-codegen version

2.2.1
master

Swagger declaration file content or url

https://gist.github.com/afrolovsky/667098bb49863fa5592e68969b923a2d

Command line used for generation
java -jar swagger-codegen-cli-2.2.1.jar generate ^
  --input-spec "swagger.json" ^
  --lang java ^
  --additional-properties dateLibrary=java8 ^
  --additional-properties allowUnicodeIdentifiers=true ^
  --output temp
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement
diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java
index a83acff..809ea8f 100644
--- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java
+++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java
@@ -990,7 +990,12 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
         }
 
         // string
-        String var = value.replaceAll("\\W+", "_").toUpperCase();
+        String var;
+        if (allowUnicodeIdentifiers) {
+            var = Pattern.compile("\\W+", Pattern.UNICODE_CHARACTER_CLASS).matcher(value).replaceAll("_").toUpperCase();
+        } else {
+            var = value.replaceAll("\\W+", "_").toUpperCase();
+        }
         if (var.matches("\\d.*")) {
             return "_" + var;
         } else {

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java, at the enum variable-name conversion shown in the issue. Run the provided generation command with the linked Swagger declaration and allowUnicodeIdentifiers enabled; done means Unicode-only enum values produce distinct valid Java enum identifiers instead of repeated underscores.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.