Skip to content

Commit d32c188

Browse files
committed
Fixed #2770
1 parent 6985fe4 commit d32c188

File tree

5 files changed

+76
-5
lines changed

5 files changed

+76
-5
lines changed

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,11 @@ João Guerra (joca-bt@github)
991991
(2.10.1)
992992
* Reported #2567: Incorrect target type for arrays when providing nulls and nulls are disabled
993993
(2.10.2)
994+
* Reported #2635: JsonParser cannot getText() for input stream on MismatchedInputException
995+
(2.11.0)
996+
* Reported #2770: JsonParser from MismatchedInputException cannot getText() for
997+
floating-point value
998+
(2.11.1)
994999
9951000
Ryan Bohn (bohnman@github)
9961001
* Reported #2475: `StringCollectionSerializer` calls `JsonGenerator.setCurrentValue(value)`,

release-notes/VERSION-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Project: jackson-databind
2020
#2760: Jackson doesn't respect `CAN_OVERRIDE_ACCESS_MODIFIERS=false` for
2121
deserializer properties
2222
(reported by Johannes K)
23+
#2770: JsonParser from MismatchedInputException cannot getText() for
24+
floating-point value
25+
(reported by João G)
2326
2427
2.11.0 (26-Apr-2020)
2528
@@ -68,6 +71,8 @@ Project: jackson-databind
6871
(reported by robotmrv@github)
6972
#2632: Failure to resolve generic type parameters on serialization
7073
(reported by Simone D)
74+
#2635: JsonParser cannot getText() for input stream on MismatchedInputException
75+
(reported by João G)
7176
#2636: ObjectReader readValue lacks Class<T> argument
7277
(contributed by Robin R)
7378
#2643: Change default textual serialization of `java.util.Date`/`Calendar`

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,10 @@ public Object handleUnexpectedToken(JavaType targetType, JsonToken t,
12351235
ClassUtil.getTypeDescription(targetType), t);
12361236
}
12371237
}
1238+
// 18-Jun-2020, tatu: to resolve [databind#2770], force access to `getText()` for scalars
1239+
if ((t != null) && t.isScalarValue()) {
1240+
p.getText();
1241+
}
12381242
reportInputMismatch(targetType, msg);
12391243
return null; // never gets here
12401244
}

src/test/java/com/fasterxml/jackson/databind/struct/ScalarCoercionTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,27 @@ public void testStringCoercionFail() throws Exception
164164
_verifyRootStringCoerceFail("123.0", BigDecimal.class);
165165
}
166166

167+
// [databind#2635], [databind#2770]
167168
public void testToBooleanCoercionFailBytes() throws Exception
168169
{
169170
final String beanDoc = aposToQuotes("{'value':1}");
170171
_verifyBooleanCoerceFail("1", true, JsonToken.VALUE_NUMBER_INT, "1", Boolean.TYPE);
171172
_verifyBooleanCoerceFail("1", true, JsonToken.VALUE_NUMBER_INT, "1", Boolean.class);
172173
_verifyBooleanCoerceFail(beanDoc, true, JsonToken.VALUE_NUMBER_INT, "1", BooleanPOJO.class);
174+
175+
_verifyBooleanCoerceFail("1.25", true, JsonToken.VALUE_NUMBER_FLOAT, "1.25", Boolean.TYPE);
176+
_verifyBooleanCoerceFail("1.25", true, JsonToken.VALUE_NUMBER_FLOAT, "1.25", Boolean.class);
173177
}
174178

179+
// [databind#2635], [databind#2770]
175180
public void testToBooleanCoercionFailChars() throws Exception
176181
{
177182
final String beanDoc = aposToQuotes("{'value':1}");
178183
_verifyBooleanCoerceFail("1", false, JsonToken.VALUE_NUMBER_INT, "1", Boolean.TYPE);
179184
_verifyBooleanCoerceFail("1", false, JsonToken.VALUE_NUMBER_INT, "1", Boolean.class);
180185
_verifyBooleanCoerceFail(beanDoc, false, JsonToken.VALUE_NUMBER_INT, "1", BooleanPOJO.class);
181186
}
182-
187+
183188
public void testMiscCoercionFail() throws Exception
184189
{
185190
// And then we have coercions from more esoteric types too
@@ -273,9 +278,8 @@ private void _verifyBooleanCoerceFail(String doc, boolean useBytes,
273278
private void _verifyBooleanCoerceFailReason(MismatchedInputException e,
274279
JsonToken tokenType, String tokenValue) throws IOException
275280
{
276-
verifyException(e, "Cannot coerce ");
277-
verifyException(e, " for type `");
278-
verifyException(e, "enable `MapperFeature.ALLOW_COERCION_OF_SCALARS` to allow");
281+
// 2 different possibilities here
282+
verifyException(e, "Cannot coerce Number", "Cannot deserialize instance of `");
279283

280284
JsonParser p = (JsonParser) e.getProcessor();
281285

@@ -286,7 +290,7 @@ private void _verifyBooleanCoerceFailReason(MismatchedInputException e,
286290
if (!tokenValue.equals(text)) {
287291
String textDesc = (text == null) ? "NULL" : quote(text);
288292
fail("Token text ("+textDesc+") via parser of type "+p.getClass().getName()
289-
+" not as expected ("+quote(tokenValue)+")");
293+
+" not as expected ("+quote(tokenValue)+"); exception message: '"+e.getMessage()+"'");
290294
}
291295
}
292296
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.util.AbstractMap;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Set;
8+
import java.util.concurrent.atomic.AtomicReference;
9+
10+
import com.fasterxml.jackson.databind.*;
11+
12+
public class ObjectMapper2757Test extends BaseMapTest
13+
{
14+
interface MultiValueMap<K, V> extends Map<K, List<V>> { }
15+
abstract static class MyMultiMap<K, V> extends AbstractMap<K, List<V>>
16+
implements MultiValueMap<K, V> { }
17+
18+
static class MyMap extends MyMultiMap<String, String> {
19+
public MyMap() { }
20+
21+
public void setValue(StringWrapper w) { }
22+
public void setValue(IntWrapper w) { }
23+
24+
public long getValue() { return 0L; }
25+
26+
@Override
27+
public Set<Entry<String, List<String>>> entrySet() {
28+
return Collections.emptySet();
29+
}
30+
}
31+
32+
private final ObjectMapper MAPPER = newJsonMapper();
33+
34+
// [databind#2757]: should allow deserialization as Map despite conflicting setters
35+
public void testCanDeserializeMap() throws Exception
36+
{
37+
// final String json = MAPPER.writeValueAsString(new MyMap());
38+
// System.out.println("json: "+json);
39+
// MyMap x = MAPPER.readValue(json, MyMap.class);
40+
final AtomicReference<Throwable> ref = new AtomicReference<>();
41+
final JavaType type = MAPPER.constructType(MyMap.class);
42+
43+
System.err.println("Type: "+type);
44+
45+
boolean can = MAPPER.canDeserialize(MAPPER.constructType(type),
46+
ref);
47+
System.err.println(" Cause -> "+ref.get());
48+
if (!can) {
49+
ref.get().printStackTrace();
50+
fail("canDeserialize() returned false; underlying failure: "+ref.get());
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)