Skip to content

Commit

Permalink
Merge branch '2.16'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 12, 2023
2 parents 86640ec + cdc737a commit ce66570
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/main/java/tools/jackson/core/StreamWriteConstraints.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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 <code>ObjectMapper</code> instances that have a {@link JsonFactory} instance with
* the required StreamWriteConstraints.
* <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 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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit ce66570

Please sign in to comment.