'How can I set a custom value for the @JsonProperty annotation, when generating a client using the kotlin-spring OpenApi generator?
I'm generating an API client from Open API specs (3.0.0), using openapi-generator:5.4.0 and kotlin-spring generator.
The generated data classes for the models have the @JsonProperty annotation. The value of the annotation is the same as the name of the property. I want to have different values for the annotation and the property name.
This is because the specs represent a 3rd party API which does not use meaningful names for its properties. I want to set descriptive names for the properties, and use the 3rd party's names in the @JsonProperty annotation. This way, Json parsing will not fail when I use those models for API calls.
Is there some way to achieve this?
For reference, here's a
sample spec: link to full spec
components:
schemas:
Pet:
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
generated data class:
data class Pet(
@field:JsonProperty("id", required = true) val id: kotlin.Long,
@field:JsonProperty("name", required = true) val name: kotlin.String,
@field:JsonProperty("tag") val tag: kotlin.String? = null
) {
}
and build.gradle.kts file:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.openapitools.generator.gradle.plugin.tasks.GenerateTask
plugins {
kotlin("jvm") version "1.6.20"
id("org.openapi.generator") version "5.3.0"
application
}
group = "io.codextor"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation("org.openapitools:openapi-generator:5.4.0")
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
application {
mainClass.set("MainKt")
}
tasks.withType<GenerateTask> {
generatorName.set("kotlin-spring")
inputSpec.set("$rootDir/specs/petstore-v3.0.yaml")
outputDir.set("$buildDir/generated")
apiPackage.set("org.openapi.example.api")
invokerPackage.set("org.openapi.example.invoker")
modelPackage.set("org.openapi.example.model")
configOptions.set(
mapOf(
"dateLibrary" to "java8"
)
)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
