Home > Software engineering >  Swagger takes both defined @JsonProperty field name and generates one automatuically
Swagger takes both defined @JsonProperty field name and generates one automatuically

Time:08-24

I have a request class:


data class RegisterDto(
        @JsonProperty("first_name")
        var firstName : String,

        @JsonProperty("last_name")
        var lastName: String,

        @JsonProperty("email")
        var email : String,

        @JsonProperty("password")
        var password : String
)

Which in swagger UI looks like this:

enter image description here

As you see there are two fields firstName and first_name in the request. How do I make it only override the field name?

Swagger takes both defined @JsonProperty field name and generates one automatuically. How do I only keep the defined field?

I use springdoc-openapi-ui dependency.

CodePudding user response:

The fields are included twice because Kotlin generates a getter for the field (e.g. getFirstName()), and springdoc-openapi-ui generates a field for both the field and the getter. When you don't override the name with @JsonProperty("first_name"), this is not a problem, because the library will detect that they are the same fields. However, with the annotation, it fails to detect this.

To solve the issue, you have to add springdoc-openapi-kotlin as a dependency. This will "teach" springdoc-openapi-ui how Kotlin fields should be handled (i.e. the getter should not be considered separately).

  • Related