jackson-databind: Deserializer fails with the function readerWithView()

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Use function readerWithView() Deserializer a Object fails I created a test project

@ExtendWith({SpringExtension.class})
@SpringBootTest(classes = Main.class)
public class JsonTest {

  @Autowired
  private  ObjectMapper objectMapper;

  static class SimpleStockFreezeStatusMessage {

    private Long businessId;
    private Boolean success;
    private String tenantId;

   // getter and setter  ...
  }

  @Test
  public void testSpringJsonMessage() throws IOException {
    var json = "{\"businessId\":1336504106360835,\"tenantId\":\"first\",\"success\":false}";
    var value = objectMapper.readValue(json, SimpleStockFreezeStatusMessage.class);

    System.out.println("message ser by readValue : " + value.tenantId);

    // success
    assertEquals(value.tenantId, "first");

    Class<?> view = SimpleStockFreezeStatusMessage.class;
    JavaType javaType = objectMapper.constructType(new  TypeReference<SimpleStockFreezeStatusMessage>() {});

    //this is Spring message Usage
    var value2 = (SimpleStockFreezeStatusMessage)objectMapper.readerWithView(view).forType(javaType).readValue(json);

    System.out.println("message ser by readerWithView: " + value2.tenantId);

    // tenantId is null
    assertEquals(value2.tenantId, "first");
  }
}

value2 all property is Null

Version Information

2.17.2 and 2.18.0

Reproduction

Download my test project run test or follow my code

Expected behavior

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 31 (18 by maintainers)

Most upvoted comments

That is interesting. Too bad, too; especially since code seems to be handling “no view” case just fine. Oh well.

I took another look at the Spring code, and it indeed returns a Class as the View when it can’t find @JsonView. Perhaps the Spring team also misunderstood the concept of View. I enjoyed our discussion and appreciate you helping me gain a deeper understanding of Jackson.

I don’t know why Spring Messaging assumes there’s a View defined; I haven’t made any settings, which is also why I misunderstood the concept of “View.”

It seems I should take a closer look at Jackson’s implementation in order to make reasonable suggestions.

DEFAULT_VIEW_INCLUSION = false is the default value in Spring, and here is the reasoning behind it: here

You’re Wellcome

A small suggestion: when @JsonView is not configured and DEFAULT_VIEW_INCLUSION is set to false, readerWithView() should throw an exception. Currently, returning an object with all empty properties is really confusing.

What do you think? @cowtowncoder

Will leave open for now but it seems this is due to misunderstanding of how Views work, not a bug.

You’re right, I misunderstood readerWithView(). This is indeed not a bug

@JsonView is where you wanna start. For references, look up some references around Jackson’s “View” in Baeldung. Or you can search test cases in jackson-databind that uses.

Honestly I can’t tell how exactly to configure ObjectMapper to reproduce the error. There is just too many of them (configs). How about compare all configurations and see difference one by one and figure out?

I made some changes to my test and found that if I run it with the default configuration, there is no error. However, if I use the ObjectMapper from the Spring context, it fails

Ah… I should’ve probably asked about the configuration first. Now you may need to consult Spring community.

Please compare what configurations are different. Probably there is API/configuration endpoint or sorts to customize Jackson config that you can use.

How exactly does it fail? Like …

  • Does it throw exception? If so, could you share stack trace also? Or
  • Is the result unexpected? Then again how exactly does result show? How different from expected behavior?