Skip to content

[BUG] Kotlin code generator should sanitize enum values according to convention #21181

@msnteixeira

Description

@msnteixeira

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 Kotlin code generator is not sanitizing enum values correctly. This occurs for hyphenated values where it gets translated to Minus.

This was previously addressed in Issue #9683 and partially fixed in PR #16267 (since it does not cover enum values). The fix should be quick to address but let me know if you need me to submit a PR for this

openapi-generator version

Version 7.13.0

OpenAPI declaration file content or url

Any OpenAPI file that contains an enum whose value contains an hyphen:

properties:
  propertyName:
    type: string
    default: HYPHENATED-VALUE
    enum:
      - HYPHENATED-VALUE
Steps to reproduce
  1. Go to AbstractKotlinCodegenTest.java
  2. Add an assertion for any enum type. Example: assertEquals(codegen.toEnumVarName("long-Name", null), "longName");
  3. Run the test
  4. Verify that it fails. It should be longName instead of longMinusName
Related issues/PRs

Similar to Issue #9683 and partially fixed in PR #16267

Suggest a fix

Replace line modified = value; with modified = sanitizeName(value, "\\W-[\\$]"); in AbstractKotlinCodegen.java
Add unit tests for the different enum properties. Example:

  1. original -> long-Name becomes longMinusName
  2. snake_case -> long-Name becomes long_name
  3. camelCase -> long-Name becomes longName
  4. PascalCase -> long-Name becomes LongName
  5. UPPERCASE -> long-Name becomes LONG_NAME

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions