Skip to content

Feature/default utf8 encoding #411

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

Merged
merged 2 commits into from
Jan 23, 2022
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
Expand Up @@ -87,9 +87,10 @@ private GraphQLInvocationInput getGraphQLInvocationInput(
}
});

String query = read(inputStream);
String query = read(inputStream, request.getCharacterEncoding());
if ("query".equals(key) && isSingleQuery(query)) {
GraphQLRequest graphqlRequest = buildRequestFromQuery(query, graphQLObjectMapper, parts);
GraphQLRequest graphqlRequest =
buildRequestFromQuery(query, graphQLObjectMapper, parts, request.getCharacterEncoding());
variablesMap.ifPresent(m -> mapMultipartVariables(graphqlRequest, m, parts));
return invocationInputFactory.create(graphqlRequest, request, response);
} else if (isSingleQuery(query)) {
Expand All @@ -109,6 +110,7 @@ private Optional<Part> findPart(Map<String, List<Part>> parts) {
.filter(parts::containsKey)
.map(key -> getPart(parts, key))
.findFirst()
.filter(Optional::isPresent)
.map(Optional::get);
}

Expand Down Expand Up @@ -141,33 +143,38 @@ private void mapMultipartVariables(
}

private GraphQLRequest buildRequestFromQuery(
String query, GraphQLObjectMapper graphQLObjectMapper, Map<String, List<Part>> parts)
String query,
GraphQLObjectMapper graphQLObjectMapper,
Map<String, List<Part>> parts,
String charset)
throws IOException {
Map<String, Object> variables = null;
final Optional<Part> variablesItem = getPart(parts, "variables");
if (variablesItem.isPresent()) {
variables =
graphQLObjectMapper.deserializeVariables(read(variablesItem.get().getInputStream()));
graphQLObjectMapper.deserializeVariables(
read(variablesItem.get().getInputStream(), charset));
}

Map<String, Object> extensions = null;
final Optional<Part> extensionsItem = getPart(parts, "extensions");
if (extensionsItem.isPresent()) {
extensions =
graphQLObjectMapper.deserializeExtensions(read(extensionsItem.get().getInputStream()));
graphQLObjectMapper.deserializeExtensions(
read(extensionsItem.get().getInputStream(), charset));
}

String operationName = null;
final Optional<Part> operationNameItem = getPart(parts, "operationName");
if (operationNameItem.isPresent()) {
operationName = read(operationNameItem.get().getInputStream()).trim();
operationName = read(operationNameItem.get().getInputStream(), charset).trim();
}

return new GraphQLRequest(query, variables, extensions, operationName);
}

private String read(InputStream inputStream) throws IOException {
try (InputStreamReader streamReader = new InputStreamReader(inputStream);
private String read(InputStream inputStream, String charset) throws IOException {
try (InputStreamReader streamReader = new InputStreamReader(inputStream, charset);
BufferedReader reader = new BufferedReader(streamReader)) {
return reader.lines().collect(joining());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import graphql.kickstart.servlet.input.GraphQLInvocationInputFactory;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import graphql.GraphQLException;
import graphql.kickstart.execution.input.GraphQLInvocationInput;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -32,14 +33,17 @@ public HttpRequestHandlerImpl(
@Override
public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
try {
if (request.getCharacterEncoding() == null) {
request.setCharacterEncoding(StandardCharsets.UTF_8.name());
}
GraphQLInvocationInputParser invocationInputParser =
GraphQLInvocationInputParser.create(
request,
configuration.getInvocationInputFactory(),
configuration.getObjectMapper(),
configuration.getContextSetting());
GraphQLInvocationInput invocationInput =
invocationInputParser.getGraphQLInvocationInput(request, response);
invocationInputParser. getGraphQLInvocationInput(request, response);
requestInvoker.execute(invocationInput, request, response);
} catch (GraphQLException | JsonProcessingException e) {
response.setStatus(STATUS_BAD_REQUEST);
Expand Down