Skip to content

DeserializationProblemHandler is not working with wrapper type when returning null #3450

@LJeanneau

Description

@LJeanneau

Describe the bug
Overriding handleWeirdStringValue method of DeserializationProblemHandler and returning null as a default value for a wrapper type (Integer in my case) is not working and is changed to 0 during deserialization
It seems to me that this is due to _parseInteger method using _parseIntPrimitive in StdDeserializer and I can't figure out a way to workaround this behavior. Trying to use an optional leads to Optional.of(0) being returned.

Version information
Bug has been observed in 2.13.2.2 ; was previously working with 2.10.2

To Reproduce

public class LenientDeserializationProblemHandler extends DeserializationProblemHandler {

    @Override
    public Object handleWeirdStringValue(DeserializationContext ctxt, Class<?> targetType, String valueToConvert,
        String failureMsg) throws IOException {

        // I just want to ignore badly formatted value
        return null;
    }
}

public class TestPojo {

    private Integer myInteger;

    public Integer getMyInteger() {
        return myInteger;
    }

    public void setMyInteger(Integer value) {
        myInteger = value;
    }
}

public class DeserializationProblemHandlerTest {

    private final ObjectMapper mapper =
        JsonMapper.builder().addHandler(new LenientDeserializationProblemHandler()).build();

    @Test
    public void present_value_is_working() throws JsonProcessingException {
        TestPojo pojo = mapper.readValue("{\"myInteger\" : \"12\"}", TestPojo.class);

        assertEquals(12, pojo.getMyInteger());
    }

    @Test
    public void non_present_value_is_working() throws JsonProcessingException {
        TestPojo pojo = mapper.readValue("{\"myInteger\" : \"\"}", TestPojo.class);

        assertNull(pojo.getMyInteger());
    }

    @Test
    public void badly_formatted_value_is_not_working() throws JsonProcessingException {
        TestPojo pojo = mapper.readValue("{\"myInteger\" : \"obviously not an integer\"}", TestPojo.class);

        // This test is failing with jackson-databind 2.13.2.2 but worked on 2.10.2
        assertNull(pojo.getMyInteger());
    }
}

Expected behavior
Expected behavior is deserializing null instead of 0 as requested by DeserializationProblemHandler as null is a valid value for Integer

Metadata

Metadata

Assignees

No one assigned

    Labels

    has-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions