Skip to content

Commit 12824dd

Browse files
authored
Allow override of StreamReadContraints default with overrideDefaultStreamReadConstraints() (#1019)
1 parent 8c3ea4f commit 12824dd

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/main/java/com/fasterxml/jackson/core/StreamReadConstraints.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,34 @@ public class StreamReadConstraints
5757
protected final int _maxNumLen;
5858
protected final int _maxStringLen;
5959

60-
private static final StreamReadConstraints DEFAULT =
60+
private static StreamReadConstraints DEFAULT =
6161
new StreamReadConstraints(DEFAULT_MAX_DEPTH, DEFAULT_MAX_NUM_LEN, DEFAULT_MAX_STRING_LEN);
6262

63+
/**
64+
* Override the default StreamReadConstraints. These defaults are only used when {@link JsonFactory}
65+
* instances are not configured with their own StreamReadConstraints.
66+
* <p>
67+
* Library maintainers should not set this as it will affect other code that uses Jackson.
68+
* Library maintainers who want to configure StreamReadConstraints for the Jackson usage within their
69+
* lib should create <code>ObjectMapper</code> instances that have a {@link JsonFactory} instance with
70+
* the required StreamReadConstraints.
71+
* <p>
72+
* This method is meant for users delivering applications. If they use this, they set it when they start
73+
* their application to avoid having other code initialize their mappers before the defaults are overridden.
74+
*
75+
* @param streamReadConstraints new default for StreamReadConstraints (a null value will reset to built-in default)
76+
* @see #defaults()
77+
* @see #builder()
78+
* @since v2.15.2
79+
*/
80+
public static void overrideDefaultStreamReadConstraints(final StreamReadConstraints streamReadConstraints) {
81+
if (streamReadConstraints == null) {
82+
DEFAULT = new StreamReadConstraints(DEFAULT_MAX_DEPTH, DEFAULT_MAX_NUM_LEN, DEFAULT_MAX_STRING_LEN);
83+
} else {
84+
DEFAULT = streamReadConstraints;
85+
}
86+
}
87+
6388
public static final class Builder {
6489
private int maxNestingDepth;
6590
private int maxNumLen;
@@ -161,6 +186,10 @@ public static Builder builder() {
161186
return new Builder();
162187
}
163188

189+
/**
190+
* @return the default {@link StreamReadConstraints} (when none is set on the {@link JsonFactory} explicitly)
191+
* @see #overrideDefaultStreamReadConstraints
192+
*/
164193
public static StreamReadConstraints defaults() {
165194
return DEFAULT;
166195
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fasterxml.jackson.core;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class TestStreamReadConstraints {
8+
@Test
9+
public void testOverride() {
10+
final int numLen = 1234;
11+
final int strLen = 12345;
12+
final int depth = 123;
13+
StreamReadConstraints constraints = StreamReadConstraints.builder()
14+
.maxNumberLength(numLen)
15+
.maxStringLength(strLen)
16+
.maxNestingDepth(depth)
17+
.build();
18+
try {
19+
StreamReadConstraints.overrideDefaultStreamReadConstraints(constraints);
20+
assertEquals(depth, StreamReadConstraints.defaults().getMaxNestingDepth());
21+
assertEquals(strLen, StreamReadConstraints.defaults().getMaxStringLength());
22+
assertEquals(numLen, StreamReadConstraints.defaults().getMaxNumberLength());
23+
} finally {
24+
StreamReadConstraints.overrideDefaultStreamReadConstraints(null);
25+
assertEquals(StreamReadConstraints.DEFAULT_MAX_DEPTH,
26+
StreamReadConstraints.defaults().getMaxNestingDepth());
27+
assertEquals(StreamReadConstraints.DEFAULT_MAX_STRING_LEN,
28+
StreamReadConstraints.defaults().getMaxStringLength());
29+
assertEquals(StreamReadConstraints.DEFAULT_MAX_NUM_LEN,
30+
StreamReadConstraints.defaults().getMaxNumberLength());
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)