Skip to content

Commit 3f94218

Browse files
committed
Fix a problem with enabling/disabling JsonReadFeatures for ObjectReader
1 parent 735d2cc commit 3f94218

File tree

4 files changed

+95
-23
lines changed

4 files changed

+95
-23
lines changed

src/main/java/com/fasterxml/jackson/databind/DeserializationConfig.java

+72-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.*;
44

55
import com.fasterxml.jackson.core.*;
6-
6+
import com.fasterxml.jackson.core.json.JsonReadFeature;
77
import com.fasterxml.jackson.databind.cfg.*;
88
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
99
import com.fasterxml.jackson.databind.introspect.*;
@@ -498,6 +498,10 @@ public DeserializationConfig withoutFeatures(JsonParser.Feature... features)
498498
*/
499499
public DeserializationConfig with(FormatFeature feature)
500500
{
501+
// 08-Oct-2018, tatu: Alas, complexity due to newly (2.10) refactored json-features:
502+
if (feature instanceof JsonReadFeature) {
503+
return _withJsonReadFeatures(feature);
504+
}
501505
int newSet = _formatReadFeatures | feature.getMask();
502506
int newMask = _formatReadFeaturesToChange | feature.getMask();
503507
return ((_formatReadFeatures == newSet) && (_formatReadFeaturesToChange == newMask)) ? this :
@@ -514,6 +518,10 @@ public DeserializationConfig with(FormatFeature feature)
514518
*/
515519
public DeserializationConfig withFeatures(FormatFeature... features)
516520
{
521+
// 08-Oct-2018, tatu: Alas, complexity due to newly (2.10) refactored json-features:
522+
if (features.length > 0 && (features[0] instanceof JsonReadFeature)) {
523+
return _withJsonReadFeatures(features);
524+
}
517525
int newSet = _formatReadFeatures;
518526
int newMask = _formatReadFeaturesToChange;
519527
for (FormatFeature f : features) {
@@ -535,6 +543,10 @@ public DeserializationConfig withFeatures(FormatFeature... features)
535543
*/
536544
public DeserializationConfig without(FormatFeature feature)
537545
{
546+
// 08-Oct-2018, tatu: Alas, complexity due to newly (2.10) refactored json-features:
547+
if (feature instanceof JsonReadFeature) {
548+
return _withoutJsonReadFeatures(feature);
549+
}
538550
int newSet = _formatReadFeatures & ~feature.getMask();
539551
int newMask = _formatReadFeaturesToChange | feature.getMask();
540552
return ((_formatReadFeatures == newSet) && (_formatReadFeaturesToChange == newMask)) ? this :
@@ -551,6 +563,10 @@ public DeserializationConfig without(FormatFeature feature)
551563
*/
552564
public DeserializationConfig withoutFeatures(FormatFeature... features)
553565
{
566+
// 08-Oct-2018, tatu: Alas, complexity due to newly (2.10) refactored json-features:
567+
if (features.length > 0 && (features[0] instanceof JsonReadFeature)) {
568+
return _withoutJsonReadFeatures(features);
569+
}
554570
int newSet = _formatReadFeatures;
555571
int newMask = _formatReadFeaturesToChange;
556572
for (FormatFeature f : features) {
@@ -562,7 +578,61 @@ public DeserializationConfig withoutFeatures(FormatFeature... features)
562578
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
563579
_parserFeatures, _parserFeaturesToChange,
564580
newSet, newMask);
565-
}
581+
}
582+
583+
// temporary for 2.10
584+
private DeserializationConfig _withJsonReadFeatures(FormatFeature... features) {
585+
int parserSet = _parserFeatures;
586+
int parserMask = _parserFeaturesToChange;
587+
int newSet = _formatReadFeatures;
588+
int newMask = _formatReadFeaturesToChange;
589+
for (FormatFeature f : features) {
590+
final int mask = f.getMask();
591+
newSet |= mask;
592+
newMask |= mask;
593+
594+
if (f instanceof JsonReadFeature) {
595+
JsonParser.Feature oldF = ((JsonReadFeature) f).mappedFeature();
596+
if (oldF != null) {
597+
final int pmask = oldF.getMask();
598+
parserSet |= pmask;
599+
parserMask |= pmask;
600+
}
601+
}
602+
}
603+
return ((_formatReadFeatures == newSet) && (_formatReadFeaturesToChange == newMask)
604+
&& (_parserFeatures == parserSet) && (_parserFeaturesToChange == parserMask)
605+
) ? this :
606+
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
607+
parserSet, parserMask, newSet, newMask);
608+
}
609+
610+
// temporary for 2.10
611+
private DeserializationConfig _withoutJsonReadFeatures(FormatFeature... features) {
612+
int parserSet = _parserFeatures;
613+
int parserMask = _parserFeaturesToChange;
614+
int newSet = _formatReadFeatures;
615+
int newMask = _formatReadFeaturesToChange;
616+
for (FormatFeature f : features) {
617+
final int mask = f.getMask();
618+
newSet &= ~mask;
619+
newMask |= mask;
620+
621+
if (f instanceof JsonReadFeature) {
622+
JsonParser.Feature oldF = ((JsonReadFeature) f).mappedFeature();
623+
if (oldF != null) {
624+
final int pmask = oldF.getMask();
625+
parserSet &= ~pmask;
626+
parserMask |= pmask;
627+
}
628+
}
629+
}
630+
return ((_formatReadFeatures == newSet) && (_formatReadFeaturesToChange == newMask)
631+
&& (_parserFeatures == parserSet) && (_parserFeaturesToChange == parserMask)
632+
) ? this :
633+
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
634+
parserSet, parserMask, newSet, newMask);
635+
}
566636

567637
/*
568638
/**********************************************************

src/test/java/com/fasterxml/jackson/databind/FullStreamReadTest.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import java.util.*;
44

55
import com.fasterxml.jackson.core.JsonParseException;
6-
import com.fasterxml.jackson.core.JsonParser;
6+
import com.fasterxml.jackson.core.json.JsonReadFeature;
7+
78
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
89

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

84-
ObjectMapper strictWithComments = strict.copy();
85-
strictWithComments.enable(JsonParser.Feature.ALLOW_COMMENTS);
85+
ObjectReader strictWithComments = strict.reader()
86+
.with(JsonReadFeature.ALLOW_JAVA_COMMENTS);
8687
_verifyArray(strictWithComments.readTree(JSON_OK_ARRAY_WITH_COMMENT));
87-
_verifyCollection(strictWithComments.readValue(JSON_OK_ARRAY_WITH_COMMENT, List.class));
88+
_verifyCollection((List<?>) strictWithComments.forType(List.class)
89+
.readValue(JSON_OK_ARRAY_WITH_COMMENT));
8890
}
8991

9092
public void testReaderAcceptTrailing() throws Exception
@@ -153,7 +155,7 @@ public void testReaderFailOnTrailing() throws Exception
153155

154156
// but works if comments enabled etc
155157

156-
ObjectReader strictRWithComments = strictR.with(JsonParser.Feature.ALLOW_COMMENTS);
158+
ObjectReader strictRWithComments = strictR.with(JsonReadFeature.ALLOW_JAVA_COMMENTS);
157159

158160
_verifyCollection((List<?>)strictRWithComments.forType(List.class).readValue(JSON_OK_ARRAY_WITH_COMMENT));
159161
_verifyArray(strictRWithComments.readTree(JSON_OK_ARRAY_WITH_COMMENT));

src/test/java/com/fasterxml/jackson/databind/ObjectMapperTest.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void testParserFeatures()
7777
ObjectMapper mapper = new ObjectMapper();
7878

7979
assertTrue(mapper.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
80-
assertFalse(mapper.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
80+
assertFalse(mapper.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));
8181

8282
mapper.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE,
8383
JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
@@ -99,9 +99,9 @@ public void testCopy() throws Exception
9999
assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
100100
InjectableValues inj = new InjectableValues.Std();
101101
m.setInjectableValues(inj);
102-
assertFalse(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
103-
m.enable(JsonParser.Feature.ALLOW_COMMENTS);
104-
assertTrue(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
102+
assertFalse(m.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));
103+
m.enable(JsonParser.Feature.IGNORE_UNDEFINED);
104+
assertTrue(m.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));
105105

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

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

140140
// [databind#913]: Ensure JsonFactory Features copied
141-
assertTrue(m2.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
141+
assertTrue(m2.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));
142142
}
143143

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

354354
ObjectMapper copy = mapper.copy();
355-
assertTrue(copy.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
355+
assertTrue(copy.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));
356356

357357
// also verify there's no back-linkage
358-
copy.configure(JsonParser.Feature.ALLOW_COMMENTS, false);
359-
assertFalse(copy.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
360-
assertTrue(mapper.isEnabled(JsonParser.Feature.ALLOW_COMMENTS));
358+
copy.configure(JsonParser.Feature.IGNORE_UNDEFINED, false);
359+
assertFalse(copy.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));
360+
assertTrue(mapper.isEnabled(JsonParser.Feature.IGNORE_UNDEFINED));
361361
}
362362

363363
// since 2.8

src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.Set;
88

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

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

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

8686
r = r.withoutFeatures(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,
8787
DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);

0 commit comments

Comments
 (0)