Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After Upgrading to 2.11, Cannot construct instance of MyEnum, problem: argument type mismatch. READ_UNKNOWN_ENUM_VALUES_AS_NULL= true does not fix it #2819

Closed
DanielHaughton opened this issue Aug 14, 2020 · 4 comments
Labels
need-test-case To work on issue, a reproduction (ideally unit test) needed

Comments

@DanielHaughton
Copy link

DanielHaughton commented Aug 14, 2020

So this issue only seems to occur on 2.11, 2.10 works fine and does not throw an exception.

I have the following model with the enum Status

@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-08-14T13:53:10.363+01:00")

public class TaskTreeResponseVM   {
  @JsonProperty("id")
  private String id = null;


  /**
   * Current status of the idea
   */
  public enum StatusEnum {
    INCOMPLETE("INCOMPLETE"),
    
    COMPLETE("COMPLETE");

    private String value;

    StatusEnum(String value) {
      this.value = value;
    }

    @Override
    @JsonValue
    public String toString() {
      return String.valueOf(value);
    }

    @JsonCreator
    public static StatusEnum fromValue(String text) {
      for (StatusEnum b : StatusEnum.values()) {
        if (String.valueOf(b.value).equals(text)) {
          return b;
        }
      }
      return null;
    }
  }

  @JsonProperty("status")
  private StatusEnum status = null;
  /**
   * Current status of the idea
   * @return status
  **/
  @ApiModelProperty(example = "INCOMPLETE", required = true, value = "Current status of the idea")
  @NotNull

@Size(max=255) 
  public StatusEnum getStatus() {
    return status;
  }

  public void setStatus(StatusEnum status) {
    this.status = status;
  }

Due to an unrelated issue, sometimes an Integer is passed to the Status field, which obviously shouldn't work. However on 2.10, it just ignored it and the field was left as null. Now after upgrading to 2.11 i am having this problem, I see in this linked issue, it seems like that past behaviour was actually a bug #2737

I tried setting READ_UNKNOWN_ENUM_VALUES_AS_NULL=true to allow this to fail gracefully and not throw an exception but it didnt seem to do anything. Am I expecting that config value to do something it doesn't?

Here is my stack trace

Caused by: com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of package.StatusEnum, problem: argument type mismatch
at [Source: (PushbackInputStream); line: 1, column: 11] (through reference chain: package.TaskTreeResponseVM["status"])
at com.fasterxml.jackson.databind.exc.ValueInstantiationException.from(ValueInstantiationException.java:47)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1758)
at com.fasterxml.jackson.databind.DeserializationContext.handleInstantiationProblem(DeserializationContext.java:1128)
at com.fasterxml.jackson.databind.deser.std.FactoryBasedEnumDeserializer.deserialize(FactoryBasedEnumDeserializer.java:159)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:371)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:164)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4482)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3487)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:269)
... 10 more

@cowtowncoder cowtowncoder added the to-evaluate Issue that has been received but not yet evaluated label Aug 19, 2020
@cowtowncoder
Copy link
Member

Hmmh. I was about to say that READ_UNKNOWN_ENUM_VALUES_AS_NULL is only designed to work for JSON Strings, but after brief thinking maybe it should also work for numbers.
The only (?) complication could come from the fact that there are all kinds of possible value types (scalars, structured)... but it does not seem unreasonable to allow all of those to not fail.

I hope to play with this idea in not-too-distant future, but right now I have a fairly long backlog of things to work on (and sometimes not tons of time to work on even then), but I thought I'll add a note to say that this is not a forgotten issue.
Even if response took too long.

Change in behavior should go in 2.12.0 (which is version I am hoping to complete so that the first release candidate was out by end of Sep 2020).

@cowtowncoder cowtowncoder added 2.12 and removed to-evaluate Issue that has been received but not yet evaluated labels Sep 5, 2020
@cowtowncoder
Copy link
Member

Hmmh. Looks like #1642 should handle number case, and I can not actually reproduce this.

So I would need full test case -- my attempt at creating test failed.

@cowtowncoder cowtowncoder added the need-test-case To work on issue, a reproduction (ideally unit test) needed label Oct 2, 2020
@cowtowncoder
Copy link
Member

Cannot reproduce: may re-open with a reproduction.

@dkocher
Copy link

dkocher commented Dec 26, 2020

Setting mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true) did resolve the problem here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-test-case To work on issue, a reproduction (ideally unit test) needed
Projects
None yet
Development

No branches or pull requests

3 participants