You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
KotlinxSerialization.asFormatString seems incapable of properly handling polymorphic types; it fails to properly serialize the type parameter. I believe this works with raw kotlinx and with lenses because they both reifiy the base provided type, rather than rely on the runtime type.
I don't really see a way around this short of extensive modifications to asFormatString to reify the type. However, doing so would also allow the http4k-format-csv module to become an AutoMarshalling, as this was a limiting issue.
Thoughts? Live with and document the limitation? Or try to reify the type in asFormatString?
See the provided tests below, where serialize sealed class - http4k asFormatString fails.
@Serializable
sealedinterfaceAnimal {
val name:String
}
@Serializable
@SerialName("cat")
data classCat(
overridevalname:String,
vallives:Int,
): Animal
val json ="""{"type":"cat","name":"Bandit","lives":8}"""val obj =Cat("Bandit", 8)
val lens =KotlinxSerialization.autoBody<Animal>().toLens()
@Test
fun`serialize sealed class - kotlinx encodeToString`() {
assertThat(
Json.encodeToString(obj asAnimal),
equalTo(json)
)
}
// PASS
@Test
fun`serialize sealed class - http4k asFormatString`() {
assertThat(
KotlinxSerialization.asFormatString(obj asAnimal),
equalTo("""{"type":"cat","name":"Bandit","lives":8}""")
)
} // FAIL. Was {"name":"Bandit","lives":8}"
@Test
fun`serialize sealed class - http4k lens`() {
assertThat(
Response(Status.OK).with(lens of obj).bodyString(),
equalTo(json)
)
}
// PASS
@Test
fun`deserialize sealed class - kotlinx decodeFromString`() {
assertThat(
Json.decodeFromString<Animal>(json),
equalTo(obj)
)
}
// PASS
@Test
fun`deserialize sealed class - http4k asA`() {
assertThat(
KotlinxSerialization.asA<Animal>(json),
equalTo(obj)
)
}
// PASS
@Test
fun`deserialize sealed class - http4k lens`() {
assertThat(
lens(Response(Status.OK).body(json)),
equalTo(obj)
)
}
// PASS
The text was updated successfully, but these errors were encountered:
KotlinxSerialization.asFormatString
seems incapable of properly handling polymorphic types; it fails to properly serialize thetype
parameter. I believe this works with raw kotlinx and with lenses because they both reifiy the base provided type, rather than rely on the runtime type.I don't really see a way around this short of extensive modifications to
asFormatString
to reify the type. However, doing so would also allow the http4k-format-csv module to become anAutoMarshalling
, as this was a limiting issue.Thoughts? Live with and document the limitation? Or try to reify the type in
asFormatString
?See the provided tests below, where serialize sealed class - http4k asFormatString fails.
The text was updated successfully, but these errors were encountered: