OpenAPITools / OpenAPITools/openapi-generator

[BUG][KOTLIN] Kotlin generator emits @get:JsonProperty causing Jackson deserialization to fail in some cases

Open
#22,067 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When generating Kotlin models with serializationLibrary=jackson, the generator uses @get:JsonProperty.
This causes Jackson to fail deserialization for Kotlin data classes in some cases, because the annotation is placed on the getter instead of the constructor parameter or backing field.

Jackson’s Kotlin module binds via constructor parameters, so @get: is effectively ignored. Jackson does have some magic of it's own, so usually it will work out these things itself. In my specific case it is sadly not so.

openapi-generator version

7.16.0, probably earlier versions too

OpenAPI declaration file content
components:
  schemas:
    MyRestModel:
      type: object
      properties:
        t_start:
          type: string
          format: date-time
Generation Details
openapi-generator generate \
  -i openapi.yaml \
  -g kotlin \
  -o generated \
  --library jvm-spring-restclient \
  --additional-properties=serializationLibrary=jackson,useSpringBoot3=true
Steps to reproduce
  1. Run CLI command
  2. Inspect generated model:

Generated model snippet:

data class MyRestModel(
    @get:JsonProperty("t_start")
    val tStart: java.time.OffsetDateTime? = null,
)
Suggest a fix

Generator should use either @param: or @field: for Jackson in Kotlin, or provide a config option to choose the annotation target. I can see that the kotlin-wiremock combo already uses @param.

Perhaps some of the other Kotlin serialization libraries would benefit from this as well.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided OpenAPI Generator command and inspecting the generated MyRestModel annotation. Compare the Kotlin templates with modules/openapi-generator/src/main/resources/kotlin-wiremock/model.mustache, which already uses @param:. Done means the Jackson Kotlin model uses a constructor-parameter or field target rather than @get:JsonProperty and the reported deserialization case is addressed.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.