Skip to content

[BUG][KOTLIN] When using fields with reserved kotlin names and type enum, generated code contains "`" instead of backtick #11148

@piotrplazienski

Description

@piotrplazienski
Description

When generating kotlin code using "kotlin-spring" generator, and one of the fields in API specs is string with enum type:

Issue:
  type: object
  properties:
    operator:
      type: string
      enum: [ One, Two ]

generated code contains backtick "`" encoded as HTML entity: `:

@field:JsonProperty("operator") val `operator`: Issue.`Operator`? = null,

Obviously, compiler has issue with that :)

openapi-generator version

5.3.0

Not regresion, I have checked 5.2.0 and 4.3.1 and all suffer from same issue. But 5.3.0 has expanded keyword list and operator and value (which we use) are escaped now.
For basic fun keyword it fails the same way in all versions.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Kotlin Issue
  version: "1.0"
paths:
  /issue:
    get:
      tags:
      - Issue
      responses:
        '200':
          description: Issue
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Issue'
components:
  schemas:
    Issue:
      type: object
      properties:
        ok:
          type: string
          enum: [ One, Two ]
        operator:
          type: string
          enum: [ One, Two ]
        fun:
          type: string
          enum: [ One, Two ]
        value:
          type: string
          enum: [ One, Two ]
Command line used for generation

gradle plugin with options:

    configOptions.putAll(mapOf(
        "library" to "spring-boot",
        "serializationLibrary" to "jackson",
        "interfaceOnly" to "true",
        "delegatePattern" to "true",
        "useBeanValidation" to "true",
        "sourceFolder" to "src/main/kotlin",
        "enumPropertyNaming" to "original"
    ))
Steps to reproduce

generate code and open Issue.kt, it will be like:

data class Issue(
    @field:JsonProperty("ok") val ok: Issue.Ok? = null,
    @field:JsonProperty("operator") val `operator`: Issue.`Operator`? = null,
/* snip */
) {
enum class `Operator`(val value: kotlin.String) {
        @JsonProperty("One") One("One"),
        @JsonProperty("Two") Two("Two");
    }
}
Related issues/PRs
Suggest a fix/enhancement

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions