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

Correct handling of empty GraphQL requests #20295

Merged
merged 1 commit into from
Sep 21, 2021
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 @@ -38,6 +38,7 @@ public class SmallRyeGraphQLExecutionHandler extends SmallRyeGraphQLAbstractHand
+ StandardCharsets.UTF_8.name();
private static final String DEFAULT_REQUEST_CONTENT_TYPE = "application/json; charset="
+ StandardCharsets.UTF_8.name();
private static final String MISSING_OPERATION = "Missing operation body";

public SmallRyeGraphQLExecutionHandler(boolean allowGet, boolean allowPostWithQueryParameters,
CurrentIdentityAssociation currentIdentityAssociation,
Expand Down Expand Up @@ -90,10 +91,23 @@ private void handlePost(HttpServerResponse response, RoutingContext ctx, String
String postResponse;
if (hasQueryParameters(ctx) && allowPostWithQueryParameters) {
JsonObject jsonObjectFromQueryParameters = getJsonObjectFromQueryParameters(ctx);
JsonObject mergedJsonObject = Json.createMergePatch(jsonObjectFromQueryParameters).apply(jsonObjectFromBody)
.asJsonObject();
JsonObject mergedJsonObject;
if (jsonObjectFromBody != null) {
mergedJsonObject = Json.createMergePatch(jsonObjectFromQueryParameters).apply(jsonObjectFromBody)
.asJsonObject();
} else {
mergedJsonObject = jsonObjectFromQueryParameters;
}
if (!mergedJsonObject.containsKey(QUERY)) {
response.setStatusCode(400).end(MISSING_OPERATION);
return;
}
postResponse = doRequest(mergedJsonObject);
} else {
if (jsonObjectFromBody == null) {
response.setStatusCode(400).end(MISSING_OPERATION);
return;
}
postResponse = doRequest(jsonObjectFromBody);
}
response.setStatusCode(200).setStatusMessage(OK).end(Buffer.buffer(postResponse, requestedCharset));
Expand All @@ -114,7 +128,7 @@ private void handleGet(HttpServerResponse response, RoutingContext ctx, String r
.end(Buffer.buffer(getResponse, requestedCharset));

} else {
response.setStatusCode(204).end();
response.setStatusCode(400).end(MISSING_OPERATION);
}
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException(uee);
Expand Down Expand Up @@ -170,6 +184,9 @@ private JsonObject getJsonObjectFromBody(RoutingContext ctx) throws IOException
return input.build();
// Else we expect a Json in the content
} else {
if (body == null || body.isEmpty()) {
return null;
}
try (StringReader bodyReader = new StringReader(body);
JsonReader jsonReader = jsonReaderFactory.createReader(bodyReader)) {
return jsonReader.readObject();
Expand Down