Skip to content

Commit

Permalink
Fix #3450
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 20, 2022
1 parent 7b6d91d commit 92718cf
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Project: jackson-databind
JSON `null` values on reading
#3445: Do not strip generic type from `Class<C>` when resolving `JavaType`
(contributed by Jan J)
#3450: DeserializationProblemHandler is not working with wrapper type
when returning null
(reported by LJeanneau@github)

2.13.3 (not yet released)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ protected final int _parseIntPrimitive(DeserializationContext ctxt, String text)
{
try {
if (text.length() > 9) {
// NOTE! Can NOT call "NumberInput.parseLong()" due to lack of validation there
long l = Long.parseLong(text);
if (_intOverflow(l)) {
Number v = (Number) ctxt.handleWeirdStringValue(Integer.TYPE, text,
Expand Down Expand Up @@ -817,7 +818,29 @@ protected final Integer _parseInteger(JsonParser p, DeserializationContext ctxt,
if (_checkTextualNull(ctxt, text)) {
return (Integer) getNullValue(ctxt);
}
return _parseIntPrimitive(ctxt, text);
return _parseInteger(ctxt, text);
}

/**
* @since 2.14
*/
protected final Integer _parseInteger(DeserializationContext ctxt, String text) throws IOException
{
try {
if (text.length() > 9) {
long l = NumberInput.parseLong(text);
if (_intOverflow(l)) {
return (Integer) ctxt.handleWeirdStringValue(Integer.class, text,
"Overflow: numeric value (%s) out of range of `java.lang.Integer` (%d -%d)",
text, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
return Integer.valueOf((int) l);
}
return NumberInput.parseInt(text);
} catch (IllegalArgumentException iae) {
return(Integer) ctxt.handleWeirdStringValue(Integer.class, text,
"not a valid `java.lang.Integer` value");
}
}

protected final long _parseLongPrimitive(JsonParser p, DeserializationContext ctxt)
Expand Down Expand Up @@ -938,7 +961,19 @@ protected final Long _parseLong(JsonParser p, DeserializationContext ctxt,
return (Long) getNullValue(ctxt);
}
// let's allow Strings to be converted too
return _parseLongPrimitive(ctxt, text);
return _parseLong(ctxt, text);
}

/**
* @since 2.14
*/
protected final Long _parseLong(DeserializationContext ctxt, String text) throws IOException
{
try {
return NumberInput.parseLong(text);
} catch (IllegalArgumentException iae) { }
return (Long) ctxt.handleWeirdStringValue(Long.class, text,
"not a valid `java.lang.Long` value");
}

protected final float _parseFloatPrimitive(JsonParser p, DeserializationContext ctxt)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.fasterxml.jackson.databind.deser.filter;

import java.io.IOException;

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

public class ProblemHandler3450Test extends BaseMapTest
{
// [databind#3450]

static class LenientDeserializationProblemHandler extends DeserializationProblemHandler {

@Override
public Object handleWeirdStringValue(DeserializationContext ctxt, Class<?> targetType, String valueToConvert,
String failureMsg) throws IOException {

// I just want to ignore badly formatted value
return null;
}
}

static class TestPojo3450Int {
public Integer myInteger;
}

static class TestPojo3450Long {
public Long myLong;
}

private final ObjectMapper LENIENT_MAPPER =
JsonMapper.builder().addHandler(new LenientDeserializationProblemHandler()).build();

public void testIntegerCoercion3450() throws Exception
{
TestPojo3450Int pojo;

// First expected coercion into `null` from empty String
pojo = LENIENT_MAPPER.readValue("{\"myInteger\" : \"\"}", TestPojo3450Int.class);
assertNull(pojo.myInteger);

// and then coercion into `null` by our problem handler
pojo = LENIENT_MAPPER.readValue("{\"myInteger\" : \"notInt\"}", TestPojo3450Int.class);
assertNull(pojo.myInteger);
}

public void testLongCoercion3450() throws Exception
{
TestPojo3450Long pojo;

// First expected coercion into `null` from empty String
pojo = LENIENT_MAPPER.readValue("{\"myLong\" : \"\"}", TestPojo3450Long.class);
assertNull(pojo.myLong);

// and then coercion into `null` by our problem handler
pojo = LENIENT_MAPPER.readValue("{\"myLong\" : \"notSoLong\"}", TestPojo3450Long.class);
assertNull(pojo.myLong);
}
}

0 comments on commit 92718cf

Please sign in to comment.