kittinunf / fuel

The easiest HTTP networking library for Kotlin/Android
https://fuel.gitbook.io/documentation/
MIT License
4.55k stars 429 forks source link

kotlinx-serialization with List<T> throws #844

Open MihaMarkic opened 1 year ago

MihaMarkic commented 1 year ago

Bug Report

Description

Can't make kotlinx-serialization to decode a list of objects from a JSON string. If I use responseString and 'manually' decode from the resulting string, it works.

To Reproduce

Steps to reproduce the behavior:

Below are two blocks of code, first doesn't work, second does.

@Serializable
data class Store(val storeID: String)

   // does not work
    val (request, response, data) = Fuel.get(address)
        .awaitObjectResponse<List<Store>>(
            kotlinxDeserializerOf()
        )
  // does work
  val httpAsync = Fuel.get(address)
        .responseString { request, response, result ->
            result.fold({ d ->
                    val obj = json.decodeFromString<List<Store>>(d)
                    println(obj[0].name)
            }, { e ->
                println(e.message)
            })
        }

Expected behavior

Should deserialize string.

Actual behavior

Throws the exception below. Exception stack:

Exception in thread "main" Expected class kotlinx.serialization.json.JsonObject (Kotlin reflection is not available) as the serialized body of kotlinx.serialization.Polymorphic<List>, but had class kotlinx.serialization.json.JsonArray (Kotlin reflection is not available)
    com.github.kittinunf.fuel.core.FuelError$Companion.wrap(FuelError.kt:86)
    com.github.kittinunf.fuel.core.DeserializableKt.awaitResponse(Deserializable.kt:258)
Caused by: kotlinx.serialization.json.internal.JsonDecodingException: Expected class kotlinx.serialization.json.JsonObject (Kotlin reflection is not available) as the serialized body of kotlinx.serialization.Polymorphic<List>, but had class kotlinx.serialization.json.JsonArray (Kotlin reflection is not available)
    kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)
    kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:94)
    kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:81)
    kotlinx.serialization.json.Json.decodeFromString(Json.kt:95)
    MainKt$test$$inlined$kotlinxDeserializerOf$1.deserialize(FuelSerialization.kt:55)

Environment

IntelliJ IDEA Community

JellyBrick commented 1 year ago

https://github.com/kittinunf/fuel/issues/817#issuecomment-942683310