FasterXML / FasterXML/jackson-databind

`@JsonCreator` for enums converts value to String event if it is not

Open
#6,164 0 comments 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

I have the following JSON `{"dataSetType":5}` where `dataSetType` in POJO is `enum` like in the example below.
Error message:
`Cannot construct instance of xyzl.DataSetType, problem: class java.lang.String cannot be cast to class java.lang.Number`
The problem is value of `dataSetType` is `int` and Jackson is converting it to `String` only to mismath with the `@JsonCreator`.
This was working fine in Jackson 2.
Workaround is to change argument of method annotated with `@JsonCreator` to `String` despite the wrong type.

### Version Information

3.2.1

### Reproduction

```java
@Getter
public enum DataSetType {

ZERO(0), FIVE(5);

private final int value;

DataSetType(final int value) {
this.value = value;
}

@JsonCreator
public static DataSetType getDataSetTypeByValue(final int value) {
for (final DataSetType dateSetType: DataSetType.values()) {
if (dateSetType.value == value) {
return dateSetType;
}
}
return null;
}

}

```

### Expected behavior

_No response_

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the enum case with the shown DataSetType example, JSON input {"dataSetType":5}, Jackson 3.2.1, and the @JsonCreator method taking int. Trace enum creator argument handling to determine why the numeric value becomes a String; done means the numeric value reaches the int creator and resolves to the matching enum without the String workaround.

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.