Hi, I have an issue with a polymorphic serializer ...
# serialization
k
Hi, I have an issue with a polymorphic serializer used alongside retrofit. When used in my tests, the serializer works correctly, but when I'm trying to get retrofit to deserialize it in my api interface, it spits an infamous
JsonDecodingException: Polymorphic serializer was not found for class discriminator [...]
. I don't know exactly where I'm supposed to have retrofit handle this serializer: should it be inside my
Json
config when declaring my
ConverterFactory
? Here is some of my code to make it more clear what I'm currently doing:
Copy code
object DecodedBarcodeSerializer : JsonContentPolymorphicSerializer<DecodedBarcode>(DecodedBarcode::class) { ... } // here's my serializer for a sealed class DecodedBarcode.

// In tests
Json.decodeFromString(DecodedBarcodeSerializer, stringified) // -> tests work just fine when specifically calling the serializer

// api
interface DecoderApi { // --> I tried using a `@file:UseSerializers(DecodedBarcodeSerializer::class)` but retrofit doesn't care about it :p
    @POST("$BASE/decoder/decoded-barcodes/decode")
    fun decodeBarcode(@Body barcode: String): IO<DecodedBarcode>
}

// retrofit declaration
val contentType = "application/json".toMediaType()
Retrofit.Builder()
    .baseUrl(instance<String>(BASE_URL))
    .client(instance())
    .addCallAdapterFactory(IOAdapterFactory())
    .addConverterFactory(Json {
        ignoreUnknownKeys = true
        isLenient = true
        prettyPrint = true // --> should I put something like a SerializersModule here?
    }.asConverterFactory(contentType))
    .build()