SMILEY4 / ktor-swagger-ui

Kotlin Ktor plugin to generate OpenAPI and provide Swagger UI
Apache License 2.0
180 stars 33 forks source link

Overriding type using annotation #157

Closed Him188 closed 6 days ago

Him188 commented 3 weeks ago

Hi, I have a data class field of type kotlinx.datetime.Instant, which is @Serializable(with = InstantIso8601Serializer::class). So the serialized value will be a String, instead of an object. Currently ktor-swagger-ui generates Instant as a class, which is incorrect:

          "begin" : {
            "$ref" : "#/components/schemas/kotlinx.datetime.Instant"
          },

Is it possible to override this type? I spent 2 hours searching for solutions but I failed. I understand that the feature might be supported in the kenerator project, but I found it difficult to integrate it into swagger-ui. It would be greatful if this use case can be considered and a simple solution is provided!

What I would like to have is:


@Serializable
data class OnAirAnimeInfo(
    @field:Schema(type = "String", example = "2024-07-06T13:00:00.000Z")
    val begin: Instant? = null,
)
          "begin" : String,

Or even better, retrieve the serializer of the field, and use its descriptor.

SMILEY4 commented 1 week ago

Hi,

I think https://github.com/SMILEY4/ktor-swagger-ui/issues/155 is describing a similar issue to what you are facing. You might be able to copy most of that for your use case.

I also like the idea of an annotation for this, since this same issue comes up more often - I'll see what i can do there.

Edit: i added a @Type and @Format that can be added on classes and properties and overwrites the type/format, avoiding having to write custom steps as described in the other issue. The annotations will be included in the next version.

Him188 commented 6 days ago

Awesome!!