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
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.1
*/
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