Skip to content

Commit 7fd0af3

Browse files
committed
Fixed #2973
1 parent b66dfb6 commit 7fd0af3

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project: jackson-databind
99
#2962: Auto-detection of constructor-based creator method skipped if there is
1010
an annotated factory-based creator method (regression from 2.11)
1111
(reported by Halil I-S)
12+
#2973: DeserializationProblemHandler is not invoked when trying to deserializing String
13+
(reported by zigzago@github)
1214

1315
2.12.0 (29-Nov-2020)
1416

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -869,9 +869,7 @@ public String extractScalarFromObject(JsonParser p, JsonDeserializer<?> deser,
869869
Class<?> scalarType)
870870
throws IOException
871871
{
872-
return reportInputMismatch(scalarType, String.format(
873-
"Cannot deserialize value of type %s from %s (token `JsonToken.START_OBJECT`)",
874-
ClassUtil.getClassDescription(scalarType), _shapeForToken(JsonToken.START_OBJECT)));
872+
return (String) handleUnexpectedToken(scalarType, p);
875873
}
876874

877875
/*

src/test/java/com/fasterxml/jackson/databind/deser/filter/ProblemHandler1767Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,4 @@ public void testPrimitivePropertyWithHandler() throws Exception {
4141
assertNotNull(result);
4242
assertEquals(1, result.a);
4343
}
44-
4544
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.fasterxml.jackson.databind.deser.filter;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.core.JsonParser;
6+
import com.fasterxml.jackson.core.JsonToken;
7+
8+
import com.fasterxml.jackson.databind.*;
9+
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
10+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
11+
12+
public class ProblemHandler2973Test extends BaseMapTest
13+
{
14+
// [databind#2973]
15+
static class WeirdTokenHandler
16+
extends DeserializationProblemHandler
17+
{
18+
@Override
19+
public Object handleUnexpectedToken(DeserializationContext ctxt,
20+
JavaType targetType, JsonToken t, JsonParser p,
21+
String failureMsg)
22+
throws IOException
23+
{
24+
String result = p.currentToken().toString();
25+
p.skipChildren();
26+
return result;
27+
}
28+
}
29+
30+
/*
31+
/**********************************************************
32+
/* Test methods
33+
/**********************************************************
34+
*/
35+
36+
// [databind#2973]
37+
public void testUnexpectedToken2973() throws Exception
38+
{
39+
// First: without handler, should get certain failure
40+
ObjectMapper mapper = sharedMapper();
41+
try {
42+
mapper.readValue("{ }", String.class);
43+
fail("Should not pass");
44+
} catch (MismatchedInputException e) {
45+
verifyException(e, "Cannot deserialize value of type `java.lang.String` from Object value");
46+
}
47+
48+
// But DeserializationProblemHandler should resolve:
49+
mapper = jsonMapperBuilder()
50+
.addHandler(new WeirdTokenHandler())
51+
.build();
52+
;
53+
String str = mapper.readValue("{ }", String.class);
54+
assertEquals("START_OBJECT", str);
55+
}
56+
}

0 commit comments

Comments
 (0)