Skip to content

Commit c100ed5

Browse files
committed
Fix #3234
1 parent 1dd2c08 commit c100ed5

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

release-notes/CREDITS-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,10 @@ Hyeonho Kim (proost@github)
13891389
* Contributed fix for #3227: Content `null` handling not working for root values
13901390
(2.13.0)
13911391
1392+
Peter Burka (pburka@github)
1393+
* Reported #3234: StdDeserializer rejects blank (all-whitespace) strings for ints
1394+
(2.13.0)
1395+
13921396
Abishek Ravichandran (abrav9595@github)
13931397
* Contributed #3259: Support for BCP 47 `java.util.Locale` serialization/deserialization
13941398
(2.13.0)

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Project: jackson-databind
6666
#3227: Content `null` handling not working for root values
6767
(reported by João G)
6868
(fix contributed by proost@github)
69+
#3234: StdDeserializer rejects blank (all-whitespace) strings for ints
70+
(reported by Peter B)
71+
(fix proposed by qthegreat3@github)
6972
#3235: `USE_BASE_TYPE_AS_DEFAULT_IMPL` not working with `DefaultTypeResolverBuilder`
7073
(reported, fix contributed by silas.u / sialais@github)
7174
#3238: Add PropertyNamingStrategies.UpperSnakeCaseStrategy (and UPPER_SNAKE_CASE constant)

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ public class CoercionConfig
2121
protected final CoercionAction[] _coercionsByShape;
2222

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

2830
protected CoercionConfig(CoercionConfig src) {

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,7 @@ public CoercionAction findCoercion(DeserializationConfig config,
215215

216216
// classic scalars are numbers, booleans; but date/time also considered
217217
// scalar for this particular purpose
218-
final boolean baseScalar = (targetType == LogicalType.Float)
219-
|| (targetType == LogicalType.Integer)
220-
|| (targetType == LogicalType.Boolean)
221-
|| (targetType == LogicalType.DateTime);
218+
final boolean baseScalar = _isScalarType(targetType);
222219

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

304301
// First: if using blank as empty is no-go, return what caller specified
305-
if (!Boolean.TRUE.equals(acceptBlankAsEmpty)) {
302+
if (Boolean.FALSE.equals(acceptBlankAsEmpty)) {
306303
return actionIfBlankNotAllowed;
307304
}
308-
309305
// Otherwise, if action found, return that
310306
if (action != null) {
311307
return action;
312308
}
309+
310+
// 23-Sep-2021, tatu: [databind#3234] Should default to "allow" for Scalar types
311+
// for backwards compatibility
312+
if (_isScalarType(targetType)) {
313+
return CoercionAction.AsNull;
314+
}
315+
313316
// If not, one specific legacy setting to consider...
314-
return config.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT) ?
315-
CoercionAction.AsNull : CoercionAction.Fail;
317+
if (config.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
318+
return CoercionAction.AsNull;
319+
}
320+
321+
// But finally consider ultimate default to be "false" and so:
322+
return actionIfBlankNotAllowed;
323+
}
324+
325+
protected boolean _isScalarType(LogicalType targetType) {
326+
return (targetType == LogicalType.Float)
327+
|| (targetType == LogicalType.Integer)
328+
|| (targetType == LogicalType.Boolean)
329+
|| (targetType == LogicalType.DateTime);
316330
}
317331
}

src/test/java/com/fasterxml/jackson/failing/CoerceEmptyToInt3234Test.java renamed to src/test/java/com/fasterxml/jackson/databind/convert/CoerceEmptyToInt3234Test.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.failing;
1+
package com.fasterxml.jackson.databind.convert;
22

33
import com.fasterxml.jackson.databind.*;
44

@@ -13,9 +13,14 @@ static class BasicLongWrapper {
1313
public long value = 7L;
1414
}
1515

16+
static class BasicDoubleWrapper {
17+
public double value = -1.25;
18+
}
19+
1620
private final ObjectMapper MAPPER = newJsonMapper();
1721
private final ObjectReader READER_INT_BASIC = MAPPER.readerFor(BasicIntWrapper.class);
1822
private final ObjectReader READER_LONG_BASIC = MAPPER.readerFor(BasicLongWrapper.class);
23+
private final ObjectReader READER_DOUBLE_BASIC = MAPPER.readerFor(BasicDoubleWrapper.class);
1924

2025
// // // Ints
2126

@@ -44,4 +49,18 @@ public void testSimpleLongFromBlank() throws Exception
4449
BasicLongWrapper w = READER_LONG_BASIC.readValue(a2q("{'value':' '}"));
4550
assertEquals(0L, w.value);
4651
}
52+
53+
// // // Double
54+
55+
public void testSimpleDoublegFromEmpty() throws Exception
56+
{
57+
BasicDoubleWrapper w = READER_DOUBLE_BASIC.readValue(a2q("{'value':''}"));
58+
assertEquals((double) 0, w.value);
59+
}
60+
61+
public void testSimpleDoubleFromBlank() throws Exception
62+
{
63+
BasicDoubleWrapper w = READER_DOUBLE_BASIC.readValue(a2q("{'value':' '}"));
64+
assertEquals((double) 0, w.value);
65+
}
4766
}

0 commit comments

Comments
 (0)