Skip to content

Commit

Permalink
Fix #3234
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 24, 2021
1 parent 1dd2c08 commit c100ed5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,10 @@ Hyeonho Kim (proost@github)
* Contributed fix for #3227: Content `null` handling not working for root values
(2.13.0)
Peter Burka (pburka@github)
* Reported #3234: StdDeserializer rejects blank (all-whitespace) strings for ints
(2.13.0)
Abishek Ravichandran (abrav9595@github)
* Contributed #3259: Support for BCP 47 `java.util.Locale` serialization/deserialization
(2.13.0)
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ Project: jackson-databind
#3227: Content `null` handling not working for root values
(reported by João G)
(fix contributed by proost@github)
#3234: StdDeserializer rejects blank (all-whitespace) strings for ints
(reported by Peter B)
(fix proposed by qthegreat3@github)
#3235: `USE_BASE_TYPE_AS_DEFAULT_IMPL` not working with `DefaultTypeResolverBuilder`
(reported, fix contributed by silas.u / sialais@github)
#3238: Add PropertyNamingStrategies.UpperSnakeCaseStrategy (and UPPER_SNAKE_CASE constant)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public class CoercionConfig
protected final CoercionAction[] _coercionsByShape;

public CoercionConfig() {
_coercionsByShape = new CoercionAction[INPUT_SHAPE_COUNT];
_acceptBlankAsEmpty = false;
_coercionsByShape = new CoercionAction[INPUT_SHAPE_COUNT];
// 23-Sep-2021, tatu: In 2.12 was `false` but should really be `null`
// to mean "not specified" (use defaults)
_acceptBlankAsEmpty = null;
}

protected CoercionConfig(CoercionConfig src) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ public CoercionAction findCoercion(DeserializationConfig config,

// classic scalars are numbers, booleans; but date/time also considered
// scalar for this particular purpose
final boolean baseScalar = (targetType == LogicalType.Float)
|| (targetType == LogicalType.Integer)
|| (targetType == LogicalType.Boolean)
|| (targetType == LogicalType.DateTime);
final boolean baseScalar = _isScalarType(targetType);

if (baseScalar) {
// Default for setting in 2.x is true
Expand Down Expand Up @@ -302,16 +299,33 @@ public CoercionAction findCoercionFromBlankString(DeserializationConfig config,
}

// First: if using blank as empty is no-go, return what caller specified
if (!Boolean.TRUE.equals(acceptBlankAsEmpty)) {
if (Boolean.FALSE.equals(acceptBlankAsEmpty)) {
return actionIfBlankNotAllowed;
}

// Otherwise, if action found, return that
if (action != null) {
return action;
}

// 23-Sep-2021, tatu: [databind#3234] Should default to "allow" for Scalar types
// for backwards compatibility
if (_isScalarType(targetType)) {
return CoercionAction.AsNull;
}

// If not, one specific legacy setting to consider...
return config.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT) ?
CoercionAction.AsNull : CoercionAction.Fail;
if (config.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
return CoercionAction.AsNull;
}

// But finally consider ultimate default to be "false" and so:
return actionIfBlankNotAllowed;
}

protected boolean _isScalarType(LogicalType targetType) {
return (targetType == LogicalType.Float)
|| (targetType == LogicalType.Integer)
|| (targetType == LogicalType.Boolean)
|| (targetType == LogicalType.DateTime);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.failing;
package com.fasterxml.jackson.databind.convert;

import com.fasterxml.jackson.databind.*;

Expand All @@ -13,9 +13,14 @@ static class BasicLongWrapper {
public long value = 7L;
}

static class BasicDoubleWrapper {
public double value = -1.25;
}

private final ObjectMapper MAPPER = newJsonMapper();
private final ObjectReader READER_INT_BASIC = MAPPER.readerFor(BasicIntWrapper.class);
private final ObjectReader READER_LONG_BASIC = MAPPER.readerFor(BasicLongWrapper.class);
private final ObjectReader READER_DOUBLE_BASIC = MAPPER.readerFor(BasicDoubleWrapper.class);

// // // Ints

Expand Down Expand Up @@ -44,4 +49,18 @@ public void testSimpleLongFromBlank() throws Exception
BasicLongWrapper w = READER_LONG_BASIC.readValue(a2q("{'value':' '}"));
assertEquals(0L, w.value);
}

// // // Double

public void testSimpleDoublegFromEmpty() throws Exception
{
BasicDoubleWrapper w = READER_DOUBLE_BASIC.readValue(a2q("{'value':''}"));
assertEquals((double) 0, w.value);
}

public void testSimpleDoubleFromBlank() throws Exception
{
BasicDoubleWrapper w = READER_DOUBLE_BASIC.readValue(a2q("{'value':' '}"));
assertEquals((double) 0, w.value);
}
}

0 comments on commit c100ed5

Please sign in to comment.