"Input mismatch reading Enum" after upgrade to Jackson 2.11

See original GitHub issue

After upgrading Jackson from 2.10.3 to 2.11.0 we started to see “Input mismatch reading Enum” errors when deserializing this Koltin Enum class which has a custom @JsonCreator.

To reproduce, you may want to check out this PR and run the added test via

./gradlew :clearly-defined:test --tests org.ossreviewtoolkit.clearlydefined.ClearlyDefinedServiceTest

with and without the Revert "Gradle: Upgrade Jackson to version 2.11.0" commit.

The full error is:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Input mismatch reading Enum `org.ossreviewtoolkit.clearlydefined.ClearlyDefinedService$Provider`: properties-based `@JsonCreator` ([method org.ossreviewtoolkit.clearlydefined.ClearlyDefinedService$Provider#fromString(1 params)]) expects JSON Object (JsonToken.START_OBJECT), got JsonToken.VALUE_STRING
at [Source: (okhttp3.ResponseBody$BomAwareReader); line: 1, column: 87] (through reference chain: org.ossreviewtoolkit.clearlydefined.ClearlyDefinedService$Curation["described"]->org.ossreviewtoolkit.clearlydefined.ClearlyDefinedService$Described["sourceLocation"]->org.ossreviewtoolkit.clearlydefined.ClearlyDefinedService$SourceLocation["provider"])

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

61reactions
lpandziccommented, May 19, 2020

I’ve encountered this problem with Java too, when using @JsonCreator on factory method of an enum with a String parameter. Switching to @JsonCreator(mode = JsonCreator.Mode.DELEGATING) fixed the issue.

3reactions
bentmanncommented, Oct 20, 2020

If test can be simplified to only use Java

How’s that:

package test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonTest
{
  public enum LinkType
  {
    TEST;

    @JsonCreator(mode = Mode.DEFAULT)
    public static LinkType fromString(@JsonProperty("type") String type) {
      return TEST;
    }
  }

  private final LinkType type;

  @JsonCreator
  public JacksonTest(@JsonProperty("type") LinkType type) {
    this.type = type;
  }

  @Override
  public String toString() {
    return "> " +  type;
  }

  public static void main(String[] args) throws Exception {
    String json = "{\"type\":\"TEST\"}";
    System.out.println(new ObjectMapper().readValue(json, JacksonTest.class));
  }
}

This works as is with Jackson 2.10.x but fails with aforementioned error on Jackson 2.11 unless

  • JsonCreator.Mode.DELEGATING is used or
  • @JsonProperty is dropped from fromString()

Which Jackson version behaves properly?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Failed to convert boolean to enum using Jackson
I have following java class in a Springboot application
Read more >
How to correctly serialize/deserialize an enum with custom ...
I'm going to experiment with a custom deserializer and will post an issue in jackson-databind as you requested. Thanks, David.
Read more >
Jackson 2.11.0无法使用@JsonCreator反序列化枚举值
HttpMessageNotReadableException: JSON parse error: Input mismatch reading Enum `xyz.jieee.demo.DemoEnum`: properties-based `@JsonCreator` ...
Read more >
DeserializationFeature (jackson-databind 2.11.0 API)
Feature that determines whether JSON integer numbers are valid values to be used for deserializing Java enum values. FAIL_ON_READING_DUP_TREE_KEY.
Read more >
Jacksonでenumをシリアライズ - Qiita
DEFAULTで使用すると"Input mismatch reading enum"というエラーメッセージを受け取ってしまいます。内部の型推測のルールが変更されたことが原因のよう ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found