Skip to content

Commit a7572d3

Browse files
committed
Backport #3450 fix in 2.13(.3)
1 parent af4962a commit a7572d3

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Project: jackson-databind
1111
(contributed by Gary M)
1212
#3446: `java.lang.StringBuffer` cannot be deserialized
1313
(reported by Lolf1010@github)
14+
#3450: DeserializationProblemHandler is not working with wrapper type
15+
when returning null
16+
(reported by LJeanneau@github)
1417

1518
2.13.2.2 (28-Mar-2022)
1619

src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ protected final int _parseIntPrimitive(DeserializationContext ctxt, String text)
754754
{
755755
try {
756756
if (text.length() > 9) {
757-
long l = Long.parseLong(text);
757+
long l = NumberInput.parseLong(text);
758758
if (_intOverflow(l)) {
759759
Number v = (Number) ctxt.handleWeirdStringValue(Integer.TYPE, text,
760760
"Overflow: numeric value (%s) out of range of int (%d -%d)",
@@ -817,7 +817,29 @@ protected final Integer _parseInteger(JsonParser p, DeserializationContext ctxt,
817817
if (_checkTextualNull(ctxt, text)) {
818818
return (Integer) getNullValue(ctxt);
819819
}
820-
return _parseIntPrimitive(ctxt, text);
820+
return _parseInteger(ctxt, text);
821+
}
822+
823+
/**
824+
* @since 2.14
825+
*/
826+
protected final Integer _parseInteger(DeserializationContext ctxt, String text) throws IOException
827+
{
828+
try {
829+
if (text.length() > 9) {
830+
long l = NumberInput.parseLong(text);
831+
if (_intOverflow(l)) {
832+
return (Integer) ctxt.handleWeirdStringValue(Integer.class, text,
833+
"Overflow: numeric value (%s) out of range of `java.lang.Integer` (%d -%d)",
834+
text, Integer.MIN_VALUE, Integer.MAX_VALUE);
835+
}
836+
return Integer.valueOf((int) l);
837+
}
838+
return NumberInput.parseInt(text);
839+
} catch (IllegalArgumentException iae) {
840+
return(Integer) ctxt.handleWeirdStringValue(Integer.class, text,
841+
"not a valid `java.lang.Integer` value");
842+
}
821843
}
822844

823845
protected final long _parseLongPrimitive(JsonParser p, DeserializationContext ctxt)
@@ -938,7 +960,19 @@ protected final Long _parseLong(JsonParser p, DeserializationContext ctxt,
938960
return (Long) getNullValue(ctxt);
939961
}
940962
// let's allow Strings to be converted too
941-
return _parseLongPrimitive(ctxt, text);
963+
return _parseLong(ctxt, text);
964+
}
965+
966+
/**
967+
* @since 2.14
968+
*/
969+
protected final Long _parseLong(DeserializationContext ctxt, String text) throws IOException
970+
{
971+
try {
972+
return NumberInput.parseLong(text);
973+
} catch (IllegalArgumentException iae) { }
974+
return (Long) ctxt.handleWeirdStringValue(Long.class, text,
975+
"not a valid `java.lang.Long` value");
942976
}
943977

944978
protected final float _parseFloatPrimitive(JsonParser p, DeserializationContext ctxt)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fasterxml.jackson.databind.deser.filter;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.databind.*;
6+
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
7+
import com.fasterxml.jackson.databind.json.JsonMapper;
8+
9+
public class ProblemHandler3450Test extends BaseMapTest
10+
{
11+
// [databind#3450]
12+
13+
static class LenientDeserializationProblemHandler extends DeserializationProblemHandler {
14+
15+
@Override
16+
public Object handleWeirdStringValue(DeserializationContext ctxt, Class<?> targetType, String valueToConvert,
17+
String failureMsg) throws IOException {
18+
19+
// I just want to ignore badly formatted value
20+
return null;
21+
}
22+
}
23+
24+
static class TestPojo3450Int {
25+
public Integer myInteger;
26+
}
27+
28+
static class TestPojo3450Long {
29+
public Long myLong;
30+
}
31+
32+
private final ObjectMapper LENIENT_MAPPER =
33+
JsonMapper.builder().addHandler(new LenientDeserializationProblemHandler()).build();
34+
35+
public void testIntegerCoercion3450() throws Exception
36+
{
37+
TestPojo3450Int pojo;
38+
39+
// First expected coercion into `null` from empty String
40+
pojo = LENIENT_MAPPER.readValue("{\"myInteger\" : \"\"}", TestPojo3450Int.class);
41+
assertNull(pojo.myInteger);
42+
43+
// and then coercion into `null` by our problem handler
44+
pojo = LENIENT_MAPPER.readValue("{\"myInteger\" : \"notInt\"}", TestPojo3450Int.class);
45+
assertNull(pojo.myInteger);
46+
}
47+
48+
public void testLongCoercion3450() throws Exception
49+
{
50+
TestPojo3450Long pojo;
51+
52+
// First expected coercion into `null` from empty String
53+
pojo = LENIENT_MAPPER.readValue("{\"myLong\" : \"\"}", TestPojo3450Long.class);
54+
assertNull(pojo.myLong);
55+
56+
// and then coercion into `null` by our problem handler
57+
pojo = LENIENT_MAPPER.readValue("{\"myLong\" : \"notSoLong\"}", TestPojo3450Long.class);
58+
assertNull(pojo.myLong);
59+
}
60+
}

0 commit comments

Comments
 (0)