Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding checks for json end_document in http batching interceptors #5893

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading