diff --git a/src/main/java/tools/jackson/core/StreamWriteConstraints.java b/src/main/java/tools/jackson/core/StreamWriteConstraints.java index cb25f32ac4..155f2eca0a 100644 --- a/src/main/java/tools/jackson/core/StreamWriteConstraints.java +++ b/src/main/java/tools/jackson/core/StreamWriteConstraints.java @@ -30,8 +30,31 @@ public class StreamWriteConstraints protected final int _maxNestingDepth; - private static final StreamWriteConstraints DEFAULT = - new StreamWriteConstraints(DEFAULT_MAX_DEPTH); + private static StreamWriteConstraints DEFAULT = new StreamWriteConstraints(DEFAULT_MAX_DEPTH); + + /** + * Override the default StreamWriteConstraints. These defaults are only used when {@link JsonFactory} + * instances are not configured with their own StreamWriteConstraints. + *

+ * Library maintainers should not set this as it will affect other code that uses Jackson. + * Library maintainers who want to configure StreamWriteConstraints for the Jackson usage within their + * lib should create ObjectMapper instances that have a {@link JsonFactory} instance with + * the required StreamWriteConstraints. + *

+ * 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 streamWriteConstraints new default for StreamWriteConstraints (a null value will reset to built-in default) + * @see #defaults() + * @see #builder() + */ + public static void overrideDefaultStreamWriteConstraints(final StreamWriteConstraints streamWriteConstraints) { + if (streamWriteConstraints == null) { + DEFAULT = new StreamWriteConstraints(DEFAULT_MAX_DEPTH); + } else { + DEFAULT = streamWriteConstraints; + } + } public static final class Builder { private int maxNestingDepth; @@ -84,6 +107,10 @@ public static Builder builder() { return new Builder(); } + /** + * @return the default {@link StreamWriteConstraints} (when none is set on the {@link JsonFactory} explicitly) + * @see #overrideDefaultStreamWriteConstraints(StreamWriteConstraints) + */ public static StreamWriteConstraints defaults() { return DEFAULT; } diff --git a/src/test/java/tools/jackson/core/constraints/StreamWriteConstraintsDefaultsTest.java b/src/test/java/tools/jackson/core/constraints/StreamWriteConstraintsDefaultsTest.java new file mode 100644 index 0000000000..a2ef032341 --- /dev/null +++ b/src/test/java/tools/jackson/core/constraints/StreamWriteConstraintsDefaultsTest.java @@ -0,0 +1,25 @@ +package tools.jackson.core.constraints; + +import org.junit.Test; + +import tools.jackson.core.StreamWriteConstraints; + +import static org.junit.Assert.assertEquals; + +public class StreamWriteConstraintsDefaultsTest { + @Test + public void testOverride() { + final int depth = 123; + StreamWriteConstraints constraints = StreamWriteConstraints.builder() + .maxNestingDepth(depth) + .build(); + try { + StreamWriteConstraints.overrideDefaultStreamWriteConstraints(constraints); + assertEquals(depth, StreamWriteConstraints.defaults().getMaxNestingDepth()); + } finally { + StreamWriteConstraints.overrideDefaultStreamWriteConstraints(null); + assertEquals(StreamWriteConstraints.DEFAULT_MAX_DEPTH, + StreamWriteConstraints.defaults().getMaxNestingDepth()); + } + } +}