OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Kotlin] [Gradle Plugin] [Android] Configuration option for mutable/immutable models behaves unexpectedly

Open
#9,175 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

I am using the OpenApiGenerator via the Gradle plugin in my Kotlin Android project. I noticed an issue where the configuration value for mutable model attributes is not working as expected.

According to the documentation for the Kotlin generator, a configuration option for mutableModel can be set. The description of this option is simply, "Create mutable models" and the default value is false.
https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/kotlin.md

I have noticed that in my particular case, setting any value results in mutable models. The lack of a value all together is what actually results in immutable models.

For example, if I set mutableModel to either true or false, the models generated are made up of all var attributes:

@Parcelize
@JsonClass(generateAdapter = true)
data class UserDto (
    @Json(name = "id")
    var id: kotlin.String,
    @Json(name = "email")
    var email: kotlin.String,
    @Json(name = "isVerified")
    var isVerified: kotlin.Boolean,
    @Json(name = "givenName")
    var givenName: kotlin.String,
    @Json(name = "familyName")
    var familyName: kotlin.String
) : Parcelable

If I leave the configuration value off completely, attributes default to val:

@Parcelize
@JsonClass(generateAdapter = true)
data class UserDto (
    @Json(name = "id")
    val id: kotlin.String,
    @Json(name = "email")
    val email: kotlin.String,
    @Json(name = "isVerified")
    val isVerified: kotlin.Boolean,
    @Json(name = "givenName")
    val givenName: kotlin.String,
    @Json(name = "familyName")
    val familyName: kotlin.String
) : Parcelable
My Configuration

build.gradle

classpath "org.openapitools:openapi-generator-gradle-plugin:5.1.0"

App-level build.gradle:

openApiGenerate {
    generatorName = "kotlin"
    inputSpec = "$projectDir/openapi-spec.json"
    ignoreFileOverride = "$projectDir/.openapi-generator-ignore"
    outputDir = "$buildDir/generated/openapi"
    apiPackage = "com.my.example.api.service"
    modelPackage = "com.my.example.api.model"
    configFile = "$projectDir/openapi-generator-config.json"
}

openapi-generator-config.json

{
  "apiSuffix": "Service",
  "collectionType": "list",
  "enumPropertyNaming": "UPPERCASE",
  "library": "jvm-retrofit2",
  "modelMutable": "false",
  "moshiCodeGen": "true",
  "packageName": "com.my.example.api",
  "parcelizeModels": "true",
  "sortModelPropertiesByRequiredFlag": "true",
  "sortParamsByRequiredFlag": "true",
  "useCoroutines": "true",
  "typeMappings": {
    "java.net.URI": "kotlin.String"
  }
}
Related Tickets

https://github.com/OpenAPITools/openapi-generator/pull/4115
https://github.com/OpenAPITools/openapi-generator/issues/3803

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 with docs/generators/kotlin.md and the Gradle plugin configuration shown in build.gradle and openapi-generator-config.json. Reproduce the generated Kotlin models with the option enabled, disabled, and omitted, then compare the documented mutableModel name with the supplied configuration. Done means true and false produce distinct documented behavior and the configuration documentation matches the implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin, openapi
Domain
build-system, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.