Skip to content

Commit 7bba507

Browse files
authored
adding checks for json end_document in http batching interceptors (#5893)
* adding checks for json end_document in http batching interceptors * making not correctly ending json responses throw an exception instead of logging a warning, as per PR feedback
1 parent 8115e51 commit 7bba507

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

libraries/apollo-runtime/src/commonMain/kotlin/com/apollographql/apollo3/network/http/BatchingHttpInterceptor.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import com.apollographql.apollo3.api.http.HttpResponse
1313
import com.apollographql.apollo3.api.http.valueOf
1414
import com.apollographql.apollo3.api.json.BufferedSinkJsonWriter
1515
import com.apollographql.apollo3.api.json.BufferedSourceJsonReader
16+
import com.apollographql.apollo3.api.json.JsonReader
1617
import com.apollographql.apollo3.api.json.buildJsonByteString
1718
import com.apollographql.apollo3.api.json.writeArray
1819
import com.apollographql.apollo3.exception.ApolloException
1920
import com.apollographql.apollo3.exception.ApolloHttpException
2021
import com.apollographql.apollo3.exception.DefaultApolloException
22+
import com.apollographql.apollo3.exception.JsonDataException
2123
import com.apollographql.apollo3.internal.CloseableSingleThreadDispatcher
2224
import com.apollographql.apollo3.mpp.currentTimeMillis
2325
import kotlinx.coroutines.CompletableDeferred
@@ -30,6 +32,7 @@ import kotlinx.coroutines.sync.Mutex
3032
import kotlinx.coroutines.sync.withLock
3133
import okio.Buffer
3234
import okio.BufferedSink
35+
import okio.use
3336
import kotlin.jvm.JvmOverloads
3437
import kotlin.jvm.JvmStatic
3538

@@ -180,8 +183,14 @@ class BatchingHttpInterceptor @JvmOverloads constructor(
180183
}
181184
val responseBody = response.body ?: throw DefaultApolloException("null body when executing batched query")
182185

183-
// TODO: this is most likely going to transform BigNumbers into strings, not sure how much of an issue that is
184-
val list = AnyAdapter.fromJson(BufferedSourceJsonReader(responseBody), CustomScalarAdapters.Empty)
186+
val list = BufferedSourceJsonReader(responseBody).use { jsonReader ->
187+
// TODO: this is most likely going to transform BigNumbers into strings, not sure how much of an issue that is
188+
AnyAdapter.fromJson(jsonReader, CustomScalarAdapters.Empty).also {
189+
if (jsonReader.peek() != JsonReader.Token.END_DOCUMENT) {
190+
throw JsonDataException("Expected END_DOCUMENT but was ${jsonReader.peek()}")
191+
}
192+
}
193+
}
185194
if (list !is List<*>) throw DefaultApolloException("batched query response is not a list when executing batched query")
186195

187196
if (list.size != pending.size) {

0 commit comments

Comments
 (0)