Skip to content

Conversion of MissingNode throws JsonProcessingException #4932

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

Closed
ludgerb opened this issue Jan 27, 2025 · 3 comments
Closed

Conversion of MissingNode throws JsonProcessingException #4932

ludgerb opened this issue Jan 27, 2025 · 3 comments
Labels
2.18 Issues planned at 2.18 or later
Milestone

Comments

@ludgerb
Copy link

ludgerb commented Jan 27, 2025

According to the documentation about MissingNode the following holds:

In most respects this placeholder node will act as NullNode; for example, for purposes of value conversions, value is considered to be null and represented as value zero when used for numeric conversions.

While this holds true for most operation, this fails when converting a json tree containing a MissingNode to a Pojo.

Version Information

2.18.0

Reproduction

  @Test
  void testTreeToValue() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();

    // Success
    var result = assertDoesNotThrow(() -> objectMapper.treeToValue(NullNode.getInstance(), Object.class));
    assertNull(result);

    // Fails
    result = assertDoesNotThrow(() -> objectMapper.treeToValue(MissingNode.getInstance(), Object.class));
    assertNull(result);
  }

Expected behavior

Expected behavior:
converting a MissingNode should behave the same as converting the NullNode

Observed behavior:
converting a NullNode returns a null reference.
converting a MissingNode throws a JsonProcessingException.

@ludgerb ludgerb added the to-evaluate Issue that has been received but not yet evaluated label Jan 27, 2025
cowtowncoder added a commit that referenced this issue Jan 28, 2025
@cowtowncoder
Copy link
Member

Sounds sub-optimal. I'll need to think about expected behavior: probably should be converted to "absent value" (which most often is null). It'd be easier to simply map to null but... I'll see.

@ludgerb
Copy link
Author

ludgerb commented Jan 28, 2025

Hi Cowtowncoder, thank you for considering

I admit my expected behavior is only because of the documentation I cited. It does not necessarily have to be null.
But current behavior of throwing an exception is "sub-optimal".

For comparison:
If you serialize a JsonTree containing a MissingNode to a String or byte stream as an intermediate operation and then de-serialize that stream to a Pojo, you get a null value.

@cowtowncoder
Copy link
Member

cowtowncoder commented Jan 28, 2025

Right, 100% agreed on now throwing exception. I think I'll go with null as the first step; can re-consider wrt getAbsentValue() at a later point.
(note: "absent value" is in almost cases null -- just not for primitives and Optional(-style) values).

@cowtowncoder cowtowncoder changed the title Conversion of MissingNode throws JsonProcessingException Conversion of MissingNode throws JsonProcessingException Jan 29, 2025
@cowtowncoder cowtowncoder added 2.18 Issues planned at 2.18 or later and removed to-evaluate Issue that has been received but not yet evaluated labels Jan 29, 2025
@cowtowncoder cowtowncoder added this to the 2.18.3 milestone Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.18 Issues planned at 2.18 or later
Projects
None yet
Development

No branches or pull requests

2 participants