Skip to content

Exception thrown for incorrectly annotated record changed from InvalidDefinitionException to MismatchedInputException #3988

Closed
@soc

Description

@soc

Describe the bug

The exception thrown for an incorrectly annotated record has changed.

Version information
2.15.2

To Reproduce

Consider these two examples:

public record RecordWithSingleValueValue(@JsonValue String value) {}

public record RecordWithSingleValueCreatorDelegating(String value) {

  @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
  public RecordWithSingleValueCreatorDelegating {}

}

public class JacksonSerializationTest {

  private static final ObjectMapper mapper = new ObjectMapper();

  @Test
  public void recordWithSingleValueValue() throws JsonProcessingException {
    var instance = new RecordWithSingleValueValue("foo");
    var result = mapper.writeValueAsString(instance);
    assertEquals("\"foo\"", result);
    // missing setter
    assertThrows(InvalidDefinitionException.class,
        () -> mapper.readValue(result, RecordWithSingleValue.class));
  }

  @Test
  public void recordWithSingleValueCreatorDelegating() throws JsonProcessingException {
    var instance = new RecordWithSingleValueCreatorDelegating("foo");
    var result = mapper.writeValueAsString(instance);
    assertEquals("{\"value\":\"foo\"}", result);
    // missing setter
    assertThrows(InvalidDefinitionException.class,
        () -> mapper.readValue(result, RecordWithSingleValueCreatorDelegating.class));
  }
}

Expected behavior
In 2.14.2 InvalidDefinitionException was thrown as shown above, with 2.15.3 this changed to MismatchedInputException.
From my understanding InvalidDefinitionException was perhaps more "correct"?

Additional context
I tried to figure out whether this change was intentional or not, but couldn't find anything in the changelog or issues.
I just wanted to let you know of this change – apologies if this was intentional and I missed it.

(I also found some other behavioral changes in 2.15, but I assume they are intentional – things that weren't deserializing before are now deserializing.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    to-evaluateIssue that has been received but not yet evaluated

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions