Skip to content

Commit

Permalink
Allow override of StreamReadContraints default with `overrideDefaul…
Browse files Browse the repository at this point in the history
…tStreamReadConstraints()` (#1019)
  • Loading branch information
pjfanning committed May 22, 2023
1 parent 8c3ea4f commit 12824dd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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 <code>ObjectMapper</code> instances that have a {@link JsonFactory} instance with
* the required StreamReadConstraints.
* <p>
* 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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit 12824dd

Please sign in to comment.