Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Set maxStringLength in Jackson JsonFactory to maxResponseSize in Http…
Browse files Browse the repository at this point in the history
…Client.

Needed because this is now enforced in Jackson 2.15.
See https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.15 and
FasterXML/jackson-core#863
  • Loading branch information
flowertwig committed Apr 26, 2023
1 parent 6cb803b commit 6f06ced
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package se.fortnox.reactivewizard.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
Expand Down Expand Up @@ -86,6 +87,13 @@ public class HttpClient implements InvocationHandler {
private static final Class BYTEARRAY_TYPE = (new byte[0]).getClass();
private static final String COOKIE = "Cookie";
private static final String QUOTE = "\"";
private static final String ERROR_CALLING_OTHER_SERVICE = """
Error calling other service:
\tResponse Status: %d
\tURL: %s
\tRequest Headers: %s
\tResponse Headers: %s
\tData: %s""";

protected final InetSocketAddress serverInfo;
protected final HttpClientConfig config;
Expand Down Expand Up @@ -114,6 +122,11 @@ public HttpClient(HttpClientConfig config,
this.objectMapper = objectMapper;
this.requestLogger = requestLogger;
this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
var constraints = StreamReadConstraints.builder()
.maxStringLength(config.getMaxResponseSize())
.build();
this.objectMapper.getFactory()
.setStreamReadConstraints(constraints);
this.requestParameterSerializers = requestParameterSerializers;

serverInfo = new InetSocketAddress(config.getHost(), config.getPort());
Expand Down Expand Up @@ -489,12 +502,7 @@ protected Mono<Response<Flux<?>>> handleError(RequestBuilder request, RwHttpClie
HttpResponseStatus status = response.getHttpClientResponse().status();
if (status.code() >= 400) {
return collector.collectString(response.getContent()).onErrorReturn("").map(data -> {
String message = format("Error calling other service:\n" +
"\tResponse Status: %d\n" +
"\tURL: %s\n" +
"\tRequest Headers: %s\n" +
"\tResponse Headers: %s\n" +
"\tData: %s",
String message = format(ERROR_CALLING_OTHER_SERVICE,
status.code(),
request.getFullUrl(),
requestLogger.getHeaderValuesOrRedactClient(request.getHeaders()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package se.fortnox.reactivewizard.client;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -181,10 +180,8 @@ protected TestResource getHttpProxy(int port, int maxConn, int maxRequestTime) {
}

private TestResource getHttpProxy(HttpClientConfig config) {
ObjectMapper mapper = new ObjectMapper().findAndRegisterModules()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
requestLogger = new RequestLogger();
HttpClient client = new HttpClient(config, new ReactorRxClientProvider(config, healthRecorder), mapper, new RequestParameterSerializers(
HttpClient client = new HttpClient(config, new ReactorRxClientProvider(config, healthRecorder), new ObjectMapper(), new RequestParameterSerializers(
Set.of(new SessionParameterSerializer())), Collections.emptySet(), requestLogger);
return client.create(TestResource.class);
}
Expand Down Expand Up @@ -1561,9 +1558,7 @@ public void shouldLogRequestDetailsOnTimeout() {

private String generateLargeString(int sizeInMB) {
char[] resp = new char[sizeInMB * 1024 * 1024];
for (int i = 0; i < resp.length; i++) {
resp[i] = 'a';
}
Arrays.fill(resp, 'a');
resp[0] = '\"';
resp[resp.length - 1] = '\"';
return new String(resp);
Expand Down

0 comments on commit 6f06ced

Please sign in to comment.