-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Throw InvalidFormatException
instead of MismatchedInputException
for ACCEPT_FLOAT_AS_INT coercion failures
#2804
Comments
Thank you for suggestion. This is an area that could use improvements for sure, and unifying exceptions used would make sense. But I will have to think about this carefully -- there is also another option of |
It seems the same is happening when using the 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!). |
Some notes on this:
I probably won't have much time to look into this in near future, but what would eventually be useful would be simple reproduction to show some use cases: these could serve as verification and regression tests (for now should just catch existing exception, check that message contains name; these would be changed after fix). |
I personally am a big fan of leveraging the type system for error handling - that's something that can be done well via exceptions. For a missing creator property, I would except something like I'm not sure what I could contribute to this issue unfortunately. I can understand that it's not a very high priority thing to look into. Thanks for commenting on it, though! Maybe here's, as an example, my personal use case with this right nowI have a REST API (using Spring Boot Web with Jackson) and I want to give clients meaningful errors when they send "garbage" JSON to me. |
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 + "')"));
}
} |
@mjustin Thank you for the test cases! These help a lot if and when tackling the issue. |
I hope to tackle this still before 2.12.0 since helper methods were added after 2.11 and it may be relatively easy to still change them at this point. |
InvalidFormatException
instead of MismatchedInputException
for ACCEPT_FLOAT_AS_INT coercion failures
I noticed that when
ACCEPT_FLOAT_AS_INT
is disabled, the application throwsMismatchedInputException
. It seemsInvalidFormatException
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 throwsMismatchedInputException
, I cannot programmatically retrieve the JSON value that failed to serialize. If it did implementInvalidFormatException
, I could useInvalidFormatException.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
.The text was updated successfully, but these errors were encountered: