-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple issues with schema generation: field order, value classes and custom handling #56
Comments
Most of these issues can be fixed by cluttering the source code with a lot of Jackson annotations. For anyone passing by with similar issues, here's how I fixed them:
Fixed using @get:JsonIgnore
val duration: Duration
get() = end - start val duration: Duration
@JsonIgnore
get() = end - start
Fixed with @Serializable
data class Committee(
@get:JsonProperty("id")
val id: CommitteeId,
val attendance: Set<DateRange>
)
We can actually use custom Jackson serializers, for example for object DateSerializer : JsonSerializer<Date>() {
override fun serialize(value: Date, gen: JsonGenerator, serializers: SerializerProvider) {
gen.writeString(value.format())
}
} Then, every time we have a field of type @Serializable
data class DateRange(
@get:JsonSerialize(using = DateSerializer::class)
val start: Date,
@get:JsonSerialize(using = DateSerializer::class)
val end: Date,
) And good luck implementing every single serializer twice for kotlinx.serialization and Jackson. While it works, this adds a lot of clutter. It also works in my current Kotlin/JVM project, but I have a lot of Kotlin/Multiplatform projects where models are defined in I also did not find a way to keep the declared field order in the output, yet. Right now the output field order seems to be random. It's not super important as json does not have key order in objects, but here for json that is supposed to be read by humans, it would be cool to have. |
Hi, thanks for your detailed issue! I unfortunately don't think there's much that can be done in Tegral without reimplementing parts of Swagger Core (which is planned) For a bit of context (and as you probably guessed), Swagger Core mainly deals with Jackson annotations and internally uses Jackson, but it should be possible to implement the required parts
I'm not sure of whether Tegral OpenAPI, Swagger Core or Jackson is the culprit
Both of these are because Swagger Core only considers Jackson annotations, and not I'll do my best to address all of these issues in #55 and will use your classes as test data. For the mangling part, I have no idea how inlined value classes are depicted when using reflection, so I'll have to check how that works. Out of curiosity, is Ktor correctly serializing your data like you'd expect in the JSON schema? |
I'm not exactly sure what you ask here, but yes, |
Hi, I just tried Tegral OpenAPI Ktor Plugins for the first time. It's kinda nice, but schema generation has multiple problems. I'll provide an example:
Here is my model, hopefully with all its dependencies so that it can be used in a reproducer:
All types used
I have a
Resource
defined like this:In an ideal world, this is the generated json schema I'd like to see:
And this is what I get:
Here's a list of what I think fails / is lacking:
duration
property onDateRange
value class
typed valuesDate
type here. Maybe add an option to usekotlinx.serialization
somehow, or just a way to pass a custom lambda in an annotation or somethingThe text was updated successfully, but these errors were encountered: