Skip to content

Commit

Permalink
Retrofit ContentReference with new ErrorReportConfiguration (#1070)
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim committed Aug 1, 2023
1 parent 3a4f156 commit 75b060e
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/fasterxml/jackson/core/JsonLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
79 changes: 72 additions & 7 deletions src/main/java/com/fasterxml/jackson/core/io/ContentReference.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

/*
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 75b060e

Please sign in to comment.