From 0d385dc6fbbeb7504d4b83c9f4bad9822c757366 Mon Sep 17 00:00:00 2001 From: Jan Duzinkiewicz Date: Wed, 15 May 2024 14:37:49 -0700 Subject: [PATCH 1/2] adding checks for json end_document in http batching interceptors --- .../apollo3/network/http/BatchingHttpInterceptor.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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..d519271e79e 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,6 +13,7 @@ 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 @@ -30,6 +31,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 +182,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) { + println("Apollo: extra tokens after payload") + } + } + } if (list !is List<*>) throw DefaultApolloException("batched query response is not a list when executing batched query") if (list.size != pending.size) { From 9185be6b0e14ab7435266b5db8b00c5bef278de8 Mon Sep 17 00:00:00 2001 From: Jan Duzinkiewicz Date: Tue, 11 Jun 2024 17:01:13 -0700 Subject: [PATCH 2/2] making not correctly ending json responses throw an exception instead of logging a warning, as per PR feedback --- .../apollo3/network/http/BatchingHttpInterceptor.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 d519271e79e..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 @@ -19,6 +19,7 @@ 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 @@ -186,7 +187,7 @@ class BatchingHttpInterceptor @JvmOverloads constructor( // 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) { - println("Apollo: extra tokens after payload") + throw JsonDataException("Expected END_DOCUMENT but was ${jsonReader.peek()}") } } }