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

[BUG][Kotlin] Enum values should not be escaped/manipulated #18107

Closed
5 of 6 tasks
tajobe opened this issue Mar 14, 2024 · 2 comments
Closed
5 of 6 tasks

[BUG][Kotlin] Enum values should not be escaped/manipulated #18107

tajobe opened this issue Mar 14, 2024 · 2 comments

Comments

@tajobe
Copy link
Contributor

tajobe commented Mar 14, 2024

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The generator takes enum values and creates "safe" names to wrap the "raw" values, but is also escaping the values.

This means I am unable to produce the enum values I need, for example:

openapi: 3.0.3
info:
  title: "manifest"
  description: "manifest"
  version: "0.0.1"
paths: {}
components:
  schemas:
    someEnum:
      type: string
      enum:
        - mything/*
        - mything/1
        - mything/2

The enum is generated with:

    star("mything/_*"),

such that the value is mything/_* rather than mything/* as desired.

This seems very similar to #12457, which was fixed via #12469 but only for that limited case.

I believe the same thing applies to other generators, though I am likely missing the underlying reasoning behind mutating the provided values.

openapi-generator version

7.1.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: "manifest"
  description: "manifest"
  version: "0.0.1"
paths: {}
components:
  schemas:
    someEnum:
      type: string
      enum:
        - mything/*
        - mything/1
        - mything/2
Generation Details

Generated above as kotlin via gradle plugin or cli, eg:

openapi-generator generate -g kotlin -o issue-oas -i ~/tmp/issueOAS.yml
Steps to reproduce
  1. Generate kotlin models for the above sample
  2. Check enum
  3. We see the value as mything/_* rather than mything/* as desired.
Related issues/PRs

#12457 is similar and "fixed" for that specific case

Suggest a fix

Do not escape/sanitize quoted enum values, eg

diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java
index 83843142ae2..9e46d916a4f 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java
@@ -903,7 +903,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
         } else if ("kotlin.Float".equals(datatype)) {
             return value + "f";
         } else {
-            return "\"" + escapeText(value) + "\"";
+            return "\"" + value + "\"";
         }
     }
 
diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java
index 374d1d08e61..b820edc3384 100644
--- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java
+++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java
@@ -60,6 +60,7 @@ public class AbstractKotlinCodegenTest {
         assertEquals(codegen.toEnumVarName("long Name", null), "long_Name");
         assertEquals(codegen.toEnumVarName("1long Name", null), "_1long_Name");
         assertEquals(codegen.toEnumVarName("not1long Name", null), "not1long_Name");
+        assertEquals(codegen.toEnumVarName("data/*", null), "dataSlashStar");
     }
     @Test
     public void pascalCaseEnumConverter() {
@@ -78,6 +79,7 @@ public class AbstractKotlinCodegenTest {
         assertEquals(codegen.toEnumValue("5", "kotlin.Float"), "5f");
         assertEquals(codegen.toEnumValue("1.0", "kotlin.Float"), "1.0f");
         assertEquals(codegen.toEnumValue("data", "Something"), "\"data\"");
+        assertEquals(codegen.toEnumValue("data/*", "Something"), "\"data/*\"");
     }
 
     private static class P_AbstractKotlinCodegen extends AbstractKotlinCodegen {
@wing328
Copy link
Member

wing328 commented Mar 19, 2024

can you please file a PR with the suggested fix when you've time?

@tajobe
Copy link
Contributor Author

tajobe commented Apr 8, 2024

Should be fixed in 18305, closing.

@tajobe tajobe closed this as completed Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants