Skip to content

Commit cdc737a

Browse files
authored
StreamWriteConstraints.overrideDefaultStreamWriteConstraints (#1060)
1 parent a7627c4 commit cdc737a

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,31 @@ public class StreamWriteConstraints
3030

3131
protected final int _maxNestingDepth;
3232

33-
private static final StreamWriteConstraints DEFAULT =
34-
new StreamWriteConstraints(DEFAULT_MAX_DEPTH);
33+
private static StreamWriteConstraints DEFAULT = new StreamWriteConstraints(DEFAULT_MAX_DEPTH);
34+
35+
/**
36+
* Override the default StreamWriteConstraints. These defaults are only used when {@link JsonFactory}
37+
* instances are not configured with their own StreamWriteConstraints.
38+
* <p>
39+
* Library maintainers should not set this as it will affect other code that uses Jackson.
40+
* Library maintainers who want to configure StreamWriteConstraints for the Jackson usage within their
41+
* lib should create <code>ObjectMapper</code> instances that have a {@link JsonFactory} instance with
42+
* the required StreamWriteConstraints.
43+
* <p>
44+
* This method is meant for users delivering applications. If they use this, they set it when they start
45+
* their application to avoid having other code initialize their mappers before the defaults are overridden.
46+
*
47+
* @param streamWriteConstraints new default for StreamWriteConstraints (a null value will reset to built-in default)
48+
* @see #defaults()
49+
* @see #builder()
50+
*/
51+
public static void overrideDefaultStreamWriteConstraints(final StreamWriteConstraints streamWriteConstraints) {
52+
if (streamWriteConstraints == null) {
53+
DEFAULT = new StreamWriteConstraints(DEFAULT_MAX_DEPTH);
54+
} else {
55+
DEFAULT = streamWriteConstraints;
56+
}
57+
}
3558

3659
public static final class Builder {
3760
private int maxNestingDepth;
@@ -84,6 +107,10 @@ public static Builder builder() {
84107
return new Builder();
85108
}
86109

110+
/**
111+
* @return the default {@link StreamWriteConstraints} (when none is set on the {@link JsonFactory} explicitly)
112+
* @see #overrideDefaultStreamWriteConstraints(StreamWriteConstraints)
113+
*/
87114
public static StreamWriteConstraints defaults() {
88115
return DEFAULT;
89116
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fasterxml.jackson.core.constraints;
2+
3+
import com.fasterxml.jackson.core.StreamWriteConstraints;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
public class StreamWriteConstraintsDefaultsTest {
9+
@Test
10+
public void testOverride() {
11+
final int depth = 123;
12+
StreamWriteConstraints constraints = StreamWriteConstraints.builder()
13+
.maxNestingDepth(depth)
14+
.build();
15+
try {
16+
StreamWriteConstraints.overrideDefaultStreamWriteConstraints(constraints);
17+
assertEquals(depth, StreamWriteConstraints.defaults().getMaxNestingDepth());
18+
} finally {
19+
StreamWriteConstraints.overrideDefaultStreamWriteConstraints(null);
20+
assertEquals(StreamWriteConstraints.DEFAULT_MAX_DEPTH,
21+
StreamWriteConstraints.defaults().getMaxNestingDepth());
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)