diff --git a/src/main/java/com/fasterxml/jackson/core/StreamReadConstraints.java b/src/main/java/com/fasterxml/jackson/core/StreamReadConstraints.java index 26756d072d..c61793e699 100644 --- a/src/main/java/com/fasterxml/jackson/core/StreamReadConstraints.java +++ b/src/main/java/com/fasterxml/jackson/core/StreamReadConstraints.java @@ -57,9 +57,34 @@ public class StreamReadConstraints protected final int _maxNumLen; protected final int _maxStringLen; - private static final StreamReadConstraints DEFAULT = + private static StreamReadConstraints DEFAULT = new StreamReadConstraints(DEFAULT_MAX_DEPTH, DEFAULT_MAX_NUM_LEN, DEFAULT_MAX_STRING_LEN); + /** + * Override the default StreamReadConstraints. These defaults are only used when {@link JsonFactory} + * instances are not configured with their own StreamReadConstraints. + *
+ * Library maintainers should not set this as it will affect other code that uses Jackson.
+ * Library maintainers who want to configure StreamReadConstraints for the Jackson usage within their
+ * lib should create ObjectMapper
instances that have a {@link JsonFactory} instance with
+ * the required StreamReadConstraints.
+ *
+ * This method is meant for users delivering applications. If they use this, they set it when they start + * their application to avoid having other code initialize their mappers before the defaults are overridden. + * + * @param streamReadConstraints new default for StreamReadConstraints (a null value will reset to built-in default) + * @see #defaults() + * @see #builder() + * @since v2.15.2 + */ + public static void overrideDefaultStreamReadConstraints(final StreamReadConstraints streamReadConstraints) { + if (streamReadConstraints == null) { + DEFAULT = new StreamReadConstraints(DEFAULT_MAX_DEPTH, DEFAULT_MAX_NUM_LEN, DEFAULT_MAX_STRING_LEN); + } else { + DEFAULT = streamReadConstraints; + } + } + public static final class Builder { private int maxNestingDepth; private int maxNumLen; @@ -161,6 +186,10 @@ public static Builder builder() { return new Builder(); } + /** + * @return the default {@link StreamReadConstraints} (when none is set on the {@link JsonFactory} explicitly) + * @see #overrideDefaultStreamReadConstraints + */ public static StreamReadConstraints defaults() { return DEFAULT; } diff --git a/src/test/java/com/fasterxml/jackson/core/TestStreamReadConstraints.java b/src/test/java/com/fasterxml/jackson/core/TestStreamReadConstraints.java new file mode 100644 index 0000000000..e38864bb33 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/core/TestStreamReadConstraints.java @@ -0,0 +1,33 @@ +package com.fasterxml.jackson.core; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestStreamReadConstraints { + @Test + public void testOverride() { + final int numLen = 1234; + final int strLen = 12345; + final int depth = 123; + StreamReadConstraints constraints = StreamReadConstraints.builder() + .maxNumberLength(numLen) + .maxStringLength(strLen) + .maxNestingDepth(depth) + .build(); + try { + StreamReadConstraints.overrideDefaultStreamReadConstraints(constraints); + assertEquals(depth, StreamReadConstraints.defaults().getMaxNestingDepth()); + assertEquals(strLen, StreamReadConstraints.defaults().getMaxStringLength()); + assertEquals(numLen, StreamReadConstraints.defaults().getMaxNumberLength()); + } finally { + StreamReadConstraints.overrideDefaultStreamReadConstraints(null); + assertEquals(StreamReadConstraints.DEFAULT_MAX_DEPTH, + StreamReadConstraints.defaults().getMaxNestingDepth()); + assertEquals(StreamReadConstraints.DEFAULT_MAX_STRING_LEN, + StreamReadConstraints.defaults().getMaxStringLength()); + assertEquals(StreamReadConstraints.DEFAULT_MAX_NUM_LEN, + StreamReadConstraints.defaults().getMaxNumberLength()); + } + } +}