fuel icon indicating copy to clipboard operation
fuel copied to clipboard

kotlinx-serialization with List<T> throws

Open MihaMarkic opened this issue 1 year ago • 1 comments

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

MihaMarkic avatar Nov 22 '22 18:11 MihaMarkic