Skip to content

Commit

Permalink
Fix a problem with enabling/disabling JsonReadFeatures for `ObjectR…
Browse files Browse the repository at this point in the history
…eader`
  • Loading branch information
cowtowncoder committed Oct 9, 2018
1 parent 735d2cc commit 3f94218
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.*;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.cfg.*;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.introspect.*;
Expand Down Expand Up @@ -498,6 +498,10 @@ public DeserializationConfig withoutFeatures(JsonParser.Feature... features)
*/
public DeserializationConfig with(FormatFeature feature)
{
// 08-Oct-2018, tatu: Alas, complexity due to newly (2.10) refactored json-features:
if (feature instanceof JsonReadFeature) {
return _withJsonReadFeatures(feature);
}
int newSet = _formatReadFeatures | feature.getMask();
int newMask = _formatReadFeaturesToChange | feature.getMask();
return ((_formatReadFeatures == newSet) && (_formatReadFeaturesToChange == newMask)) ? this :
Expand All @@ -514,6 +518,10 @@ public DeserializationConfig with(FormatFeature feature)
*/
public DeserializationConfig withFeatures(FormatFeature... features)
{
// 08-Oct-2018, tatu: Alas, complexity due to newly (2.10) refactored json-features:
if (features.length > 0 && (features[0] instanceof JsonReadFeature)) {
return _withJsonReadFeatures(features);
}
int newSet = _formatReadFeatures;
int newMask = _formatReadFeaturesToChange;
for (FormatFeature f : features) {
Expand All @@ -535,6 +543,10 @@ public DeserializationConfig withFeatures(FormatFeature... features)
*/
public DeserializationConfig without(FormatFeature feature)
{
// 08-Oct-2018, tatu: Alas, complexity due to newly (2.10) refactored json-features:
if (feature instanceof JsonReadFeature) {
return _withoutJsonReadFeatures(feature);
}
int newSet = _formatReadFeatures & ~feature.getMask();
int newMask = _formatReadFeaturesToChange | feature.getMask();
return ((_formatReadFeatures == newSet) && (_formatReadFeaturesToChange == newMask)) ? this :
Expand All @@ -551,6 +563,10 @@ public DeserializationConfig without(FormatFeature feature)
*/
public DeserializationConfig withoutFeatures(FormatFeature... features)
{
// 08-Oct-2018, tatu: Alas, complexity due to newly (2.10) refactored json-features:
if (features.length > 0 && (features[0] instanceof JsonReadFeature)) {
return _withoutJsonReadFeatures(features);
}
int newSet = _formatReadFeatures;
int newMask = _formatReadFeaturesToChange;
for (FormatFeature f : features) {
Expand All @@ -562,7 +578,61 @@ public DeserializationConfig withoutFeatures(FormatFeature... features)
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
_parserFeatures, _parserFeaturesToChange,
newSet, newMask);
}
}

// temporary for 2.10
private DeserializationConfig _withJsonReadFeatures(FormatFeature... features) {
int parserSet = _parserFeatures;
int parserMask = _parserFeaturesToChange;
int newSet = _formatReadFeatures;
int newMask = _formatReadFeaturesToChange;
for (FormatFeature f : features) {
final int mask = f.getMask();
newSet |= mask;
newMask |= mask;

if (f instanceof JsonReadFeature) {
JsonParser.Feature oldF = ((JsonReadFeature) f).mappedFeature();
if (oldF != null) {
final int pmask = oldF.getMask();
parserSet |= pmask;
parserMask |= pmask;
}
}
}
return ((_formatReadFeatures == newSet) && (_formatReadFeaturesToChange == newMask)
&& (_parserFeatures == parserSet) && (_parserFeaturesToChange == parserMask)
) ? this :
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
parserSet, parserMask, newSet, newMask);
}

// temporary for 2.10
private DeserializationConfig _withoutJsonReadFeatures(FormatFeature... features) {
int parserSet = _parserFeatures;
int parserMask = _parserFeaturesToChange;
int newSet = _formatReadFeatures;
int newMask = _formatReadFeaturesToChange;
for (FormatFeature f : features) {
final int mask = f.getMask();
newSet &= ~mask;
newMask |= mask;

if (f instanceof JsonReadFeature) {
JsonParser.Feature oldF = ((JsonReadFeature) f).mappedFeature();
if (oldF != null) {
final int pmask = oldF.getMask();
parserSet &= ~pmask;
parserMask |= pmask;
}
}
}
return ((_formatReadFeatures == newSet) && (_formatReadFeaturesToChange == newMask)
&& (_parserFeatures == parserSet) && (_parserFeaturesToChange == parserMask)
) ? this :
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
parserSet, parserMask, newSet, newMask);
}

/*
/**********************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import java.util.*;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.json.JsonReadFeature;

import com.fasterxml.jackson.databind.exc.MismatchedInputException;

public class FullStreamReadTest extends BaseMapTest
Expand Down Expand Up @@ -81,10 +82,11 @@ public void testMapperFailOnTrailing() throws Exception
verifyException(e, "maybe a (non-standard) comment");
}

ObjectMapper strictWithComments = strict.copy();
strictWithComments.enable(JsonParser.Feature.ALLOW_COMMENTS);
ObjectReader strictWithComments = strict.reader()
.with(JsonReadFeature.ALLOW_JAVA_COMMENTS);
_verifyArray(strictWithComments.readTree(JSON_OK_ARRAY_WITH_COMMENT));
_verifyCollection(strictWithComments.readValue(JSON_OK_ARRAY_WITH_COMMENT, List.class));
_verifyCollection((List<?>) strictWithComments.forType(List.class)
.readValue(JSON_OK_ARRAY_WITH_COMMENT));
}

public void testReaderAcceptTrailing() throws Exception
Expand Down Expand Up @@ -153,7 +155,7 @@ public void testReaderFailOnTrailing() throws Exception

// but works if comments enabled etc

ObjectReader strictRWithComments = strictR.with(JsonParser.Feature.ALLOW_COMMENTS);
ObjectReader strictRWithComments = strictR.with(JsonReadFeature.ALLOW_JAVA_COMMENTS);

_verifyCollection((List<?>)strictRWithComments.forType(List.class).readValue(JSON_OK_ARRAY_WITH_COMMENT));
_verifyArray(strictRWithComments.readTree(JSON_OK_ARRAY_WITH_COMMENT));
Expand Down
24 changes: 12 additions & 12 deletions src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void testParserFeatures()
ObjectMapper mapper = new ObjectMapper();

assertTrue(mapper.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
assertFalse(mapper.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
assertFalse(mapper.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));

mapper.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE,
JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
Expand All @@ -99,9 +99,9 @@ public void testCopy() throws Exception
assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
InjectableValues inj = new InjectableValues.Std();
m.setInjectableValues(inj);
assertFalse(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
m.enable(JsonParser.Feature.ALLOW_COMMENTS);
assertTrue(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
assertFalse(m.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));
m.enable(JsonParser.Feature.IGNORE_UNDEFINED);
assertTrue(m.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));

// // First: verify that handling of features is decoupled:

Expand Down Expand Up @@ -138,7 +138,7 @@ public void testCopy() throws Exception
assertEquals(0, m2.getDeserializationConfig().mixInCount());

// [databind#913]: Ensure JsonFactory Features copied
assertTrue(m2.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
assertTrue(m2.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));
}

// [databind#1580]
Expand Down Expand Up @@ -347,17 +347,17 @@ public void testCopyOfParserFeatures() throws Exception
{
// ensure we have "fresh" instance to start with
ObjectMapper mapper = new ObjectMapper();
assertFalse(mapper.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
assertTrue(mapper.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
assertFalse(mapper.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));
mapper.configure(JsonParser.Feature.IGNORE_UNDEFINED, true);
assertTrue(mapper.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));

ObjectMapper copy = mapper.copy();
assertTrue(copy.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
assertTrue(copy.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));

// also verify there's no back-linkage
copy.configure(JsonParser.Feature.ALLOW_COMMENTS, false);
assertFalse(copy.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
assertTrue(mapper.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
copy.configure(JsonParser.Feature.IGNORE_UNDEFINED, false);
assertFalse(copy.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));
assertTrue(mapper.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));
}

// since 2.8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Set;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand Down Expand Up @@ -51,7 +51,7 @@ public void testParserFeatures() throws Exception
final String JSON = "[ /* foo */ 7 ]";
// default won't accept comments, let's change that:
ObjectReader reader = MAPPER.readerFor(int[].class)
.with(JsonParser.Feature.ALLOW_COMMENTS);
.with(JsonReadFeature.ALLOW_JAVA_COMMENTS);

int[] value = reader.readValue(JSON);
assertNotNull(value);
Expand All @@ -60,7 +60,7 @@ public void testParserFeatures() throws Exception

// but also can go back
try {
reader.without(JsonParser.Feature.ALLOW_COMMENTS).readValue(JSON);
reader.without(JsonReadFeature.ALLOW_JAVA_COMMENTS).readValue(JSON);
fail("Should not have passed");
} catch (JsonProcessingException e) {
verifyException(e, "foo");
Expand All @@ -81,7 +81,7 @@ public void testFeatureSettings() throws Exception
{
ObjectReader r = MAPPER.reader();
assertFalse(r.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
assertFalse(r.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
assertFalse(r.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));

r = r.withoutFeatures(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,
DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
Expand Down

0 comments on commit 3f94218

Please sign in to comment.