Skip to content

Commit 75b060e

Browse files
authored
Retrofit ContentReference with new ErrorReportConfiguration (#1070)
1 parent 3a4f156 commit 75b060e

File tree

4 files changed

+122
-9
lines changed

4 files changed

+122
-9
lines changed

src/main/java/com/fasterxml/jackson/core/JsonLocation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class JsonLocation
2323
private static final long serialVersionUID = 2L; // in 2.13
2424

2525
/**
26-
* @deprecated Since 2.13 use {@link ContentReference#DEFAULT_MAX_CONTENT_SNIPPET} instead
26+
* @deprecated Since 2.13 use {@link ErrorReportConfiguration#DEFAULT_MAX_RAW_CONTENT_LENGTH} instead
2727
*/
2828
@Deprecated
2929
public static final int MAX_CONTENT_SNIPPET = 500;

src/main/java/com/fasterxml/jackson/core/io/ContentReference.java

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fasterxml.jackson.core.io;
22

3+
import com.fasterxml.jackson.core.ErrorReportConfiguration;
4+
35
import java.io.File;
46
import java.io.IOException;
57
import java.io.ObjectInputStream;
@@ -52,7 +54,9 @@ public class ContentReference
5254
* logs.
5355
*
5456
* @since 2.9
57+
* @deprecated Since 2.16. {@link ErrorReportConfiguration.Builder#maxRawContentLength(int)} will be used instead.
5558
*/
59+
@Deprecated
5660
public static final int DEFAULT_MAX_CONTENT_SNIPPET = 500;
5761

5862
/**
@@ -81,23 +85,56 @@ public class ContentReference
8185
*/
8286
protected final boolean _isContentTextual;
8387

88+
/**
89+
* max raw content to return as configured
90+
*
91+
* @since 2.16
92+
*/
93+
protected final int _maxRawContentLength;
94+
8495
/*
8596
/**********************************************************************
8697
/* Life-cycle
8798
/**********************************************************************
8899
*/
89100

101+
/**
102+
* @deprecated Since 2.16. Use {@link #ContentReference(boolean, Object, ErrorReportConfiguration)} instead.
103+
*/
104+
@Deprecated
90105
protected ContentReference(boolean isContentTextual, Object rawContent) {
91-
this(isContentTextual, rawContent, -1, -1);
106+
this(isContentTextual, rawContent, -1, -1, ErrorReportConfiguration.defaults());
92107
}
93108

109+
/**
110+
* @deprecated Since 2.16. Use {@link #ContentReference(boolean, Object, int, int, ErrorReportConfiguration)} instead.
111+
*/
112+
@Deprecated
94113
protected ContentReference(boolean isContentTextual, Object rawContent,
95114
int offset, int length)
115+
{
116+
this(isContentTextual, rawContent, offset, length, ErrorReportConfiguration.defaults());
117+
}
118+
119+
/**
120+
* @since 2.16
121+
*/
122+
protected ContentReference(boolean isContentTextual, Object rawContent, ErrorReportConfiguration errorReportConfiguration)
123+
{
124+
this(isContentTextual, rawContent, -1, -1, errorReportConfiguration);
125+
}
126+
127+
/**
128+
* @since 2.16
129+
*/
130+
protected ContentReference(boolean isContentTextual, Object rawContent,
131+
int offset, int length, ErrorReportConfiguration errorReportConfiguration)
96132
{
97133
_isContentTextual = isContentTextual;
98134
_rawContent = rawContent;
99135
_offset = offset;
100136
_length = length;
137+
_maxRawContentLength = errorReportConfiguration.getMaxRawContentLength();
101138
}
102139

103140
/**
@@ -124,14 +161,41 @@ public static ContentReference unknown() {
124161
public static ContentReference redacted() {
125162
return REDACTED_CONTENT;
126163
}
127-
164+
165+
166+
/**
167+
* @deprecated Since 2.16. Use {@link #construct(boolean, Object, ErrorReportConfiguration)} instead.
168+
*/
169+
@Deprecated
128170
public static ContentReference construct(boolean isContentTextual, Object rawContent) {
129-
return new ContentReference(isContentTextual, rawContent);
171+
return new ContentReference(isContentTextual, rawContent, ErrorReportConfiguration.defaults());
130172
}
131173

174+
/**
175+
* @deprecated Since 2.16. Use {@link #construct(boolean, Object, int, int, ErrorReportConfiguration)} instead.
176+
*/
177+
@Deprecated
132178
public static ContentReference construct(boolean isContentTextual, Object rawContent,
133179
int offset, int length) {
134-
return new ContentReference(isContentTextual, rawContent, offset, length);
180+
return new ContentReference(isContentTextual, rawContent, offset, length, ErrorReportConfiguration.defaults());
181+
}
182+
183+
/**
184+
* @since 2.16
185+
*/
186+
public static ContentReference construct(boolean isContentTextual, Object rawContent,
187+
int offset, int length, ErrorReportConfiguration errorReportConfiguration)
188+
{
189+
return new ContentReference(isContentTextual, rawContent, offset, length, errorReportConfiguration);
190+
}
191+
192+
/**
193+
* @since 2.16
194+
*/
195+
public static ContentReference construct(boolean isContentTextual, Object rawContent,
196+
ErrorReportConfiguration errorReportConfiguration)
197+
{
198+
return new ContentReference(isContentTextual, rawContent, errorReportConfiguration);
135199
}
136200

137201
/**
@@ -203,10 +267,11 @@ public Object getRawContent() {
203267
* which content is counted, either bytes or chars) to use for truncation
204268
* (so as not to include full content for humongous sources or targets)
205269
*
270+
* @see ErrorReportConfiguration#getMaxRawContentLength()
206271
* @return Maximum content snippet to include before truncating
207272
*/
208-
protected int maxContentSnippetLength() {
209-
return DEFAULT_MAX_CONTENT_SNIPPET;
273+
protected int maxRawContentLength() {
274+
return _maxRawContentLength;
210275
}
211276

212277
/*
@@ -266,7 +331,7 @@ public StringBuilder appendSourceDescription(StringBuilder sb)
266331
String trimmed;
267332

268333
// poor man's tuple...
269-
final int maxLen = maxContentSnippetLength();
334+
final int maxLen = maxRawContentLength();
270335
int[] offsets = new int[] { contentOffset(), contentLength() };
271336

272337
if (srcRef instanceof CharSequence) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fasterxml.jackson.core;
2+
3+
import com.fasterxml.jackson.core.io.ContentReference;
4+
5+
/**
6+
* Unit tests for class {@link ErrorReportConfiguration#getMaxRawContentLength()}.
7+
*/
8+
public class ErrorReportConfigurationMaxRawContentLengthTest extends BaseTest {
9+
/*
10+
/**********************************************************
11+
/* Unit Tests
12+
/**********************************************************
13+
*/
14+
15+
public void testBasicToStringErrorConfig() throws Exception {
16+
// Truncated result
17+
_verifyToString("abc", 2,
18+
"[Source: (String)\"ab\"[truncated 1 chars]; line: 1, column: 1]");
19+
// Exact length
20+
_verifyToString("abc", 3,
21+
"[Source: (String)\"abc\"; line: 1, column: 1]");
22+
// Enough length
23+
_verifyToString("abc", 4,
24+
"[Source: (String)\"abc\"; line: 1, column: 1]");
25+
}
26+
27+
/*
28+
/**********************************************************
29+
/* Internal helper methods
30+
/**********************************************************
31+
*/
32+
33+
private void _verifyToString(String rawSrc, int rawContentLength, String expectedMessage) {
34+
ContentReference reference = _sourceRefWithErrorReportConfig(rawSrc, rawContentLength);
35+
String location = new JsonLocation(reference, 10L, 10L, 1, 1).toString();
36+
assertEquals(expectedMessage, location);
37+
}
38+
39+
private ContentReference _sourceRefWithErrorReportConfig(String rawSrc, int rawContentLength) {
40+
return _sourceRef(rawSrc,
41+
ErrorReportConfiguration.builder().maxRawContentLength(rawContentLength).build());
42+
}
43+
44+
private ContentReference _sourceRef(String rawSrc, ErrorReportConfiguration errorReportConfiguration) {
45+
return ContentReference.construct(true, rawSrc, 0, rawSrc.length(),errorReportConfiguration);
46+
}
47+
48+
}

src/test/java/com/fasterxml/jackson/core/JsonLocationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void testBasicToString() throws Exception
6464
public void testTruncatedSource() throws Exception
6565
{
6666
StringBuilder sb = new StringBuilder();
67-
for (int i = 0; i < ContentReference.DEFAULT_MAX_CONTENT_SNIPPET; ++i) {
67+
for (int i = 0; i < ErrorReportConfiguration.DEFAULT_MAX_RAW_CONTENT_LENGTH; ++i) {
6868
sb.append("x");
6969
}
7070
String main = sb.toString();

0 commit comments

Comments
 (0)