FasterXML / FasterXML/jackson-databind

Current token incorrectly reported when using `@JsonTypeInfo`

Open
#6,091 1 comment 0 reactions 0 assignees View on GitHub
to-evaluate
Dominant language
Java
Stars
3.7k
Forks
1.5k
Avg merge
3d 6h
Merged PRs (30d)
28

Description

### Search before asking

- [x] I searched in the [issues](https://github.com/FasterXML/jackson-databind/issues) and found nothing similar.

### Describe the bug

A MismatchedInputException gives access to the currentToken of the parser. When using a @JsonTypeInfo with defaultImpl the currentToken without the property is 'END_OBJECT' even if the actual mismatch is at a completely different location and thus token.

### Version Information

Tested the example with 2.21.0, 2.21.5 and 2.22.1

### Reproduction

<-- Any of the following
1. Brief code sample/snippet: include here in preformatted/code section
2. Longer example stored somewhere else (diff repo, snippet), add a link
3. Textual explanation: include here
-->
I have the following minimal example type ([attached test DefaultImplCurrentTokenTest.java](https://github.com/user-attachments/files/29970173/DefaultImplCurrentTokenTest.java))
```java
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type",
defaultImpl = Payload.class)
class Payload {
public int count;
}
```
(Note: The actual use case has a more sensible structure with inheritance and multiple subtypes.)

In all the following cases does it result in a MismatchInputException with message "Cannot deserialize value of type int from String "x": not a valid int value", but the result of getCurrentToken is different.

| Content | getCurrentToken() |
|---------|---------------|
| `{"type":"Payload","count":"x"}` | VALUE_STRING |
| `{"count":"x"}` | END_OBJECT |
| `{"count":"x", "type":"Payload"}` | null |

### Expected behavior

Ideally I would expect that the second case, `{"count": "x"}, would report VALUE_STRING. From #2386 I glance that this is a hard problem.

Unlike #2386 there may be a backup solution here, returning `null` from `getCurrentToken` has the meaning 'unknown', so that would be a suitable answer.

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading and running the attached DefaultImplCurrentTokenTest.java to reproduce the three getCurrentToken() results with @JsonTypeInfo and defaultImpl. Trace the deserialization path involved in the mismatch, then add regression coverage showing that the missing-type case reports the mismatched VALUE_STRING or the documented null fallback.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.