在使用Kotlin-spring OpenAPI Generator生成客户端时,如何为@jsonproperty注释设置自定义值?
我使用OpenAPI-Generator:5.4.0和Kotlin-Spring Generator生成了Open API规格(3.0.0)的API客户端。
模型生成的数据类具有@jsonproperty注释。注释的值与属性名称相同。我想为注释和属性名称具有不同的值。
这是因为规格代表第三方API,该API并未使用有意义的属性名称。我想为属性设置描述性名称,并在@jsonproperty注释中使用第三方的名称。这样,当我将这些模型用于API调用时,JSON解析就不会失败。
有什么方法可以实现这一目标吗?
供参考,这是一个
示例规格:链接到完整规格
components:
schemas:
Pet:
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
生成的数据类:
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
) {
}
build.gradle.kts文件:
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"
)
)
}
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"
)
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注释
如果您希望生成
,则需要在build.gradle.kts中修改您的配置为更多此类配置值,请访问 https://github.com/openapitools/openapi-generator/blob/blob/master/master/docs/generators/java.md.md
If you want the annotation
to be generated, you need to modify your configOptions in the build.gradle.kts to
More such config values can be found on https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/java.md