Skip to content

Commit 02f8b14

Browse files
authored
fix #3624 ALLOW_COERCION_OF_SCALARS allows int->float coercion (#3625)
1 parent 070cf68 commit 02f8b14

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/main/java/com/fasterxml/jackson/databind/cfg/CoercionConfigs.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,14 @@ public CoercionAction findCoercion(DeserializationConfig config,
217217
// scalar for this particular purpose
218218
final boolean baseScalar = _isScalarType(targetType);
219219

220-
if (baseScalar) {
221-
// Default for setting in 2.x is true
222-
if (!config.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS)) {
220+
if (baseScalar
221+
// Default for setting in 2.x is true
222+
&& !config.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS)
223+
// Coercion from integer-shaped data into a floating point type is not banned by the
224+
// ALLOW_COERCION_OF_SCALARS feature because '1' is a valid JSON representation of
225+
// '1.0' in a way that other types of coercion do not satisfy.
226+
&& (targetType != LogicalType.Float || inputShape != CoercionInputShape.Integer)) {
223227
return CoercionAction.Fail;
224-
}
225228
}
226229

227230
if (inputShape == CoercionInputShape.EmptyString) {

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

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.BaseMapTest;
5+
import com.fasterxml.jackson.databind.MapperFeature;
56
import com.fasterxml.jackson.databind.ObjectMapper;
67
import com.fasterxml.jackson.databind.ObjectReader;
78
import com.fasterxml.jackson.databind.cfg.CoercionAction;
@@ -34,6 +35,10 @@ public class CoerceIntToFloatTest extends BaseMapTest
3435
cfg.setCoercion(CoercionInputShape.Integer, CoercionAction.AsEmpty))
3536
.build();
3637

38+
private final ObjectMapper LEGACY_SCALAR_COERCION_FAIL = jsonMapperBuilder()
39+
.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS)
40+
.build();
41+
3742
public void testDefaultIntToFloatCoercion() throws JsonProcessingException
3843
{
3944
assertSuccessfulIntToFloatConversionsWith(DEFAULT_MAPPER);
@@ -115,6 +120,11 @@ public void testCoerceConfigToFail() throws JsonProcessingException
115120
_verifyCoerceFail(MAPPER_TO_FAIL, BigDecimal.class, "73455342");
116121
}
117122

123+
public void testLegacyConfiguration() throws JsonProcessingException
124+
{
125+
assertSuccessfulIntToFloatConversionsWith(LEGACY_SCALAR_COERCION_FAIL);
126+
}
127+
118128
/*
119129
/********************************************************
120130
/* Helper methods

0 commit comments

Comments
 (0)