diff --git a/libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/network/http/BatchingHttpInterceptor.kt b/libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/network/http/BatchingHttpInterceptor.kt index 75be0accecf..17408b35c1f 100644 --- a/libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/network/http/BatchingHttpInterceptor.kt +++ b/libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/network/http/BatchingHttpInterceptor.kt @@ -13,11 +13,13 @@ import com.apollographql.apollo3.api.http.HttpResponse import com.apollographql.apollo3.api.http.valueOf import com.apollographql.apollo3.api.json.BufferedSinkJsonWriter import com.apollographql.apollo3.api.json.BufferedSourceJsonReader +import com.apollographql.apollo3.api.json.JsonReader import com.apollographql.apollo3.api.json.buildJsonByteString import com.apollographql.apollo3.api.json.writeArray import com.apollographql.apollo3.exception.ApolloException import com.apollographql.apollo3.exception.ApolloHttpException import com.apollographql.apollo3.exception.DefaultApolloException +import com.apollographql.apollo3.exception.JsonDataException import com.apollographql.apollo3.internal.CloseableSingleThreadDispatcher import com.apollographql.apollo3.mpp.currentTimeMillis import kotlinx.coroutines.CompletableDeferred @@ -30,6 +32,7 @@ import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import okio.Buffer import okio.BufferedSink +import okio.use import kotlin.jvm.JvmOverloads import kotlin.jvm.JvmStatic @@ -180,8 +183,14 @@ class BatchingHttpInterceptor @JvmOverloads constructor( } val responseBody = response.body ?: throw DefaultApolloException("null body when executing batched query") - // TODO: this is most likely going to transform BigNumbers into strings, not sure how much of an issue that is - val list = AnyAdapter.fromJson(BufferedSourceJsonReader(responseBody), CustomScalarAdapters.Empty) + val list = BufferedSourceJsonReader(responseBody).use { jsonReader -> + // TODO: this is most likely going to transform BigNumbers into strings, not sure how much of an issue that is + AnyAdapter.fromJson(jsonReader, CustomScalarAdapters.Empty).also { + if (jsonReader.peek() != JsonReader.Token.END_DOCUMENT) { + throw JsonDataException("Expected END_DOCUMENT but was ${jsonReader.peek()}") + } + } + } if (list !is List<*>) throw DefaultApolloException("batched query response is not a list when executing batched query") if (list.size != pending.size) {