Skip to content

Commit a67a72b

Browse files
authored
Improve JavaDoc and Test for config ACCEPT_EMPTY_STRING_AS_NULL_OBJECT wrt special cases (#4012)
1 parent 4a0d565 commit a67a72b

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/main/java/com/fasterxml/jackson/databind/DeserializationFeature.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ public enum DeserializationFeature implements ConfigFeature
358358
* whether they can be coerced depends on
359359
* {@link MapperFeature#ALLOW_COERCION_OF_SCALARS}.
360360
*<p>
361+
* IMPORTANT: This feature might work even when an empty string {@code ""}
362+
* may be a valid value for some types.
363+
*<p>
361364
* Feature is disabled by default.
362365
*/
363366
ACCEPT_EMPTY_STRING_AS_NULL_OBJECT(false),
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fasterxml.jackson.databind.deser.jdk;
2+
3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.BaseMapTest;
6+
import com.fasterxml.jackson.databind.DeserializationFeature;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.ObjectReader;
9+
import java.util.Locale;
10+
11+
// [databind#4009] Locale "" is deserialised as NULL if ACCEPT_EMPTY_STRING_AS_NULL_OBJECT is true
12+
public class LocaleDeser4009Test extends BaseMapTest
13+
{
14+
15+
static class MyPojo {
16+
public String field;
17+
}
18+
final ObjectMapper mapper = newJsonMapper();
19+
20+
final ObjectReader DISABLED_READER = mapper.reader()
21+
.without(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
22+
23+
final ObjectReader ENABLED_READER = mapper.reader()
24+
.with(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
25+
26+
public void testPOJOWithFeatureDisabled()
27+
{
28+
assertThrows(JsonProcessingException.class, () -> {
29+
DISABLED_READER.readValue("\"\"", MyPojo.class);
30+
});
31+
}
32+
33+
public void testPOJOWithFeatureEnabled() throws Exception
34+
{
35+
assertNull(ENABLED_READER.readValue("\"\"", MyPojo.class));
36+
}
37+
38+
public void testLocaleWithFeatureDisabled() throws Exception
39+
{
40+
assertEquals(Locale.ROOT, DISABLED_READER.readValue("\"\"", Locale.class));
41+
}
42+
43+
public void testLocaleWithFeatureEnabled() throws Exception
44+
{
45+
assertNull(ENABLED_READER.readValue("\"\"", Locale.class));
46+
}
47+
}

0 commit comments

Comments
 (0)