Skip to content

Commit

Permalink
fix #3624 ALLOW_COERCION_OF_SCALARS allows int->float coercion (#3625)
Browse files Browse the repository at this point in the history
  • Loading branch information
carterkozak authored Oct 13, 2022
1 parent 070cf68 commit 02f8b14
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,14 @@ public CoercionAction findCoercion(DeserializationConfig config,
// scalar for this particular purpose
final boolean baseScalar = _isScalarType(targetType);

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

if (inputShape == CoercionInputShape.EmptyString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
Expand Down Expand Up @@ -34,6 +35,10 @@ public class CoerceIntToFloatTest extends BaseMapTest
cfg.setCoercion(CoercionInputShape.Integer, CoercionAction.AsEmpty))
.build();

private final ObjectMapper LEGACY_SCALAR_COERCION_FAIL = jsonMapperBuilder()
.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS)
.build();

public void testDefaultIntToFloatCoercion() throws JsonProcessingException
{
assertSuccessfulIntToFloatConversionsWith(DEFAULT_MAPPER);
Expand Down Expand Up @@ -115,6 +120,11 @@ public void testCoerceConfigToFail() throws JsonProcessingException
_verifyCoerceFail(MAPPER_TO_FAIL, BigDecimal.class, "73455342");
}

public void testLegacyConfiguration() throws JsonProcessingException
{
assertSuccessfulIntToFloatConversionsWith(LEGACY_SCALAR_COERCION_FAIL);
}

/*
/********************************************************
/* Helper methods
Expand Down

0 comments on commit 02f8b14

Please sign in to comment.