Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

fix(#524): return valid error even if request body is invalid #616

Merged
Show file tree
Hide file tree
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
@@ -0,0 +1,42 @@
package graphql.kickstart.spring.webflux.boot;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.assertj.core.util.Files;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class InvalidJsonRequestTest {

@Autowired private WebTestClient webTestClient;

@ParameterizedTest
@ValueSource(strings = {"\"false\":true", "not-a-json"})
@DisplayName("Should return valid response to a request with invalid JSON or non-JSON body.")
void testHandlingInvalidJsonRequest(String badRequestBody) throws IOException {
// GIVEN
final String expectedJson =
Files.contentOf(
new ClassPathResource("response-to-invalid-request.json").getFile(),
StandardCharsets.UTF_8);
// WHEN - THEN
webTestClient
.post()
.uri("/graphql")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(badRequestBody)
.exchange()
.expectStatus()
.isEqualTo(HttpStatus.OK)
.expectBody()
.json(expectedJson);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"errors": [
{
"message": "Bad request - invalid request body.",
"locations": []
}
],
"data": null
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package graphql.kickstart.spring;

import graphql.ExecutionResultImpl;
import graphql.kickstart.execution.GraphQLObjectMapper;
import graphql.kickstart.execution.GraphQLRequest;
import graphql.kickstart.execution.error.GenericGraphQLError;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -20,8 +23,11 @@
import org.springframework.web.server.ServerWebExchange;

@RequiredArgsConstructor
@Slf4j
public abstract class AbstractGraphQLController {

private static final String INVALID_REQUEST_BODY_MESSAGE = "Bad request - invalid request body.";

private final GraphQLObjectMapper objectMapper;

@PostMapping(
Expand All @@ -34,13 +40,17 @@ public Object graphqlPOST(
@Nullable @RequestParam(value = "operationName", required = false) String operationName,
@Nullable @RequestParam(value = "variables", required = false) String variablesJson,
@Nullable @RequestBody(required = false) String body,
ServerWebExchange serverWebExchange)
throws IOException {
ServerWebExchange serverWebExchange) {

body = Optional.ofNullable(body).orElse("");

if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
GraphQLRequest request = objectMapper.readGraphQLRequest(body);
GraphQLRequest request;
try {
request = objectMapper.readGraphQLRequest(body);
} catch (IOException e) {
return handleBodyParsingException(e, serverWebExchange);
}
if (request.getQuery() == null) {
request.setQuery("");
}
Expand Down Expand Up @@ -95,4 +105,11 @@ protected abstract Object executeRequest(
String operationName,
Map<String, Object> variables,
ServerWebExchange serverWebExchange);

protected Object handleBodyParsingException(
Exception exception, ServerWebExchange serverWebExchange) {
log.error("{} {}", INVALID_REQUEST_BODY_MESSAGE, exception.getMessage());
return objectMapper.createResultFromExecutionResult(
new ExecutionResultImpl(new GenericGraphQLError(INVALID_REQUEST_BODY_MESSAGE)));
}
}