From fe0143f5fd99eca9cdd497d4b9e7fac4bf64936e Mon Sep 17 00:00:00 2001 From: ikasovitch Date: Tue, 28 Jun 2022 21:35:16 +0300 Subject: [PATCH 1/2] Use an appropriate charset for json in case the request didn't specify a charset --- .idea/.name | 1 - .../kotlin/com/apurebase/kgraphql/KtorFeature.kt | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) delete mode 100644 .idea/.name diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index 0f31176c..00000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -core \ No newline at end of file diff --git a/kgraphql-ktor/src/main/kotlin/com/apurebase/kgraphql/KtorFeature.kt b/kgraphql-ktor/src/main/kotlin/com/apurebase/kgraphql/KtorFeature.kt index 23ac8325..bc7d5d08 100644 --- a/kgraphql-ktor/src/main/kotlin/com/apurebase/kgraphql/KtorFeature.kt +++ b/kgraphql-ktor/src/main/kotlin/com/apurebase/kgraphql/KtorFeature.kt @@ -9,6 +9,7 @@ import io.ktor.request.* import io.ktor.response.* import io.ktor.routing.* import io.ktor.util.* +import java.nio.charset.Charset import kotlinx.coroutines.coroutineScope import kotlinx.serialization.json.* import kotlinx.serialization.json.Json.Default.decodeFromString @@ -56,7 +57,8 @@ class GraphQL(val schema: Schema) { val routing: Route.() -> Unit = { route(config.endpoint) { post { - val request = decodeFromString(GraphqlRequest.serializer(), call.receiveText()) + val bodyAsText = call.receiveTextWithCorrectEncoding() + val request = decodeFromString(GraphqlRequest.serializer(), bodyAsText) val ctx = context { config.contextSetup?.invoke(this, call) } @@ -90,6 +92,17 @@ class GraphQL(val schema: Schema) { return GraphQL(schema) } + private suspend fun ApplicationCall.receiveTextWithCorrectEncoding(): String { + fun ContentType.defaultCharset(): Charset = when (this) { + ContentType.Application.Json -> Charsets.UTF_8 + else -> Charsets.ISO_8859_1 + } + + val contentType = request.contentType() + val suitableCharset = contentType.charset() ?: contentType.defaultCharset() + return receiveStream().bufferedReader(charset = suitableCharset).readText() + } + private fun GraphQLError.serialize(): String = buildJsonObject { put("errors", buildJsonArray { addJsonObject { From 19ab94dc17b8558d6b9c9221bd507017e4ae45d9 Mon Sep 17 00:00:00 2001 From: ikasovitch Date: Tue, 28 Jun 2022 21:51:16 +0300 Subject: [PATCH 2/2] bring back a file which was delete by mistake --- .idea/.name | 1 + 1 file changed, 1 insertion(+) create mode 100644 .idea/.name diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 00000000..0f31176c --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +core \ No newline at end of file