Skip to content

Commit 00b5991

Browse files
committed
Complete test improvements for full verification of check-the-full-stream feature
1 parent df8d6be commit 00b5991

File tree

2 files changed

+120
-9
lines changed

2 files changed

+120
-9
lines changed

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,21 +1682,28 @@ protected final JsonNode _bindAsTree(JsonParser p) throws IOException
16821682
return _config.getNodeFactory().missingNode();
16831683
}
16841684
}
1685+
final DeserializationContext ctxt;
16851686
final JsonNode resultNode;
1687+
final boolean checkTrailing = _config.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
1688+
16861689
if (t == JsonToken.VALUE_NULL) {
16871690
resultNode = _config.getNodeFactory().nullNode();
1691+
if (!checkTrailing) {
1692+
return resultNode;
1693+
}
1694+
ctxt = createDeserializationContext(p);
16881695
} else {
1689-
final DeserializationContext ctxt = createDeserializationContext(p);
1696+
ctxt = createDeserializationContext(p);
16901697
final JsonDeserializer<Object> deser = _findTreeDeserializer(ctxt);
16911698
if (_unwrapRoot) {
16921699
resultNode = (JsonNode) _unwrapAndDeserialize(p, ctxt, JSON_NODE_TYPE, deser);
16931700
} else {
16941701
resultNode = (JsonNode) deser.deserialize(p, ctxt);
1695-
if (_config.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)) {
1696-
_verifyNoTrailingTokens(p, ctxt, JSON_NODE_TYPE);
1697-
}
16981702
}
16991703
}
1704+
if (_config.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)) {
1705+
_verifyNoTrailingTokens(p, ctxt, JSON_NODE_TYPE);
1706+
}
17001707
return resultNode;
17011708
}
17021709

@@ -1717,21 +1724,27 @@ protected final JsonNode _bindAsTreeOrNull(JsonParser p) throws IOException
17171724
return null;
17181725
}
17191726
}
1727+
final DeserializationContext ctxt;
17201728
final JsonNode resultNode;
1729+
final boolean checkTrailing = _config.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
17211730
if (t == JsonToken.VALUE_NULL) {
17221731
resultNode = _config.getNodeFactory().nullNode();
1732+
if (!checkTrailing) {
1733+
return resultNode;
1734+
}
1735+
ctxt = createDeserializationContext(p);
17231736
} else {
1724-
final DeserializationContext ctxt = createDeserializationContext(p);
1737+
ctxt = createDeserializationContext(p);
17251738
final JsonDeserializer<Object> deser = _findTreeDeserializer(ctxt);
17261739
if (_unwrapRoot) {
17271740
resultNode = (JsonNode) _unwrapAndDeserialize(p, ctxt, JSON_NODE_TYPE, deser);
17281741
} else {
17291742
resultNode = (JsonNode) deser.deserialize(p, ctxt);
1730-
if (_config.isEnabled(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)) {
1731-
_verifyNoTrailingTokens(p, ctxt, JSON_NODE_TYPE);
1732-
}
17331743
}
17341744
}
1745+
if (checkTrailing) {
1746+
_verifyNoTrailingTokens(p, ctxt, JSON_NODE_TYPE);
1747+
}
17351748
return resultNode;
17361749
}
17371750

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

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,57 @@ public void testMapperFailOnTrailing() throws Exception
112112

