From 866b2b42df80f91bd2adc33751787d6fb6e900cf Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Thu, 10 Dec 2020 13:13:43 -0800 Subject: [PATCH] Minor fix for #2215 implementation, related to #2978 --- .../databind/deser/ValueInstantiator.java | 8 ++-- .../deser/std/StdValueInstantiator.java | 25 +++++----- .../jackson/databind/BaseMapTest.java | 18 ------- .../databind/deser/creators/TestCreators.java | 47 ++++++++++++++++--- 4 files changed, 56 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java b/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java index aa658e36bb..afaebdc345 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/ValueInstantiator.java @@ -335,8 +335,8 @@ public Object createFromLong(DeserializationContext ctxt, long value) throws IOE public Object createFromBigInteger(DeserializationContext ctxt, BigInteger value) throws IOException { return ctxt.handleMissingInstantiator(getValueClass(),this,null, - "no BigInteger-argument constructor/factory method to deserialize from Number value (%s)", - value + "no BigInteger-argument constructor/factory method to deserialize from Number value (%s)", + value ); } @@ -349,8 +349,8 @@ public Object createFromDouble(DeserializationContext ctxt, double value) throws public Object createFromBigDecimal(DeserializationContext ctxt, BigDecimal value) throws IOException { return ctxt.handleMissingInstantiator(getValueClass(),this,null, - "no BigDecimal/double/Double-argument constructor/factory method to deserialize from Number value (%s)", - value + "no BigDecimal/double/Double-argument constructor/factory method to deserialize from Number value (%s)", + value ); } diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java index 280857cb35..bdcf988bb2 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java @@ -368,7 +368,7 @@ public Object createFromInt(DeserializationContext ctxt, int value) throws IOExc return _fromBigIntegerCreator.call1(arg); } catch (Throwable t0) { return ctxt.handleInstantiationProblem(_fromBigIntegerCreator.getDeclaringClass(), - arg, rewrapCtorProblem(ctxt, t0) + arg, rewrapCtorProblem(ctxt, t0) ); } } @@ -380,24 +380,23 @@ arg, rewrapCtorProblem(ctxt, t0) public Object createFromLong(DeserializationContext ctxt, long value) throws IOException { if (_fromLongCreator != null) { - Object arg = Long.valueOf(value); + Long arg = Long.valueOf(value); try { return _fromLongCreator.call1(arg); } catch (Throwable t0) { return ctxt.handleInstantiationProblem(_fromLongCreator.getDeclaringClass(), - arg, - rewrapCtorProblem(ctxt, t0) + arg, rewrapCtorProblem(ctxt, t0) ); } } if (_fromBigIntegerCreator != null) { - Object arg = BigInteger.valueOf(value); + BigInteger arg = BigInteger.valueOf(value); try { return _fromBigIntegerCreator.call1(arg); } catch (Throwable t0) { return ctxt.handleInstantiationProblem(_fromBigIntegerCreator.getDeclaringClass(), - arg, rewrapCtorProblem(ctxt, t0) + arg, rewrapCtorProblem(ctxt, t0) ); } } @@ -408,12 +407,12 @@ arg, rewrapCtorProblem(ctxt, t0) @Override public Object createFromBigInteger(DeserializationContext ctxt, BigInteger value) throws IOException { - if (_fromBigDecimalCreator != null) { + if (_fromBigIntegerCreator != null) { try { return _fromBigIntegerCreator.call1(value); } catch (Throwable t) { return ctxt.handleInstantiationProblem(_fromBigIntegerCreator.getDeclaringClass(), - value, rewrapCtorProblem(ctxt, t) + value, rewrapCtorProblem(ctxt, t) ); } } @@ -425,22 +424,22 @@ value, rewrapCtorProblem(ctxt, t) public Object createFromDouble(DeserializationContext ctxt, double value) throws IOException { if(_fromDoubleCreator != null) { - Object arg = Double.valueOf(value); + Double arg = Double.valueOf(value); try { return _fromDoubleCreator.call1(arg); } catch (Throwable t0) { return ctxt.handleInstantiationProblem(_fromDoubleCreator.getDeclaringClass(), - arg, rewrapCtorProblem(ctxt, t0)); + arg, rewrapCtorProblem(ctxt, t0)); } } if (_fromBigDecimalCreator != null) { - Object arg = BigDecimal.valueOf(value); + BigDecimal arg = BigDecimal.valueOf(value); try { return _fromBigDecimalCreator.call1(arg); } catch (Throwable t0) { return ctxt.handleInstantiationProblem(_fromBigDecimalCreator.getDeclaringClass(), - arg, rewrapCtorProblem(ctxt, t0)); + arg, rewrapCtorProblem(ctxt, t0)); } } @@ -455,7 +454,7 @@ public Object createFromBigDecimal(DeserializationContext ctxt, BigDecimal value return _fromBigDecimalCreator.call1(value); } catch (Throwable t) { return ctxt.handleInstantiationProblem(_fromBigDecimalCreator.getDeclaringClass(), - value, rewrapCtorProblem(ctxt, t) + value, rewrapCtorProblem(ctxt, t) ); } } diff --git a/src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java b/src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java index 7d37b692f3..fc65e756d3 100644 --- a/src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java @@ -1,8 +1,6 @@ package com.fasterxml.jackson.databind; import java.io.*; -import java.math.BigDecimal; -import java.math.BigInteger; import java.util.*; import static org.junit.Assert.*; @@ -59,14 +57,6 @@ public LongWrapper() { } public LongWrapper(long value) { l = value; } } - protected static class BigIntegerWrapper { - public BigInteger i; - - public BigIntegerWrapper() { } - - public BigIntegerWrapper(final BigInteger value) { i = value; } - } - protected static class DoubleWrapper { public double d; @@ -74,14 +64,6 @@ public DoubleWrapper() { } public DoubleWrapper(double value) { d = value; } } - protected static class BigDecimalWrapper { - public BigDecimal d; - - public BigDecimalWrapper() { } - - public BigDecimalWrapper(final BigDecimal value) { d = value; } - } - /** * Simple wrapper around String type, usually to test value * conversions or wrapping diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/creators/TestCreators.java b/src/test/java/com/fasterxml/jackson/databind/deser/creators/TestCreators.java index 7c8e389d8c..ef810431a8 100644 --- a/src/test/java/com/fasterxml/jackson/databind/deser/creators/TestCreators.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/creators/TestCreators.java @@ -5,9 +5,10 @@ import java.util.*; import com.fasterxml.jackson.annotation.*; - +import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.exc.InvalidDefinitionException; +import com.fasterxml.jackson.databind.util.TokenBuffer; /** * Unit tests for verifying that it is possible to annotate @@ -172,7 +173,7 @@ static class NoArgFactoryBean { public static NoArgFactoryBean create() { return new NoArgFactoryBean(123); } } - // [Issue#208] + // [databind#208] static class FromStringBean { protected String value; @@ -185,7 +186,25 @@ public static FromStringBean fromString(String s) { return new FromStringBean(s, false); } } - + + // [databind#2215] + protected static class BigIntegerWrapper { + BigInteger _value; + + public BigIntegerWrapper() { } + + public BigIntegerWrapper(final BigInteger value) { _value = value; } + } + + // [databind#2215] + protected static class BigDecimalWrapper { + BigDecimal _value; + + public BigDecimalWrapper() { } + + public BigDecimalWrapper(final BigDecimal value) { _value = value; } + } + /* /********************************************************** /* Annotated helper classes, mixed (creator and props) @@ -333,14 +352,28 @@ public void testSimpleBooleanConstructor() throws Exception public void testSimpleBigIntegerConstructor() throws Exception { - final BigIntegerWrapper result = MAPPER.readValue("17", BigIntegerWrapper.class); - assertEquals(new BigInteger("17"), result.i); + // 10-Dec-2020, tatu: Small (magnitude) values will NOT trigger path + // we want; must use something outside of Long range... + + BigInteger INPUT = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN); + final BigIntegerWrapper result = MAPPER.readValue(INPUT.toString(), BigIntegerWrapper.class); + assertEquals(INPUT, result._value); } public void testSimpleBigDecimalConstructor() throws Exception { - final BigDecimalWrapper result = MAPPER.readValue("42.5", BigDecimalWrapper.class); - assertEquals(new BigDecimal("42.5"), result.d); + // 10-Dec-2020, tatu: not sure we can ever trigger this with JSON; + // but should be possible to handle via TokenBuffer? + + BigDecimal INPUT = new BigDecimal("42.5"); + try (TokenBuffer buf = new TokenBuffer(null, false)) { + buf.writeNumber(INPUT); + try (JsonParser p = buf.asParser()) { + final BigDecimalWrapper result = MAPPER.readValue(p, + BigDecimalWrapper.class); + assertEquals(INPUT, result._value); + } + } } public void testSimpleFactory() throws Exception