Skip to content

Commit

Permalink
KNET-15501 work around jackson databind string length limit (#1273)
Browse files Browse the repository at this point in the history
Implement the change from FasterXML/jackson-core#1001 (comment)

I'm using maxint for the value, as we do length checks of the stream elsewhere, and this seems safest to avoid a regression (see the code comment for more details)
  • Loading branch information
ehumber authored Jun 7, 2024
1 parent 2657c77 commit 23a5b5f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package io.confluent.kafkarest;

import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
Expand Down Expand Up @@ -176,13 +177,28 @@ public void setupResources(Configurable<?> config, KafkaRestConfig appConfig) {

@Override
public ObjectMapper getJsonMapper() {
return super.getJsonMapper()
.registerModule(new GuavaModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"))
.setTimeZone(TimeZone.getTimeZone("UTC"));
ObjectMapper mapper =
super.getJsonMapper()
.registerModule(new GuavaModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"))
.setTimeZone(TimeZone.getTimeZone("UTC"));

// We could use PRODUCE_REQUEST_SIZE_LIMIT_MAX_BYTES_CONFIG to set the max read length.
// However, the integration test produceBinaryWithLargerSizeMessage shows that a 20M
// message needs this value to be set to 23999999 to allow the message to be read.
// Rather than trying to work out how many extra bytes to add to account for headers etc,
// we'll let the existing code in JsonStreamMessageBodyReader.readFrom() deal with message
// length checks, and just use maxint for the limit imposed by jackson.databind from v2.15
// onwards.
mapper
.getFactory()
.setStreamReadConstraints(
StreamReadConstraints.builder().maxStringLength(Integer.MAX_VALUE).build());

return mapper;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ public class KafkaRestConfig extends RestConfig {
+ "Due to the produce request size counting algorithm's limitation, it is recommended "
+ "to add 8192 (8KiB) buffer to the intended number that you want to set for the limit "
+ "to avoid rejecting legitimate produce requests."
+ "If this limit is set to a non-positive number, no limit is applied. Default is 0.";
+ "If this limit is set to 0 or a negative number, no limit is applied. Default is "
+ "0.";
public static final String PRODUCE_REQUEST_SIZE_LIMIT_MAX_BYTES_DEFAULT = "0";
public static final ConfigDef.Range PRODUCE_REQUEST_SIZE_LIMIT_MAX_BYTES_VALIDATOR =
ConfigDef.Range.atLeast(0);
Expand Down

0 comments on commit 23a5b5f

Please sign in to comment.