Skip to content

Commit e3f005b

Browse files
committed
Backport #2220 fix
1 parent 21314d4 commit e3f005b

File tree

4 files changed

+15
-29
lines changed

4 files changed

+15
-29
lines changed

release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Project: jackson-databind
2424
#2211: Change of behavior (2.8 -> 2.9) with `ObjectMapper.readTree(input)` with no content
2525
#2217: Suboptimal memory allocation in `TextNode.getBinaryValue()`
2626
(reported by Christoph B)
27+
#2220: Force serialization always for `convertValue()`; avoid short-cuts
2728
#2223: Add `missingNode()` method in `JsonNodeFactory`
2829
#2227: Minor cleanup of exception message for `Enum` binding failure
2930
(reported by RightHandedMonkey@github)

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ public enum DeserializationFeature implements ConfigFeature
3333
* or {@link java.util.Collection} context) is available.
3434
* If enabled such values will be deserialized as {@link java.math.BigDecimal}s;
3535
* if disabled, will be deserialized as {@link Double}s.
36-
* <p>
36+
*<p>
37+
* NOTE: one aspect of {@link java.math.BigDecimal} handling that may need
38+
* configuring is whether trailing zeroes are trimmed:
39+
* {@link com.fasterxml.jackson.databind.node.JsonNodeFactory} has
40+
* {@link com.fasterxml.jackson.databind.node.JsonNodeFactory#withExactBigDecimals} for
41+
* changing default behavior (default is for trailing zeroes to be trimmed).
42+
*<p>
3743
* Feature is disabled by default, meaning that "untyped" floating
3844
* point numbers will by default be deserialized as {@link Double}s
3945
* (choice is for performance reason -- BigDecimals are slower than

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

+4-16
Original file line numberDiff line numberDiff line change
@@ -2688,9 +2688,10 @@ public <T> T treeToValue(TreeNode n, Class<T> valueType)
26882688
throws JsonProcessingException
26892689
{
26902690
try {
2691+
// 25-Jan-2019, tatu: [databind#2220] won't prevent existing coercions here
26912692
// Simple cast when we just want to cast to, say, ObjectNode
2692-
// ... one caveat; while everything is Object.class, let's not take shortcut
2693-
if (valueType != Object.class && valueType.isAssignableFrom(n.getClass())) {
2693+
if (TreeNode.class.isAssignableFrom(valueType)
2694+
&& valueType.isAssignableFrom(n.getClass())) {
26942695
return (T) n;
26952696
}
26962697
// 20-Apr-2016, tatu: Another thing: for VALUE_EMBEDDED_OBJECT, assume similar
@@ -3648,20 +3649,7 @@ public <T> T convertValue(Object fromValue, JavaType toValueType)
36483649
protected Object _convert(Object fromValue, JavaType toValueType)
36493650
throws IllegalArgumentException
36503651
{
3651-
// [databind#1433] Do not shortcut null values.
3652-
// This defaults primitives and fires deserializer getNullValue hooks.
3653-
if (fromValue != null) {
3654-
// also, as per [databind#11], consider case for simple cast
3655-
// But with caveats: one is that while everything is Object.class, we don't
3656-
// want to "optimize" that out; and the other is that we also do not want
3657-
// to lose conversions of generic types.
3658-
Class<?> targetType = toValueType.getRawClass();
3659-
if (targetType != Object.class
3660-
&& !toValueType.hasGenericTypes()
3661-
&& targetType.isAssignableFrom(fromValue.getClass())) {
3662-
return fromValue;
3663-
}
3664-
}
3652+
// 25-Jan-2019, tatu: [databind#2220] Let's NOT try to short-circuit anything
36653653

36663654
// Then use TokenBuffer, which is a JsonGenerator:
36673655
TokenBuffer buf = new TokenBuffer(this, false);

src/test/java/com/fasterxml/jackson/databind/convert/TestBeanConversions.java

+3-12
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,10 @@ private void _convertAndVerifyPoint(ObjectMapper m)
217217
*/
218218
public void testIssue11() throws Exception
219219
{
220-
// First the expected use case, Node specification
221-
ObjectNode root = MAPPER.createObjectNode();
222-
JsonNode n = root;
223-
ObjectNode ob2 = MAPPER.convertValue(n, ObjectNode.class);
224-
assertSame(root, ob2);
225-
226-
JsonNode n2 = MAPPER.convertValue(n, JsonNode.class);
227-
assertSame(root, n2);
228-
229220
// then some other no-op conversions
230-
String STR = "test";
231-
CharSequence seq = MAPPER.convertValue(STR, CharSequence.class);
232-
assertSame(STR, seq);
221+
StringBuilder SB = new StringBuilder("test");
222+
CharSequence seq = MAPPER.convertValue(SB, CharSequence.class);
223+
assertNotSame(SB, seq);
233224

234225
// and then something that should NOT use short-cut
235226
Leaf l = new Leaf(13);

0 commit comments

Comments
 (0)