Skip to content

Commit 369c674

Browse files
committed
[spring-server] fix: correctly handle graphql request with charset encoding
In our default GraphQL route we were explicitly checking if `Content-Type` header is just `application/json`. If request was also specifying charset, e.g. `application/json;charset=UTF-8` our logic would fail to match it and result in HTTP 400 (Bad Request) response.
1 parent 9816f5c commit 369c674

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/RoutesConfiguration.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ class RoutesConfiguration(
7777
}
7878
GraphQLRequest(query = query, operationName = operationName, variables = graphQLVariables)
7979
}
80-
serverRequest.method() == HttpMethod.POST -> when (serverRequest.headers().contentType().orElse(MediaType.APPLICATION_JSON)) {
81-
MediaType.APPLICATION_JSON -> serverRequest.awaitBody()
82-
graphQLMediaType -> GraphQLRequest(query = serverRequest.awaitBody())
83-
else -> null
80+
serverRequest.method() == HttpMethod.POST -> {
81+
val contentType = serverRequest.headers().contentType().orElse(MediaType.APPLICATION_JSON)
82+
when {
83+
contentType.includes(MediaType.APPLICATION_JSON) -> serverRequest.awaitBody()
84+
contentType.includes(graphQLMediaType) -> GraphQLRequest(query = serverRequest.awaitBody())
85+
else -> null
86+
}
8487
}
8588
else -> null
8689
}

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/routes/RouteConfigurationIT.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,23 @@ directive @deprecated(reason: String = "No longer supported") on FIELD_DEFINITIO
215215
.jsonPath("$.extensions").doesNotExist()
216216
}
217217

218+
@Test
219+
fun `verify POST graphQL request with explicit charset`() {
220+
val request = GraphQLRequest(
221+
query = "query helloWorldQuery(\$name: String!) { hello(name: \$name) }",
222+
variables = mapOf("name" to "JUNIT route with charset encoding"),
223+
operationName = "helloWorldQuery"
224+
)
225+
226+
testClient.post()
227+
.uri("/graphql")
228+
.accept(MediaType.APPLICATION_JSON_UTF8)
229+
.contentType(MediaType.APPLICATION_JSON_UTF8)
230+
.bodyValue(request)
231+
.exchange()
232+
.verifyGraphQLRoute("Hello JUNIT route with charset encoding!")
233+
}
234+
218235
private fun WebTestClient.ResponseSpec.verifyGraphQLRoute(expected: String) = this.expectStatus().isOk
219236
.expectBody()
220237
.jsonPath("$.data.hello").isEqualTo(expected)

0 commit comments

Comments
 (0)