springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.26k stars 493 forks source link

Kotlin value classes get an incorrect name #2623

Closed serandel closed 3 months ago

serandel commented 3 months ago

Describe the bug

Springdoc is putting a random (?) suffix to the end of my fields when they're a Kotlin value class.

This is my DTO for this example:

@Serializable
data class Address(
    val streetName: String? = null,
    val postalCode: String? = null,
    val town: String? = null,
    val country: Country,
)

@JvmInline
@Serializable
value class Country(val isoCode: String)

Please, bear in mind that the other properties should also be value classes. I don't have it this way yet because I was researching this issue.

The problem is that when a schema is generated through

@Operation(summary = "foobar")
fun fooBar(
    @Parameter(schema = Schema(implementation = Address::class))
    address: Address
)

the country field is documented as being called country-GvCqL24, which is just wrong.

I've tested that KotlinX Serialization (which we're using instead of Jackson) is correctly defining the field name as country in the Json objects.

To Reproduce Steps to reproduce the behavior:

Expected behavior

The field should be named as the field is in the enclosing object, in this case: "country". That it is a value class for better domain modelling in my Kotlin service should be irrelevant. (The type is already being translated to string, which is right, it's only the name what's being garbled.)

Screenshots

Here we can see the wrong name being generated.

image

image

Additional context

This is a sample object when serialized from my controller:

{
    "streetName":"Main St",
    "postalCode":"12345",
    "town":"Smallville",
    "country":"US"
}
bnasslahsen commented 3 months ago

@serandel,

Not reproducible. Feel free to provide a Minimal, Reproducible Example - with HelloController that reproduces the problem.

This ticket will be closed, but can be reopened if your provide the reproducible sample.

serandel commented 3 months ago

Hi, @bnasslahsen , reopening this issue with a PoC in a repo, as asked.

https://github.com/serandel/springdoc-bug-poc

While doing this I realized the problem does not happen when we use Jackson to serialize the types, but just when changing to KotlinX Serialization. If you look at my repo, the first commit works, and then the second doesn't.

serandel commented 3 months ago

(Well, asking for the issue to be reopened, of course. :D)

serandel commented 3 months ago
{
  "openapi": "3.0.1",
  "info": {
    "title": "Foo API",
    "version": "v1"
  },
  "servers": [
    {
      "url": "http://localhost:8080",
      "description": "Generated server url"
    }
  ],
  "tags": [
    {
      "name": "Foo",
      "description": "Foo operations"
    }
  ],
  "paths": {
    "/foo/": {
      "post": {
        "tags": [
          "Foo"
        ],
        "summary": "Post a foo",
        "operationId": "postFoo",
        "parameters": [
          {
            "name": "foo",
            "in": "query",
            "required": true,
            "schema": {
              "$ref": "#/components/schemas/Foo"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Foo posted"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Foo": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "bar-kCuQ4ew": {
            "type": "string"
          }
        }
      }
    }
  }
}

with bar-kCuQ4ew being the problem here.

bnasslahsen commented 3 months ago

@serandel,

There is an important Note in the docs you are missing: https://springdoc.org/#kotlin-support image

If you add, that should work better:

implementation("com.fasterxml.jackson.module:jackson-module-kotlin") 
serandel commented 3 months ago

Yes, I had missed that indeed. Good catch! Thanks a lot!

Now I just have to ask... would it be feasible and reasonable to ask, as a feature request, to not depend on Jackson and also work with KotlinX Serialization?

bnasslahsen commented 3 months ago

@serandel,

If you are having better proposal than jackson-module-kotlin and willing to submit a PR, we can have look to it!