Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ public static int collectDefaults() {
*/
protected StreamReadConstraints _streamReadConstraints;

/**
* Container for configuration values used when handling errorneous token inputs.
*
* @see ErrorReportConfiguration
* @since 2.16
*/
protected ErrorReportConfiguration _errorReportConfiguration;

/**
* Write constraints to use for {@link JsonGenerator}s constructed using
* this factory.
Expand Down Expand Up @@ -361,6 +369,7 @@ public JsonFactory(ObjectCodec oc) {
_quoteChar = DEFAULT_QUOTE_CHAR;
_streamReadConstraints = StreamReadConstraints.defaults();
_streamWriteConstraints = StreamWriteConstraints.defaults();
_errorReportConfiguration = ErrorReportConfiguration.defaults();
_generatorDecorators = null;
}

Expand All @@ -385,6 +394,7 @@ protected JsonFactory(JsonFactory src, ObjectCodec codec)
_generatorDecorators = _copy(src._generatorDecorators);
_streamReadConstraints = Objects.requireNonNull(src._streamReadConstraints);
_streamWriteConstraints = Objects.requireNonNull(src._streamWriteConstraints);
_errorReportConfiguration = Objects.requireNonNull(src._errorReportConfiguration);

// JSON-specific
_characterEscapes = src._characterEscapes;
Expand Down Expand Up @@ -412,6 +422,7 @@ public JsonFactory(JsonFactoryBuilder b) {
_generatorDecorators = _copy(b._generatorDecorators);
_streamReadConstraints = Objects.requireNonNull(b._streamReadConstraints);
_streamWriteConstraints = Objects.requireNonNull(b._streamWriteConstraints);
_errorReportConfiguration = Objects.requireNonNull(b._errorReportConfiguration);

// JSON-specific
_characterEscapes = b._characterEscapes;
Expand Down Expand Up @@ -439,6 +450,7 @@ protected JsonFactory(TSFBuilder<?,?> b, boolean bogus) {
_generatorDecorators = _copy(b._generatorDecorators);
_streamReadConstraints = Objects.requireNonNull(b._streamReadConstraints);
_streamWriteConstraints = Objects.requireNonNull(b._streamWriteConstraints);
_errorReportConfiguration = Objects.requireNonNull(b._errorReportConfiguration);

// JSON-specific: need to assign even if not really used
_characterEscapes = null;
Expand Down Expand Up @@ -838,6 +850,26 @@ public JsonFactory setStreamReadConstraints(StreamReadConstraints src) {
return this;
}

/**
* Method for overriding {@link ErrorReportConfiguration} defined for
* this factory.
*<p>
* NOTE: the preferred way to set constraints is by using
* {@link JsonFactoryBuilder#errorReportConfiguration}: this method is only
* provided to support older non-builder-based construction.
* In Jackson 3.x this method will not be available.
*
* @param src Configuration
*
* @return This factory instance (to allow call chaining)
*
* @since 2.16
*/
public JsonFactory setErrorReportConfiguration(ErrorReportConfiguration src) {
_errorReportConfiguration = Objects.requireNonNull(src, "Cannot pass null ErrorReportConfiguration");;
return this;
}

/**
* Method for overriding {@link StreamWriteConstraints} defined for
* this factory.
Expand Down Expand Up @@ -2112,7 +2144,7 @@ protected IOContext _createContext(ContentReference contentRef, boolean resource
if (contentRef == null) {
contentRef = ContentReference.unknown();
}
return new IOContext(_streamReadConstraints, _streamWriteConstraints,
return new IOContext(_streamReadConstraints, _streamWriteConstraints, _errorReportConfiguration,
_getBufferRecycler(), contentRef, resourceManaged);
}

Expand All @@ -2128,7 +2160,7 @@ protected IOContext _createContext(ContentReference contentRef, boolean resource
*/
@Deprecated // @since 2.13
protected IOContext _createContext(Object rawContentRef, boolean resourceManaged) {
return new IOContext(_streamReadConstraints, _streamWriteConstraints,
return new IOContext(_streamReadConstraints, _streamWriteConstraints, _errorReportConfiguration,
_getBufferRecycler(),
_createContentReference(rawContentRef),
resourceManaged);
Expand All @@ -2147,7 +2179,7 @@ protected IOContext _createContext(Object rawContentRef, boolean resourceManaged
protected IOContext _createNonBlockingContext(Object srcRef) {
// [jackson-core#479]: allow recycling for non-blocking parser again
// now that access is thread-safe
return new IOContext(_streamReadConstraints, _streamWriteConstraints,
return new IOContext(_streamReadConstraints, _streamWriteConstraints, _errorReportConfiguration,
_getBufferRecycler(),
_createContentReference(srcRef),
false);
Expand All @@ -2168,7 +2200,7 @@ protected IOContext _createNonBlockingContext(Object srcRef) {
protected ContentReference _createContentReference(Object contentAccessor) {
// 21-Mar-2021, tatu: For now assume "canHandleBinaryNatively()" is reliable
// indicator of textual vs binary format:
return ContentReference.construct(!canHandleBinaryNatively(), contentAccessor);
return ContentReference.construct(!canHandleBinaryNatively(), contentAccessor, _errorReportConfiguration);
}

/**
Expand All @@ -2192,7 +2224,7 @@ protected ContentReference _createContentReference(Object contentAccessor,
// 21-Mar-2021, tatu: For now assume "canHandleBinaryNatively()" is reliable
// indicator of textual vs binary format:
return ContentReference.construct(!canHandleBinaryNatively(),
contentAccessor, offset, length);
contentAccessor, offset, length, _errorReportConfiguration);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*<p>
* Since Jackson 2.12 extends intermediate {@link JacksonException} type
* instead of directly extending {@link java.io.IOException}.
*<p>
* Since Jackson 2.16, handles its content as configured using {@link com.fasterxml.jackson.core.ErrorReportConfiguration}.
*/
public class JsonProcessingException extends JacksonException
{
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/TSFBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public abstract class TSFBuilder<F extends JsonFactory,
*/
protected List<JsonGeneratorDecorator> _generatorDecorators;

/**
* Optional {@link ErrorReportConfiguration} to use.
*
* @since 2.16
*/
protected ErrorReportConfiguration _errorReportConfiguration;

/*
/**********************************************************************
/* Construction
Expand All @@ -120,6 +127,7 @@ protected TSFBuilder(JsonFactory base)
_outputDecorator = base._outputDecorator;
_streamReadConstraints = base._streamReadConstraints;
_streamWriteConstraints = base._streamWriteConstraints;
_errorReportConfiguration = base._errorReportConfiguration;
_generatorDecorators = _copy(base._generatorDecorators);
}

Expand All @@ -134,6 +142,7 @@ protected TSFBuilder(int factoryFeatures,
_outputDecorator = null;
_streamReadConstraints = StreamReadConstraints.defaults();
_streamWriteConstraints = StreamWriteConstraints.defaults();
_errorReportConfiguration = ErrorReportConfiguration.defaults();
_generatorDecorators = null;
}

Expand Down Expand Up @@ -324,6 +333,17 @@ public B streamReadConstraints(StreamReadConstraints streamReadConstraints) {
return _this();
}

/**
* Sets the configuration for error tokens.
*
* @param errorReportConfiguration configuration values used for handling errorneous token inputs.
* @return this factory
* @since 2.16
*/
public B errorReportConfiguration(ErrorReportConfiguration errorReportConfiguration) {
_errorReportConfiguration = errorReportConfiguration;
return _this();
}
/**
* Sets the constraints for streaming writes.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.fasterxml.jackson.core;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Unit tests for class {@link ErrorReportConfiguration#getMaxErrorTokenLength()}.
*/
public class ErrorReportConfigurationMaxErrorTokenLengthTest extends BaseTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about combine various tests related to ErrorReportConfiguration, instead of separate test classes? Or at least these two.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for sure 👍🏻

{

// Should be 256
public final static int DEFAULT_LENGTH = ErrorReportConfiguration.DEFAULT_MAX_ERROR_TOKEN_LENGTH;

/*
/**********************************************************
/* Unit Tests
/**********************************************************
*/

public void testExpectedTokenLengthWithConfigurations()
throws Exception
{
// default
_verifyErrorTokenLength(263,
ErrorReportConfiguration.builder().build());

// default
_verifyErrorTokenLength(263,
ErrorReportConfiguration.defaults());

// shorter
_verifyErrorTokenLength(63,
ErrorReportConfiguration.builder()
.maxErrorTokenLength(DEFAULT_LENGTH - 200).build());

// longer
_verifyErrorTokenLength(463,
ErrorReportConfiguration.builder()
.maxErrorTokenLength(DEFAULT_LENGTH + 200).build());

// zero
_verifyErrorTokenLength(9,
ErrorReportConfiguration.builder()
.maxErrorTokenLength(0).build());

// negative value fails
try {
_verifyErrorTokenLength(9,
ErrorReportConfiguration.builder()
.maxErrorTokenLength(-1).build());
} catch (IllegalArgumentException e) {
_verifyIllegalArgumentExceptionMessage(e.getMessage());
}

// null is not allowed
try {
_verifyErrorTokenLength(263,
null);
} catch (NullPointerException e) {
// no-op
}
}

public void testNegativeBuilderConfiguration()
{
// Zero should be ok
ErrorReportConfiguration.builder().maxErrorTokenLength(0).build();

// But not -1
try {
ErrorReportConfiguration.builder().maxErrorTokenLength(-1).build();
fail();
} catch (IllegalArgumentException e) {
_verifyIllegalArgumentExceptionMessage(e.getMessage());
}
}

public void testNullSetterThrowsException() {
try {
newStreamFactory().setErrorReportConfiguration(null);
fail();
} catch (NullPointerException npe) {
assertThat(npe).hasMessage("Cannot pass null ErrorReportConfiguration");
}
}

/*
/**********************************************************
/* Internal helper methods
/**********************************************************
*/

private void _verifyIllegalArgumentExceptionMessage(String message) {
assertThat(message)
.contains("Value of maxErrorTokenLength")
.contains("cannot be negative");
}

private void _verifyErrorTokenLength(int expectedTokenLen, ErrorReportConfiguration errorReportConfiguration)
throws Exception
{
JsonFactory jf3 = streamFactoryBuilder()
.errorReportConfiguration(errorReportConfiguration)
.build();
_testWithMaxErrorTokenLength(expectedTokenLen,
// creating arbitrary number so that token reaches max len, but not over-do it
50 * DEFAULT_LENGTH, jf3);
}

private void _testWithMaxErrorTokenLength(int expectedSize, int tokenLen, JsonFactory factory)
throws Exception
{
String inputWithDynamicLength = _buildBrokenJsonOfLength(tokenLen);
try (JsonParser parser = factory.createParser(inputWithDynamicLength)) {
parser.nextToken();
parser.nextToken();
} catch (JsonProcessingException e) {
assertThat(e.getLocation()._totalChars).isEqualTo(expectedSize);
assertThat(e.getMessage()).contains("Unrecognized token");
}
}

private String _buildBrokenJsonOfLength(int len)
{
StringBuilder sb = new StringBuilder("{\"key\":");
for (int i = 0; i < len; i++) {
sb.append("a");
}
sb.append("!}");
return sb.toString();
}
}
Loading