Skip to content

Commit

Permalink
about to test float->int coercion settings
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 13, 2020
1 parent e6e73d6 commit 1f4c2cd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,8 @@ public byte[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
// whether we should allow truncating conversions?
byte value;
if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) {
// should we catch overflow exceptions?
value = p.getByteValue();
if (t == JsonToken.VALUE_NUMBER_INT) {
value = p.getByteValue(); // note: may throw due to overflow
} else {
// should probably accept nulls as 0
if (t == JsonToken.VALUE_NULL) {
Expand Down Expand Up @@ -529,9 +528,8 @@ protected byte[] handleSingleElementUnwrapped(JsonParser p,
{
byte value;
JsonToken t = p.currentToken();
if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) {
// should we catch overflow exceptions?
value = p.getByteValue();
if (t == JsonToken.VALUE_NUMBER_INT) {
value = p.getByteValue(); // note: may throw due to overflow
} else {
// should probably accept nulls as 'false'
if (t == JsonToken.VALUE_NULL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,41 @@
import java.math.BigInteger;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.type.LogicalType;

public class CoerceFloatToIntTest extends BaseMapTest
{
private final ObjectMapper DEFAULT_MAPPER = sharedMapper();
private final ObjectReader READER_LEGACY_FAIL = DEFAULT_MAPPER.reader()
.without(DeserializationFeature.ACCEPT_FLOAT_AS_INT);

private final ObjectMapper MAPPER_TO_EMPTY; {
MAPPER_TO_EMPTY = newJsonMapper();
MAPPER_TO_EMPTY.coercionConfigFor(LogicalType.Integer)
.setCoercion(CoercionInputShape.Float, CoercionAction.AsEmpty);
}

private final ObjectMapper MAPPER_TRY_CONVERT; {
MAPPER_TRY_CONVERT = newJsonMapper();
MAPPER_TRY_CONVERT.coercionConfigFor(LogicalType.Integer)
.setCoercion(CoercionInputShape.Float, CoercionAction.TryConvert);
}

private final ObjectMapper MAPPER_TO_NULL; {
MAPPER_TO_NULL = newJsonMapper();
MAPPER_TO_NULL.coercionConfigFor(LogicalType.Integer)
.setCoercion(CoercionInputShape.Float, CoercionAction.AsNull);
}

private final ObjectMapper MAPPER_TO_FAIL; {
MAPPER_TO_FAIL = newJsonMapper();
MAPPER_TO_FAIL.coercionConfigFor(LogicalType.Integer)
.setCoercion(CoercionInputShape.Float, CoercionAction.Fail);
}

/*
/********************************************************
/* Test methods, defaults (legacy)
Expand Down Expand Up @@ -57,9 +84,13 @@ public void testLegacyFailDoubleToInt() throws Exception
_verifyCoerceFail(READER_LEGACY_FAIL, LongWrapper.class, "{\"l\": 7.7 }");
_verifyCoerceFail(READER_LEGACY_FAIL, long[].class, "[ -1.35 ]");

_verifyCoerceFailShort(READER_LEGACY_FAIL, Short.class, "0.5");
_verifyCoerceFailShort(READER_LEGACY_FAIL, Short.TYPE, "-2.5");
_verifyCoerceFailShort(READER_LEGACY_FAIL, short[].class, "[ -1.35 ]");
_verifyCoerceFail(READER_LEGACY_FAIL, Short.class, "0.5");
_verifyCoerceFail(READER_LEGACY_FAIL, Short.TYPE, "-2.5");
_verifyCoerceFail(READER_LEGACY_FAIL, short[].class, "[ -1.35 ]");

_verifyCoerceFail(READER_LEGACY_FAIL, Byte.class, "0.5");
_verifyCoerceFail(READER_LEGACY_FAIL, Byte.TYPE, "-2.5");
_verifyCoerceFail(READER_LEGACY_FAIL, byte[].class, "[ -1.35 ]");

_verifyCoerceFail(READER_LEGACY_FAIL, BigInteger.class, "25236.256");
}
Expand All @@ -71,33 +102,30 @@ private void _verifyCoerceFail(ObjectReader r, Class<?> targetType,
r.forType(targetType).readValue(doc);
fail("Should not pass");
} catch (MismatchedInputException e) {
verifyException(e, "Cannot coerce a floating-point",
"Cannot coerce Floating-point");
verifyException(e, "Cannot coerce Floating-point");
}
}

private void _verifyCoerceFailShort(ObjectReader r, Class<?> targetType,
String doc) throws Exception
{
try {
r.forType(targetType).readValue(doc);
fail("Should not pass");
} catch (MismatchedInputException e) {
verifyException(e,
"Cannot deserialize value of type `short` from Floating-point value",
"Cannot coerce Floating-point");
}
}

public void testDoubleToLong() throws Exception
{

}

/*
/********************************************************
/* Test methods, CoerceConfig
/* Test methods, CoerceConfig, to empty/null
/********************************************************
*/

/*
/********************************************************
/* Test methods, CoerceConfig, coerce
/********************************************************
*/

/*
/********************************************************
/* Test methods, CoerceConfig, fail
/********************************************************
*/
}

0 comments on commit 1f4c2cd

Please sign in to comment.