Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow override of StreamReadContraints default with overrideDefaultStreamReadConstraints() #1019

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
}