[BUG] [Kotlin] [Gradle Plugin] [Android] Configuration option for mutable/immutable models behaves unexpectedly
Created by: stephen-mojo
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