Skip to content

Commit

Permalink
Fix #1767
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 19, 2017
1 parent a8e7cdf commit dcfb0cc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Project: jackson-databind

2.8.11 (not yet released)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ public Object handleWeirdStringValue(Class<?> targetClass, String value,
Object instance = h.value().handleWeirdStringValue(this, targetClass, value, msg);
if (instance != DeserializationProblemHandler.NOT_HANDLED) {
// Sanity check for broken handlers, otherwise nasty to debug:
if ((instance == null) || targetClass.isInstance(instance)) {
if (_isCompatible(targetClass, instance)) {
return instance;
}
throw weirdStringException(value, targetClass, String.format(
Expand Down Expand Up @@ -959,7 +959,7 @@ public Object handleWeirdNumberValue(Class<?> targetClass, Number value,
Object key = h.value().handleWeirdNumberValue(this, targetClass, value, msg);
if (key != DeserializationProblemHandler.NOT_HANDLED) {
// Sanity check for broken handlers, otherwise nasty to debug:
if ((key == null) || targetClass.isInstance(key)) {
if (_isCompatible(targetClass, key)) {
return key;
}
throw weirdNumberException(value, targetClass, String.format(
Expand Down Expand Up @@ -1000,7 +1000,7 @@ public Object handleMissingInstantiator(Class<?> instClass, JsonParser p,
instClass, p, msg);
if (instance != DeserializationProblemHandler.NOT_HANDLED) {
// Sanity check for broken handlers, otherwise nasty to debug:
if ((instance == null) || instClass.isInstance(instance)) {
if (_isCompatible(instClass, instance)) {
return instance;
}
throw instantiationException(instClass, String.format(
Expand Down Expand Up @@ -1039,7 +1039,7 @@ public Object handleInstantiationProblem(Class<?> instClass, Object argument,
Object instance = h.value().handleInstantiationProblem(this, instClass, argument, t);
if (instance != DeserializationProblemHandler.NOT_HANDLED) {
// Sanity check for broken handlers, otherwise nasty to debug:
if ((instance == null) || instClass.isInstance(instance)) {
if (_isCompatible(instClass, instance)) {
return instance;
}
throw instantiationException(instClass, String.format(
Expand Down Expand Up @@ -1102,7 +1102,7 @@ public Object handleUnexpectedToken(Class<?> instClass, JsonToken t,
Object instance = h.value().handleUnexpectedToken(this,
instClass, t, p, msg);
if (instance != DeserializationProblemHandler.NOT_HANDLED) {
if ((instance == null) || instClass.isInstance(instance)) {
if (_isCompatible(instClass, instance)) {
return instance;
}
reportMappingException("DeserializationProblemHandler.handleUnexpectedToken() for type %s returned value of type %s",
Expand Down Expand Up @@ -1170,6 +1170,19 @@ public JavaType handleUnknownTypeId(JavaType baseType, String id,
throw unknownTypeIdException(baseType, id, extraDesc);
}

/**
* @since 2.9.2
*/
protected boolean _isCompatible(Class<?> target, Object value)
{
if ((value == null) || target.isInstance(value)) {
return true;
}
// [databind#1767]: Make sure to allow wrappers for primitive fields
return target.isPrimitive()
&& ClassUtil.wrapperType(target).isInstance(value);
}

/*
/**********************************************************
/* Methods for problem reporting, in cases where recovery
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.fasterxml.jackson.databind.filter;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;

public class ProblemHandler1767Test extends BaseMapTest
{
static class IntHandler
extends DeserializationProblemHandler
{
@Override
public Object handleWeirdStringValue(DeserializationContext ctxt,
Class<?> targetType,
String valueToConvert,
String failureMsg)
{
if (targetType != Integer.TYPE) {
return NOT_HANDLED;
}
return 1;
}
}

static class TestBean {
int a;

public int getA() {
return a;
}

public void setA(int a) {
this.a = a;
}

}

public void testPrimitivePropertyWithHandler() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
mapper.addHandler(new IntHandler());
TestBean result = mapper.readValue(aposToQuotes("{'a': 'not-a-number'}"), TestBean.class);
assertNotNull(result);
assertEquals(1, result.a);
}

}

0 comments on commit dcfb0cc

Please sign in to comment.