Skip to content

Commit 9b1b35d

Browse files
committed
Fix #1940
2 parents bc22f90 + c5bfb9a commit 9b1b35d

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,3 +753,8 @@ Deblock Thomas (deblockt@github)
753753
lilei@venusgroup.com.cn:
754754
* Reported #1931: Two more `c3p0` gadgets to exploit default typing issue
755755
(2.9.5)
756+
757+
Aniruddha Maru (maroux@github)
758+
* Reported #1940: `Float` values with integer value beyond `int` lose precision if
759+
bound to `long`
760+
(2.9.5)

release-notes/VERSION-2.x

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Project: jackson-databind
1414
(contributed by Deblock T)
1515
#1931: Two more `c3p0` gadgets to exploit default typing issue
1616
(reported by lilei@venusgroup.com.cn)
17+
#1940: `Float` values with integer value beyond `int` lose precision if
18+
bound to `long`
19+
(reported by Aniruddha M)
1720

1821
2.9.4 (24-Jan-2018)
1922

@@ -208,7 +211,7 @@ Project: jackson-databind
208211
`MapperFeature.ALLOW_COERCION_OF_SCALARS`
209212
(requested by magdel@github)
210213

211-
2.8.11.1 (not yet released)
214+
2.8.11.1 (11-Feb-2018)
212215

213216
#1872: `NullPointerException` in `SubTypeValidator.validateSubType` when
214217
validating Spring interface

src/main/java/com/fasterxml/jackson/databind/util/TokenBuffer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,8 +1637,7 @@ private final boolean _smallerThanLong(Number n) {
16371637
return (n instanceof Integer) || (n instanceof Short) || (n instanceof Byte);
16381638
}
16391639

1640-
/* 02-Jan-2017, tatu: Modified from method(s) in `ParserBase`
1641-
*/
1640+
// 02-Jan-2017, tatu: Modified from method(s) in `ParserBase`
16421641

16431642
protected int _convertNumberToInt(Number n) throws IOException
16441643
{
@@ -1689,7 +1688,7 @@ protected long _convertNumberToLong(Number n) throws IOException
16891688
if (d < MIN_LONG_D || d > MAX_LONG_D) {
16901689
reportOverflowLong();
16911690
}
1692-
return (int) d;
1691+
return (long) d;
16931692
} else if (n instanceof BigDecimal) {
16941693
BigDecimal big = (BigDecimal) n;
16951694
if (BD_MIN_LONG.compareTo(big) > 0

src/test/java/com/fasterxml/jackson/databind/node/TestConversions.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.junit.Assert;
1010

11+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
1112
import com.fasterxml.jackson.core.*;
1213
import com.fasterxml.jackson.core.type.WritableTypeId;
1314
import com.fasterxml.jackson.databind.*;
@@ -70,7 +71,16 @@ static class Issue467TmpBean {
7071

7172
public Issue467TmpBean(int i) { x = i; }
7273
}
73-
74+
75+
static class Issue709Bean {
76+
public byte[] data;
77+
}
78+
79+
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="_class")
80+
static class LongContainer1940 {
81+
public Long longObj;
82+
}
83+
7484
/*
7585
/**********************************************************
7686
/* Unit tests
@@ -173,10 +183,6 @@ public void testBase64Text() throws Exception
173183
}
174184
}
175185

176-
static class Issue709Bean {
177-
public byte[] data;
178-
}
179-
180186
/**
181187
* Simple test to verify that byte[] values can be handled properly when
182188
* converting, as long as there is metadata (from POJO definitions).
@@ -288,5 +294,12 @@ public void testConversionOfTrees() throws Exception
288294
assertTrue("Expected Object, got "+tree.getNodeType(), tree.isBoolean());
289295
assertEquals(EXP, MAPPER.writeValueAsString(tree));
290296
}
291-
}
292297

298+
// [databind#1940]: losing of precision due to coercion
299+
public void testBufferedLongViaCoercion() throws Exception {
300+
long EXP = 1519348261000L;
301+
JsonNode tree = MAPPER.readTree("{\"longObj\": "+EXP+".0, \"_class\": \""+LongContainer1940.class.getName()+"\"}");
302+
LongContainer1940 obj = MAPPER.treeToValue(tree, LongContainer1940.class);
303+
assertEquals(Long.valueOf(EXP), obj.longObj);
304+
}
305+
}

0 commit comments

Comments
 (0)