Skip to content

Commit

Permalink
adding checks for json end_document in http batching interceptors (#5893
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
duzinkie authored Jun 14, 2024
1 parent 8115e51 commit 7bba507
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7bba507

Please sign in to comment.