113113
public void testMapperFailOnTrailingWithNull() throws Exception
114114
{
115-
// !!! TODO
115+
final ObjectMapper strict = newObjectMapper()
116+
.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
117+
118+
// some still ok
119+
JsonNode n = strict.readTree(JSON_OK_NULL);
120+
assertNotNull(n);
121+
assertTrue(n.isNull());
122+
123+
// but if real content exists, will fail
124+
try {
125+
strict.readTree(JSON_FAIL_NULL);
126+
fail("Should not have passed");
127+
} catch (MismatchedInputException e) {
128+
verifyException(e, "Trailing token (of type VALUE_FALSE)");
129+
verifyException(e, "value (bound as `com.fasterxml.jackson.databind.JsonNode`)");
130+
}
131+
132+
try {
133+
strict.readValue(JSON_FAIL_NULL, List.class);
134+
fail("Should not have passed");
135+
} catch (MismatchedInputException e) {
136+
verifyException(e, "Trailing token (of type VALUE_FALSE)");
137+
verifyException(e, "value (bound as `java.util.List`)");
138+
}
139+
140+
// others fail conditionally: will fail on comments unless enabled
141+
142+
try {
143+
strict.readValue(JSON_OK_NULL_WITH_COMMENT, Object.class);
144+
fail("Should not have passed");
145+
} catch (JsonParseException e) {
146+
verifyException(e, "Unexpected character");
147+
verifyException(e, "maybe a (non-standard) comment");
148+
}
149+
try {
150+
strict.readTree(JSON_OK_NULL_WITH_COMMENT);
151+
fail("Should not have passed");
152+
} catch (JsonParseException e) {
153+
verifyException(e, "Unexpected character");
154+
verifyException(e, "maybe a (non-standard) comment");
155+
}
156+
157+
ObjectReader strictWithComments = strict.reader()
158+
.with(JsonReadFeature.ALLOW_JAVA_COMMENTS);
159+
n = strictWithComments.readTree(JSON_OK_NULL);
160+
assertNotNull(n);
161+
assertTrue(n.isNull());
162+
163+
Object ob = strictWithComments.forType(List.class)
164+
.readValue(JSON_OK_NULL_WITH_COMMENT);
165+
assertNull(ob);
116166
}
117167

118168
public void testReaderAcceptTrailing() throws Exception
@@ -187,6 +237,54 @@ public void testReaderFailOnTrailing() throws Exception
187237
_verifyArray(strictRWithComments.readTree(JSON_OK_ARRAY_WITH_COMMENT));
188238
}
189239

240+
public void testReaderFailOnTrailingWithNull() throws Exception
241+
{
242+
ObjectReader strictR = MAPPER.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
243+
ObjectReader strictRForList = strictR.forType(List.class);
244+
JsonNode n = strictR.readTree(JSON_OK_NULL);
245+
assertTrue(n.isNull());
246+
247+
// Will fail hard if there is a trailing token
248+
try {
249+
strictRForList.readValue(JSON_FAIL_NULL);
250+
fail("Should not have passed");
251+
} catch (MismatchedInputException e) {
252+
verifyException(e, "Trailing token (of type VALUE_FALSE)");
253+
verifyException(e, "value (bound as `java.util.List`)");
254+
}
255+
256+
try {
257+
strictR.readTree(JSON_FAIL_NULL);
258+
fail("Should not have passed");
259+
} catch (MismatchedInputException e) {
260+
verifyException(e, "Trailing token (of type VALUE_FALSE)");
261+
verifyException(e, "value (bound as `com.fasterxml.jackson.databind.JsonNode`)");
262+
}
263+
264+
// others conditionally: will fail on comments unless enabled
265+
266+
try {
267+
strictRForList.readValue(JSON_OK_NULL_WITH_COMMENT);
268+
fail("Should not have passed");
269+
} catch (JsonParseException e) {
270+
verifyException(e, "Unexpected character");
271+
verifyException(e, "maybe a (non-standard) comment");
272+
}
273+
try {
274+
strictR.readTree(JSON_OK_NULL_WITH_COMMENT);
275+
fail("Should not have passed");
276+
} catch (JsonParseException e) {
277+
verifyException(e, "Unexpected character");
278+
verifyException(e, "maybe a (non-standard) comment");
279+
}
280+
281+
// but works if comments enabled etc
282+
283+
ObjectReader strictRWithComments = strictR.with(JsonReadFeature.ALLOW_JAVA_COMMENTS);
284+
Object ob = strictRWithComments.forType(List.class).readValue(JSON_OK_NULL_WITH_COMMENT);
285+
assertNull(ob);
286+
}
287+
190288
private void _verifyArray(JsonNode n) throws Exception
191289
{
192290
assertTrue(n.isArray());

0 commit comments

Comments
 (0)