Skip to content

Commit 98abb93

Browse files
authored
Fix #5179: add "currentToken" in MismatchedInputException (and sub-types) (#5180)
1 parent 9843e23 commit 98abb93

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Project: jackson-databind
1818
serialization
1919
#5151: Add new exception type, `MissingInjectValueException`, to be used
2020
for failed `@JacksonInject`
21+
#5179: Add "current token" info into `MismatchedInputException`
2122
- Generate SBOMs [JSTEP-14]
2223
2324
2.19.1 (not yet released)

src/main/java/com/fasterxml/jackson/databind/exc/MismatchedInputException.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.fasterxml.jackson.databind.exc;
22

3-
import com.fasterxml.jackson.core.JsonLocation;
4-
import com.fasterxml.jackson.core.JsonParser;
3+
import com.fasterxml.jackson.core.*;
54
import com.fasterxml.jackson.databind.JavaType;
65
import com.fasterxml.jackson.databind.JsonMappingException;
76
import com.fasterxml.jackson.databind.util.ClassUtil;
@@ -31,22 +30,36 @@ public class MismatchedInputException
3130
*/
3231
protected Class<?> _targetType;
3332

33+
/**
34+
* Current token at the point when exception was thrown (if available).
35+
*
36+
* @since 2.20
37+
*/
38+
protected JsonToken _currentToken;
39+
3440
protected MismatchedInputException(JsonParser p, String msg) {
3541
this(p, msg, (JavaType) null);
3642
}
3743

3844
protected MismatchedInputException(JsonParser p, String msg, JsonLocation loc) {
3945
super(p, msg, loc);
46+
_currentToken = _currentToken(p);
4047
}
4148

4249
protected MismatchedInputException(JsonParser p, String msg, Class<?> targetType) {
4350
super(p, msg);
4451
_targetType = targetType;
52+
_currentToken = _currentToken(p);
4553
}
4654

4755
protected MismatchedInputException(JsonParser p, String msg, JavaType targetType) {
4856
super(p, msg);
4957
_targetType = ClassUtil.rawClass(targetType);
58+
_currentToken = _currentToken(p);
59+
}
60+
61+
protected static JsonToken _currentToken(JsonParser p) {
62+
return (p == null) ? null : p.currentToken();
5063
}
5164

5265
// Only to prevent super-class static method from getting called
@@ -68,11 +81,25 @@ public MismatchedInputException setTargetType(JavaType t) {
6881
return this;
6982
}
7083

84+
public MismatchedInputException setCurrentToken(JsonToken t) {
85+
_currentToken = t;
86+
return this;
87+
}
88+
7189
/**
7290
* Accessor for getting intended target type, with which input did not match,
7391
* if known; `null` if not known for some reason.
7492
*/
7593
public Class<?> getTargetType() {
7694
return _targetType;
7795
}
96+
97+
/**
98+
* @return Current token at the point when exception was thrown, if available
99+
* ({@code null} if not)
100+
* @since 2.20
101+
*/
102+
public JsonToken getCurrentToken() {
103+
return _currentToken;
104+
}
78105
}

src/test/java/com/fasterxml/jackson/databind/convert/CoerceToBooleanTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,10 @@ private void _verifyBooleanCoerceFailReason(MismatchedInputException e,
413413
JsonToken tokenType, String tokenValue) throws IOException
414414
{
415415
verifyException(e, "Cannot coerce ", "Cannot deserialize value of type ");
416+
// 30-May-2025, tatu: [databind#5179] got access via exception now
417+
assertToken(tokenType, e.getCurrentToken());
416418

417419
JsonParser p = (JsonParser) e.getProcessor();
418-
419-
assertToken(tokenType, p.currentToken());
420-
421420
final String text = p.getText();
422421
if (!tokenValue.equals(text)) {
423422
String textDesc = (text == null) ? "NULL" : q(text);

src/test/java/com/fasterxml/jackson/databind/exc/DeserExceptionTypeTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
import com.fasterxml.jackson.core.*;
88
import com.fasterxml.jackson.databind.*;
99
import com.fasterxml.jackson.databind.testutil.BrokenStringReader;
10+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
1011

1112
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertNull;
1214
import static org.junit.jupiter.api.Assertions.fail;
1315

14-
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.newJsonMapper;
15-
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.verifyException;
16-
1716
/**
1817
* Unit test for verifying that exceptions are properly handled (caught,
1918
* re-thrown or wrapped, depending) with Object deserialization,
2019
* including using concrete subtypes of {@link DatabindException}
2120
* (and streaming-level equivalents).
2221
*/
2322
public class DeserExceptionTypeTest
23+
extends DatabindTestUtil
2424
{
2525
static class Bean {
2626
public String propX;
@@ -59,6 +59,8 @@ public void testHandlingOfUnrecognized() throws Exception
5959
assertEquals(Bean.class, exc.getReferringClass());
6060
// also: should get list of known properties
6161
verifyException(exc, "propX");
62+
// and as per [databind#5179] current token (pointing to start of value?)
63+
assertEquals(JsonToken.VALUE_NUMBER_INT, exc.getCurrentToken());
6264
}
6365

6466
/**
@@ -73,6 +75,7 @@ public void testExceptionWithEmpty() throws Exception
7375
fail("Expected an exception, but got result value: "+result);
7476
} catch (MismatchedInputException e) {
7577
verifyException(e, "No content");
78+
assertNull(e.getCurrentToken());
7679
}
7780
}
7881

@@ -106,6 +109,7 @@ public void testExceptionWithEOF() throws Exception
106109
fail("Should have gotten an exception");
107110
} catch (MismatchedInputException e) {
108111
verifyException(e, "No content");
112+
assertNull(e.getCurrentToken());
109113
}
110114
// also: should have no current token after end-of-input
111115
JsonToken t = p.currentToken();

0 commit comments

Comments
 (0)