question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throw `InvalidFormatException` instead of `MismatchedInputException` for ACCEPT_FLOAT_AS_INT coercion failures

See original GitHub issue

I noticed that when ACCEPT_FLOAT_AS_INT is disabled, the application throws MismatchedInputException. It seems InvalidFormatException would be a good fit here, with the benefit of programmatically exposing the value that failed conversion.

I noticed this since I’ve added some custom handling in my application for InvalidFormatException/MismatchedInputException to provide terser/more user-appropriate errors. Since this scenario throws MismatchedInputException, I cannot programmatically retrieve the JSON value that failed to serialize. If it did implement InvalidFormatException, I could use InvalidFormatException.getValue() to get the value.

Note: This same reasoning applies to other similar features, such as ALLOW_COERCION_OF_SCALARS & FAIL_ON_NULL_FOR_PRIMITIVES.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mjustincommented, Oct 9, 2020

Here’s a simple test for the current behavior:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import org.junit.jupiter.api.Test;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class DisallowFloatAsIntTest {
    private ObjectMapper objectMapper = new ObjectMapper().disable(DeserializationFeature.ACCEPT_FLOAT_AS_INT);

    @Test
    public void exceptionIncludesProblematicValue() {
        testExceptionIncludesProblematicValue("5.5", Integer.class);
        testExceptionIncludesProblematicValue("5.0", Long.class);
        testExceptionIncludesProblematicValue("1234567890123456789.0", BigInteger.class);
        testExceptionIncludesProblematicValue("[4, 5.5, 6]", "5.5", new TypeReference<List<Integer>>() {});
        testExceptionIncludesProblematicValue("{\"key1\": 4, \"key2\": 5.5}", "5.5", new TypeReference<Map<String, Integer>>() {});
    }

    private void testExceptionIncludesProblematicValue(String value, Class<?> valueType) {
        JsonProcessingException e = assertThrows(MismatchedInputException.class,
                () -> objectMapper.readValue(value, valueType));
        assertTrue(e.getMessage().contains("('" + value + "')"));
    }

    private void testExceptionIncludesProblematicValue(String content, String value, TypeReference<?> valueType) {
        JsonProcessingException e = assertThrows(MismatchedInputException.class,
                () -> objectMapper.readValue(content, valueType));
        assertTrue(e.getMessage().contains("('" + value + "')"));
    }
}
1reaction
Blacklandscommented, Jul 29, 2020

It seems the same is happening when using the FAIL_ON_NULL_CREATOR_PROPERTIES and FAIL_ON_MISSING_CREATOR_PROPERTIES features for deserialization. These also cause just a MismatchedInputException to be thrown. The information (the name and index of the attribute that was null/missing) is in the exception message, so it should be possible to throw a more specific exception that also contains these as fields, right?

For now, I suppose I will have to parse the exception message to obtain this information (which is very hacky and prone to break, I know!).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson 2.12 Most Wanted (4/5):. CoercionConfig system
After going over couple of rather old feature requests, this one ... setCoercion(CoercionInputShape.Integer, CoercionAction.Fail);.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found