diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 2f1b1dc4f9..e682236612 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -9,6 +9,8 @@ Project: jackson-databind #2962: Auto-detection of constructor-based creator method skipped if there is an annotated factory-based creator method (regression from 2.11) (reported by Halil I-S) +#2973: DeserializationProblemHandler is not invoked when trying to deserializing String + (reported by zigzago@github) 2.12.0 (29-Nov-2020) diff --git a/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java b/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java index 9b8ef77136..5229520710 100644 --- a/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java +++ b/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java @@ -869,9 +869,7 @@ public String extractScalarFromObject(JsonParser p, JsonDeserializer deser, Class scalarType) throws IOException { - return reportInputMismatch(scalarType, String.format( -"Cannot deserialize value of type %s from %s (token `JsonToken.START_OBJECT`)", -ClassUtil.getClassDescription(scalarType), _shapeForToken(JsonToken.START_OBJECT))); + return (String) handleUnexpectedToken(scalarType, p); } /* diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandler1767Test.java b/src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandler1767Test.java index ffbd7b4aaf..a57ffdd0e2 100644 --- a/src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandler1767Test.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandler1767Test.java @@ -41,5 +41,4 @@ public void testPrimitivePropertyWithHandler() throws Exception { assertNotNull(result); assertEquals(1, result.a); } - } diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandler2973Test.java b/src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandler2973Test.java new file mode 100644 index 0000000000..6c9c1abf54 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandler2973Test.java @@ -0,0 +1,56 @@ +package com.fasterxml.jackson.databind.deser.filter; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; + +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; +import com.fasterxml.jackson.databind.exc.MismatchedInputException; + +public class ProblemHandler2973Test extends BaseMapTest +{ + // [databind#2973] + static class WeirdTokenHandler + extends DeserializationProblemHandler + { + @Override + public Object handleUnexpectedToken(DeserializationContext ctxt, + JavaType targetType, JsonToken t, JsonParser p, + String failureMsg) + throws IOException + { + String result = p.currentToken().toString(); + p.skipChildren(); + return result; + } + } + + /* + /********************************************************** + /* Test methods + /********************************************************** + */ + + // [databind#2973] + public void testUnexpectedToken2973() throws Exception + { + // First: without handler, should get certain failure + ObjectMapper mapper = sharedMapper(); + try { + mapper.readValue("{ }", String.class); + fail("Should not pass"); + } catch (MismatchedInputException e) { + verifyException(e, "Cannot deserialize value of type `java.lang.String` from Object value"); + } + + // But DeserializationProblemHandler should resolve: + mapper = jsonMapperBuilder() + .addHandler(new WeirdTokenHandler()) + .build(); + ; + String str = mapper.readValue("{ }", String.class); + assertEquals("START_OBJECT", str); + } +}