springwolf-core icon indicating copy to clipboard operation
springwolf-core copied to clipboard

Add support for Kotlin Serialization

Open ctasada opened this issue 1 year ago • 3 comments

Springwolf doesn't support the generation of AsyncAPI components when used with Kotlin

Example:

@Serializable
data class Period(
    @SerialName("start_date")
    val startDate: String,
    @SerialName("period_end")
    val endDate: String,
)

The outcome looks like:

    Period:
      required:
        - endDate
        - startDate
      type: object
      properties:
        endDate:
          type: string
        startDate:
          type: string

Ignoring the @SerialName annotation.

ctasada avatar Feb 06 '24 11:02 ctasada

Hi @ctasada ,

springwolf uses swagger to get the property names.

Even though I use kotlin myself, adding support for one 'external' annotations opens the door to add support for many more annotations. Besides, iterating through the class hierarchy to identify all annotations and patch the properties names can be tricky.

Multiple alternatives come to mind:

  1. Get support for the @SerialName annotation in swagger itself.
  2. Create some kind of meta-annotation/code-generation/compiler plugin that adds for each @SerialName annotation the swagger @JsonProperty/@Schema annotation.

Do you have a simple solution in mind?

timonback avatar Feb 09 '24 13:02 timonback

Agree, I was thinking about maybe something like a Swagger ModelConverter that supports the annotation.

I found this old ticket in SpringDoc which we may use as reference: https://github.com/springdoc/springdoc-openapi/issues/1226

ctasada avatar Feb 09 '24 14:02 ctasada

Awesome, Springwolf does support the ModelConverter mechanism. Just create a bean in the spring context (example: MonetaryAmountConverter within the add-ons package)

The class in the linked GH comment has quite high complexity and I am unsure about creating a Springwolf plugin at this point, which will require maintenance. Adding it to Springwolf raises the expectation that the full implementation is tested and works as expected - including resolving issues.

Still, you can add the class to your project.

I am open to re-consider adding it to springwolf, if more people request it.

timonback avatar Feb 09 '24 14:02 timonback

Thank you @ctasada for the contribution. It is released now.

As discussed, we mark it as beta for now. Feel free to update the documentation - as this becomes stable - to share the effort you put into it to better support Kotlin.

https://github.com/springwolf/springwolf.github.io/blob/master/docs

timonback avatar Mar 24 '24 09:03 timonback