在使用Kotlin-spring OpenAPI Generator生成客户端时,如何为@jsonproperty注释设置自定义值?

发布于 2025-01-24 01:40:40 字数 2270 浏览 2 评论 0原文

我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水般的淡然安静女子 2025-01-31 01:40:40

注释

@field:JsonProperty

如果您希望生成

mapOf(
    "dateLibrary" to "java8",
    "serializationLibrary" to "jackson" 
)

,则需要在build.gradle.kts中修改您的配置为更多此类配置值,请访问 https://github.com/openapitools/openapi-generator/blob/blob/master/master/docs/generators/java.md.md

If you want the annotation

@field:JsonProperty

to be generated, you need to modify your configOptions in the build.gradle.kts to

mapOf(
    "dateLibrary" to "java8",
    "serializationLibrary" to "jackson" 
)

More such config values can be found on https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/java.md

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文