-
Notifications
You must be signed in to change notification settings - Fork 637
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
Support for additional properties #1978
Comments
for reference the KSerializer I wrote for my example: object ResponsesSerializer : KSerializer<Responses> {
private val mapSerializer = MapSerializer(String.serializer(), Response.serializer())
private const val defaultFieldName = "default"
override val descriptor: SerialDescriptor = object : SerialDescriptor by mapSerializer.descriptor {
override val serialName: String = "Responses"
}
override fun serialize(encoder: Encoder, value: Responses) {
val pairs = listOfNotNull(
value.default?.let { Pair(defaultFieldName, it) },
) + value.statuses.map { (key, value) -> Pair(key.code.toString(), value) }
mapSerializer.serialize(encoder, pairs.toMap())
}
override fun deserialize(decoder: Decoder): Responses {
val map = mapSerializer.deserialize(decoder).toMutableMap()
val default = map.remove(defaultFieldName)
val statuses = map.mapKeys { (key, _) -> HttpStatusCode(key.toUShort()) }
return Responses(default, statuses)
}
} |
See #959 (comment) |
Interesting alternative to my serializer, but it still requires me to write a custom serializer doesn't it? |
Yes, it still requires one, but given that |
I guess "require" is a strong word here. Technically it is obviously not required since there is at least two alternative approaches. However it would be great to have a dedicated feature for that, especially since this requires duplicating field names into the transformation which can easily go out of sync with the model. An annotation |
The use cases for KxS are more than just JSON, would be great to have this possibly supported in custom formats |
Introduces support for [kotlinx.serialization](https://kotlinlang.org/docs/serialization.html) (KxS) annotations on the generated models. A new `--serialization-library` option allows the user to choose between `JACKSON` and `KOTLINX_SERIALIZATION`. The Jackson implementation is the default and has not changed, and users will get the same output as previously. For date and datetime [kotlinx-datetime](https://github.com/Kotlin/kotlinx-datetime) is used to make the generated code work in multi platform / non-JVM environments. Date is translated to `kotlinx.datetime.LocalDate` and datetime to `kotlinx.datetime.Instant`. **Unsupported features** 1. When KxS is chosen `additionalProperties: true` is not supported. This is due to fact that supporting a `map<String,Any?>` is quite tricky (if at all possible?) with Kotlinx.Serialization, whereas Jackson has `JsonAnyGetter`/`JsonAnySetter` to support this. We might be able to configure the property with [@contextual](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-contextual/) to instruct the plugin to resolve serializer at runtime. Related: Kotlin/kotlinx.serialization#1978. 3. URI and UUID are translated to strings as no native Kotlin types exist ([yet](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.uuid/-experimental-uuid-api/)?). 4. `number` without format is currently translated to Java's `BigDecimal` which will not work in non-JVM Kotlin. Works fine for `number`/`float` and `number`/`double` as these are translated into Kotlin types. **Tests** I am unsure how many more tests for the examples we want. Many of the examples are concerned with translating OpenAPI into models, and are not specific to the serialization annotations. Any opinions/input/suggestion are welcome.
It is sometimes the case that I want to parse an object with a few concrete properties and a bunch of additional properties.
OpenAPI 3's Responses Object is an example for this. It has the fixed
default
default plus additional fields for various http statuses.so this could look like:
And it would be great to be able to parse this into something like:
Jackson has the
@JsonAnySetter
annotation which can be used to implement this.OpenAPI/JSON Schema support specifying object schemas that have both
properties
andadditionalProperties
.Currently I think the only solution would be a hand-written KSerializer for Responses.
The text was updated successfully, but these errors were encountered: