diff --git a/src/main/java/com/fasterxml/jackson/core/JsonLocation.java b/src/main/java/com/fasterxml/jackson/core/JsonLocation.java index d17a866ee4..969f79c5c9 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonLocation.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonLocation.java @@ -23,7 +23,7 @@ public class JsonLocation private static final long serialVersionUID = 2L; // in 2.13 /** - * @deprecated Since 2.13 use {@link ContentReference#DEFAULT_MAX_CONTENT_SNIPPET} instead + * @deprecated Since 2.13 use {@link ErrorReportConfiguration#DEFAULT_MAX_RAW_CONTENT_LENGTH} instead */ @Deprecated public static final int MAX_CONTENT_SNIPPET = 500; diff --git a/src/main/java/com/fasterxml/jackson/core/io/ContentReference.java b/src/main/java/com/fasterxml/jackson/core/io/ContentReference.java index 612dda5064..08e3a22a1c 100644 --- a/src/main/java/com/fasterxml/jackson/core/io/ContentReference.java +++ b/src/main/java/com/fasterxml/jackson/core/io/ContentReference.java @@ -1,5 +1,7 @@ package com.fasterxml.jackson.core.io; +import com.fasterxml.jackson.core.ErrorReportConfiguration; + import java.io.File; import java.io.IOException; import java.io.ObjectInputStream; @@ -52,7 +54,9 @@ public class ContentReference * logs. * * @since 2.9 + * @deprecated Since 2.16. {@link ErrorReportConfiguration.Builder#maxRawContentLength(int)} will be used instead. */ + @Deprecated public static final int DEFAULT_MAX_CONTENT_SNIPPET = 500; /** @@ -81,23 +85,56 @@ public class ContentReference */ protected final boolean _isContentTextual; + /** + * max raw content to return as configured + * + * @since 2.16 + */ + protected final int _maxRawContentLength; + /* /********************************************************************** /* Life-cycle /********************************************************************** */ + /** + * @deprecated Since 2.16. Use {@link #ContentReference(boolean, Object, ErrorReportConfiguration)} instead. + */ + @Deprecated protected ContentReference(boolean isContentTextual, Object rawContent) { - this(isContentTextual, rawContent, -1, -1); + this(isContentTextual, rawContent, -1, -1, ErrorReportConfiguration.defaults()); } + /** + * @deprecated Since 2.16. Use {@link #ContentReference(boolean, Object, int, int, ErrorReportConfiguration)} instead. + */ + @Deprecated protected ContentReference(boolean isContentTextual, Object rawContent, int offset, int length) + { + this(isContentTextual, rawContent, offset, length, ErrorReportConfiguration.defaults()); + } + + /** + * @since 2.16 + */ + protected ContentReference(boolean isContentTextual, Object rawContent, ErrorReportConfiguration errorReportConfiguration) + { + this(isContentTextual, rawContent, -1, -1, errorReportConfiguration); + } + + /** + * @since 2.16 + */ + protected ContentReference(boolean isContentTextual, Object rawContent, + int offset, int length, ErrorReportConfiguration errorReportConfiguration) { _isContentTextual = isContentTextual; _rawContent = rawContent; _offset = offset; _length = length; + _maxRawContentLength = errorReportConfiguration.getMaxRawContentLength(); } /** @@ -124,14 +161,41 @@ public static ContentReference unknown() { public static ContentReference redacted() { return REDACTED_CONTENT; } - + + + /** + * @deprecated Since 2.16. Use {@link #construct(boolean, Object, ErrorReportConfiguration)} instead. + */ + @Deprecated public static ContentReference construct(boolean isContentTextual, Object rawContent) { - return new ContentReference(isContentTextual, rawContent); + return new ContentReference(isContentTextual, rawContent, ErrorReportConfiguration.defaults()); } + /** + * @deprecated Since 2.16. Use {@link #construct(boolean, Object, int, int, ErrorReportConfiguration)} instead. + */ + @Deprecated public static ContentReference construct(boolean isContentTextual, Object rawContent, int offset, int length) { - return new ContentReference(isContentTextual, rawContent, offset, length); + return new ContentReference(isContentTextual, rawContent, offset, length, ErrorReportConfiguration.defaults()); + } + + /** + * @since 2.16 + */ + public static ContentReference construct(boolean isContentTextual, Object rawContent, + int offset, int length, ErrorReportConfiguration errorReportConfiguration) + { + return new ContentReference(isContentTextual, rawContent, offset, length, errorReportConfiguration); + } + + /** + * @since 2.16 + */ + public static ContentReference construct(boolean isContentTextual, Object rawContent, + ErrorReportConfiguration errorReportConfiguration) + { + return new ContentReference(isContentTextual, rawContent, errorReportConfiguration); } /** @@ -203,10 +267,11 @@ public Object getRawContent() { * which content is counted, either bytes or chars) to use for truncation * (so as not to include full content for humongous sources or targets) * + * @see ErrorReportConfiguration#getMaxRawContentLength() * @return Maximum content snippet to include before truncating */ - protected int maxContentSnippetLength() { - return DEFAULT_MAX_CONTENT_SNIPPET; + protected int maxRawContentLength() { + return _maxRawContentLength; } /* @@ -266,7 +331,7 @@ public StringBuilder appendSourceDescription(StringBuilder sb) String trimmed; // poor man's tuple... - final int maxLen = maxContentSnippetLength(); + final int maxLen = maxRawContentLength(); int[] offsets = new int[] { contentOffset(), contentLength() }; if (srcRef instanceof CharSequence) { diff --git a/src/test/java/com/fasterxml/jackson/core/ErrorReportConfigurationMaxRawContentLengthTest.java b/src/test/java/com/fasterxml/jackson/core/ErrorReportConfigurationMaxRawContentLengthTest.java new file mode 100644 index 0000000000..3afd9fd011 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/core/ErrorReportConfigurationMaxRawContentLengthTest.java @@ -0,0 +1,48 @@ +package com.fasterxml.jackson.core; + +import com.fasterxml.jackson.core.io.ContentReference; + +/** + * Unit tests for class {@link ErrorReportConfiguration#getMaxRawContentLength()}. + */ +public class ErrorReportConfigurationMaxRawContentLengthTest extends BaseTest { + /* + /********************************************************** + /* Unit Tests + /********************************************************** + */ + + public void testBasicToStringErrorConfig() throws Exception { + // Truncated result + _verifyToString("abc", 2, + "[Source: (String)\"ab\"[truncated 1 chars]; line: 1, column: 1]"); + // Exact length + _verifyToString("abc", 3, + "[Source: (String)\"abc\"; line: 1, column: 1]"); + // Enough length + _verifyToString("abc", 4, + "[Source: (String)\"abc\"; line: 1, column: 1]"); + } + + /* + /********************************************************** + /* Internal helper methods + /********************************************************** + */ + + private void _verifyToString(String rawSrc, int rawContentLength, String expectedMessage) { + ContentReference reference = _sourceRefWithErrorReportConfig(rawSrc, rawContentLength); + String location = new JsonLocation(reference, 10L, 10L, 1, 1).toString(); + assertEquals(expectedMessage, location); + } + + private ContentReference _sourceRefWithErrorReportConfig(String rawSrc, int rawContentLength) { + return _sourceRef(rawSrc, + ErrorReportConfiguration.builder().maxRawContentLength(rawContentLength).build()); + } + + private ContentReference _sourceRef(String rawSrc, ErrorReportConfiguration errorReportConfiguration) { + return ContentReference.construct(true, rawSrc, 0, rawSrc.length(),errorReportConfiguration); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/core/JsonLocationTest.java b/src/test/java/com/fasterxml/jackson/core/JsonLocationTest.java index 1339c546f5..9a3e94b8a6 100644 --- a/src/test/java/com/fasterxml/jackson/core/JsonLocationTest.java +++ b/src/test/java/com/fasterxml/jackson/core/JsonLocationTest.java @@ -64,7 +64,7 @@ public void testBasicToString() throws Exception public void testTruncatedSource() throws Exception { StringBuilder sb = new StringBuilder(); - for (int i = 0; i < ContentReference.DEFAULT_MAX_CONTENT_SNIPPET; ++i) { + for (int i = 0; i < ErrorReportConfiguration.DEFAULT_MAX_RAW_CONTENT_LENGTH; ++i) { sb.append("x"); } String main = sb.toString();