-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[Jackson-3] Add new DeserializationProblemHandler.handleNullForPrimitives()
#5493
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
base: 3.0
Are you sure you want to change the base?
Changes from 4 commits
0f7137e
a7ea15d
04d6392
af83096
d62274c
a010efa
dfed7ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1354,6 +1354,41 @@ public Object handleWeirdStringValue(Class<?> targetClass, String value, | |
| throw weirdStringException(value, targetClass, msg); | ||
| } | ||
|
|
||
| /** | ||
| * Method that deserializers should call if they encounter a null value and | ||
| * target value type is a Primitive type. | ||
| * | ||
| * Default implementation will try to call {@link DeserializationContext#reportInputMismatch(Class, String, Object...)}, | ||
| * which by default would throw {@link MismatchedInputException} | ||
| * | ||
| * @param targetClass Primitive type into which incoming {@code null} value should be converted to | ||
| * @param deser Type of {@link ValueDeserializer} calling this method | ||
| * @param msg Error message template caller wants to use if exception is to be thrown | ||
| * | ||
| * @throws JacksonException To indicate unrecoverable problem, usually based on <code>msg</code> | ||
| */ | ||
| public Object handleNullForPrimitives(Class<?> targetClass, | ||
| ValueDeserializer<?> deser, String msg) | ||
| throws JacksonException | ||
|
|
||
| { | ||
| // but if not handled, just throw exception | ||
| LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers(); | ||
| while (h != null) { | ||
| // Can bail out if it's handled | ||
| Object instance = h.value().handleNullForPrimitives(this, deser, msg); | ||
| if (instance != DeserializationProblemHandler.NOT_HANDLED) { | ||
| // Sanity check for broken handlers, otherwise nasty to debug: | ||
| if (_isCompatible(targetClass, instance)) { | ||
| return instance; | ||
| } | ||
| return reportInputMismatch(deser, msg); | ||
|
||
| } | ||
| h = h.next(); | ||
| } | ||
| return reportInputMismatch(deser, msg); | ||
| } | ||
|
|
||
| /** | ||
| * Method that deserializers should call if they encounter a numeric value | ||
| * that cannot be converted to target property type, in cases where some | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package tools.jackson.databind.deser.filter; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import tools.jackson.core.JacksonException; | ||
| import tools.jackson.core.JsonParser; | ||
| import tools.jackson.core.JsonToken; | ||
| import tools.jackson.databind.*; | ||
| import tools.jackson.databind.deser.DeserializationProblemHandler; | ||
| import tools.jackson.databind.json.JsonMapper; | ||
| import tools.jackson.databind.testutil.DatabindTestUtil; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| // For [databind#5469] Add callback to signal null for primitive in DeserializationProblemHandler | ||
| public class DeserializationProblemHandler5469Test | ||
| extends DatabindTestUtil | ||
| { | ||
| private static int hitCount = 0; | ||
| static class Person5469 { | ||
| public String id; | ||
| public String name; | ||
| public long age; | ||
| } | ||
|
|
||
| static class ProblemHandler5469 extends DeserializationProblemHandler | ||
| { | ||
| @Override | ||
| public Object handleNullForPrimitives(DeserializationContext ctxt, ValueDeserializer<?> deser, String failureMsg) throws JacksonException { | ||
| hitCount++; | ||
| return 5469L; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testIssue5469() | ||
| throws Exception | ||
| { | ||
| // Given | ||
| assertEquals(0, hitCount); | ||
| ObjectMapper mapper = JsonMapper.builder() | ||
| .enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) | ||
| .addHandler(new ProblemHandler5469()) | ||
| .build(); | ||
|
|
||
| // When | ||
| Person5469 person = mapper.readValue( | ||
| "{\"id\": \"12ab\", \"name\": \"Bob\", " + | ||
| // Input is NULL, but.... | ||
| "\"age\": null}", Person5469.class); | ||
|
|
||
| // Then | ||
| assertNotNull(person); | ||
| assertEquals("12ab", person.id); | ||
| assertEquals("Bob", person.name); | ||
| // We get the MAGIC NUMBER as age | ||
| assertEquals(5469L, person.age); | ||
| // Sanity check, we hit the code path as we wanted | ||
| assertEquals(1, hitCount); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.