Skip to content

Commit dcfb0cc

Browse files
committed
Fix #1767
1 parent a8e7cdf commit dcfb0cc

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Project: jackson-databind
55

66
2.8.11 (not yet released)
77

8+
#1767: Allow `DeserializationProblemHandler` to respond to primitive types
9+
(reported by nhtzr@github)
810
#1768: Improve `TypeFactory.constructFromCanonical()` to work with
911
`java.lang.reflect.Type.getTypeName()` format
1012

src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ public Object handleWeirdStringValue(Class<?> targetClass, String value,
914914
Object instance = h.value().handleWeirdStringValue(this, targetClass, value, msg);
915915
if (instance != DeserializationProblemHandler.NOT_HANDLED) {
916916
// Sanity check for broken handlers, otherwise nasty to debug:
917-
if ((instance == null) || targetClass.isInstance(instance)) {
917+
if (_isCompatible(targetClass, instance)) {
918918
return instance;
919919
}
920920
throw weirdStringException(value, targetClass, String.format(
@@ -959,7 +959,7 @@ public Object handleWeirdNumberValue(Class<?> targetClass, Number value,
959959
Object key = h.value().handleWeirdNumberValue(this, targetClass, value, msg);
960960
if (key != DeserializationProblemHandler.NOT_HANDLED) {
961961
// Sanity check for broken handlers, otherwise nasty to debug:
962-
if ((key == null) || targetClass.isInstance(key)) {
962+
if (_isCompatible(targetClass, key)) {
963963
return key;
964964
}
965965
throw weirdNumberException(value, targetClass, String.format(
@@ -1000,7 +1000,7 @@ public Object handleMissingInstantiator(Class<?> instClass, JsonParser p,
10001000
instClass, p, msg);
10011001
if (instance != DeserializationProblemHandler.NOT_HANDLED) {
10021002
// Sanity check for broken handlers, otherwise nasty to debug:
1003-
if ((instance == null) || instClass.isInstance(instance)) {
1003+
if (_isCompatible(instClass, instance)) {
10041004
return instance;
10051005
}
10061006
throw instantiationException(instClass, String.format(
@@ -1039,7 +1039,7 @@ public Object handleInstantiationProblem(Class<?> instClass, Object argument,
10391039
Object instance = h.value().handleInstantiationProblem(this, instClass, argument, t);
10401040
if (instance != DeserializationProblemHandler.NOT_HANDLED) {
10411041
// Sanity check for broken handlers, otherwise nasty to debug:
1042-
if ((instance == null) || instClass.isInstance(instance)) {
1042+
if (_isCompatible(instClass, instance)) {
10431043
return instance;
10441044
}
10451045
throw instantiationException(instClass, String.format(
@@ -1102,7 +1102,7 @@ public Object handleUnexpectedToken(Class<?> instClass, JsonToken t,
11021102
Object instance = h.value().handleUnexpectedToken(this,
11031103
instClass, t, p, msg);
11041104
if (instance != DeserializationProblemHandler.NOT_HANDLED) {
1105-
if ((instance == null) || instClass.isInstance(instance)) {
1105+
if (_isCompatible(instClass, instance)) {
11061106
return instance;
11071107
}
11081108
reportMappingException("DeserializationProblemHandler.handleUnexpectedToken() for type %s returned value of type %s",
@@ -1170,6 +1170,19 @@ public JavaType handleUnknownTypeId(JavaType baseType, String id,
11701170
throw unknownTypeIdException(baseType, id, extraDesc);
11711171
}
11721172

1173+
/**
1174+
* @since 2.9.2
1175+
*/
1176+
protected boolean _isCompatible(Class<?> target, Object value)
1177+
{
1178+
if ((value == null) || target.isInstance(value)) {
1179+
return true;
1180+
}
1181+
// [databind#1767]: Make sure to allow wrappers for primitive fields
1182+
return target.isPrimitive()
1183+
&& ClassUtil.wrapperType(target).isInstance(value);
1184+
}
1185+
11731186
/*
11741187
/**********************************************************
11751188
/* Methods for problem reporting, in cases where recovery
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fasterxml.jackson.databind.filter;
2+
3+
import com.fasterxml.jackson.databind.*;
4+
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
5+
6+
public class ProblemHandler1767Test extends BaseMapTest
7+
{
8+
static class IntHandler
9+
extends DeserializationProblemHandler
10+
{
11+
@Override
12+
public Object handleWeirdStringValue(DeserializationContext ctxt,
13+
Class<?> targetType,
14+
String valueToConvert,
15+
String failureMsg)
16+
{
17+
if (targetType != Integer.TYPE) {
18+
return NOT_HANDLED;
19+
}
20+
return 1;
21+
}
22+
}
23+
24+
static class TestBean {
25+
int a;
26+
27+
public int getA() {
28+
return a;
29+
}
30+
31+
public void setA(int a) {
32+
this.a = a;
33+
}
34+
35+
}
36+
37+
public void testPrimitivePropertyWithHandler() throws Exception {
38+
final ObjectMapper mapper = new ObjectMapper();
39+
mapper.addHandler(new IntHandler());
40+
TestBean result = mapper.readValue(aposToQuotes("{'a': 'not-a-number'}"), TestBean.class);
41+
assertNotNull(result);
42+
assertEquals(1, result.a);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)