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

ValueInstantiationException when deserializing using a builder and UNWRAP_SINGLE_VALUE_ARRAYS #2608

Closed
cadrake opened this issue Jan 29, 2020 · 1 comment
Labels
lombok Issue (likely) related to use of Lombok
Milestone

Comments

@cadrake
Copy link

cadrake commented Jan 29, 2020

I am running into an issue with using @JsonDeserialize to deserialize via a builder when one field is really an array with a single value. I create a mapper and set DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS in order to unwrap the single object in the array but running my test gives Cannot construct instance of org.cadrake.POJOValue$POJOValueBuilder, problem: object is not an instance of declaring class. I Stepped through the jackson classes and it looks like when BuilderBasedDeserializer calls deserializeFromArray() and then passes the result to finishBuild(). The object has already been built by the super class and thus when finishBuild() uses reflection to call the build() method on the actual builder, it is failing because the object is of the target type not the builder type. I have included my example pojo's and test as well below, I generate the boiler plate getters/builder via lombok. I would like to help with a fix as well if this is more than a trivial change.

@Getter
@EqualsAndHashCode
@Builder
@JsonDeserialize(builder = ExamplePOJO.ExamplePOJOBuilder.class)
public class ExamplePOJO {
  private final int id;
  private final String name;
  private final POJOValue value;

  @JsonPOJOBuilder(withPrefix = "")
  public static class ExamplePOJOBuilder {
  }
}
@Getter
@EqualsAndHashCode
@Builder
@JsonDeserialize(builder = POJOValue.POJOValueBuilder.class)
public class POJOValue {
  private final String subValue;

  @JsonPOJOBuilder(withPrefix = "")
  public static class POJOValueBuilder {
  }
}
public class ExamplePOJOSerializationTest {

  @Test
  public void testDeserializationAndFail() throws IOException {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);

    final String serialized = "{\"id\": 1, \"name\": \"test\", \"value\": [ {\"subValue\": \"123\"} ]}";
    final ExamplePOJO pojoDeserialized = mapper.readValue(serialized, new TypeReference<ExamplePOJO>() {});
  }
}
@cowtowncoder cowtowncoder added the lombok Issue (likely) related to use of Lombok label Apr 4, 2020
cowtowncoder added a commit that referenced this issue Apr 4, 2020
@cowtowncoder
Copy link
Member

I can reproduce this with the example; thank you reporting the problem.

@cowtowncoder cowtowncoder added this to the 2.11.0 milestone Apr 5, 2020
@cowtowncoder cowtowncoder changed the title Jackson 2.10.2 ValueInstantiationException when deserializing using a builder and UNWRAP_SINGLE_VALUE_ARRAYS ValueInstantiationException when deserializing using a builder and UNWRAP_SINGLE_VALUE_ARRAYS Apr 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lombok Issue (likely) related to use of Lombok
Projects
None yet
Development

No branches or pull requests

2 participants