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] [Gradle Plugin] [Android] Configuration option for mutable/immutable models behaves unexpectedly #9175

Open
stephen-mojo opened this issue Apr 5, 2021 · 0 comments

Comments

@stephen-mojo
Copy link

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

#4115
#3803

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

1 participant