Skip to content

[BUG] [Kotlin] Class fields are generated with immutable 'val' instead of 'var' #3803

@vitaliysapounov

Description

@vitaliysapounov

Description

Kotlin generator generates Kotlin classes with 'val' fields, not 'var'. Thus, the fields are constant and cannot be changed by a programmer -- you cannot even an instance programmatically.

openapi-generator version

Generator Version 4.1.1
Spec Version 3.0.2

OpenAPI declaration file content or url
openapi: "3.0.2"

info:
  version: "1.0.0"
  title: Testing Kotlin generation

# We only care about schemas, but this prevents codegen error: "attribute paths is missing"
paths:
  /dummy:
    get:
      responses:
        '200':
          description: OK

components:
  schemas:

    TestRec:
      properties:
        id:
          type: integer
          description: >
            Unique id.
          minLength: 1
          example: 1
        label:
          type: string
          description: >
            User-friendly name.
          example: "North"
        userId:
          type: string
          description: >
            User Id.
          minLength: 38
          example: "Test1234"
Command line used for generation

openapi-generator generate -i my-models.yml -g kotlin

NOTE: we actually use the Gradle plugin for code generation (org.openapi.generator version 4.1.1), but the above command-line tool gives the same result. However, we would like the fix to propagate to the Gradle plugin as well, so we can use in our build.

Steps to reproduce

Run the command above and inspect the generated TestRec.kt. It will look like below:

package org.openapitools.client.models


import com.squareup.moshi.Json

data class TestRec (
    /* Unique id.  */
    @Json(name = "id")
    val id: kotlin.Int? = null,
    /* User-friendly name.  */
    @Json(name = "label")
    val label: kotlin.String? = null,
    /* User Id.  */
    @Json(name = "userId")
    val userId: kotlin.String? = null
) {

}

Note val is used, not var.

Related issues/PRs

Not found.

Suggest a fix

Generate Kotlin fields with 'var' instead of 'val'.

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