Skip to content

Commit c806f26

Browse files
committed
Minor tweaks to #3418
1 parent 9f68b51 commit c806f26

File tree

3 files changed

+133
-117
lines changed

3 files changed

+133
-117
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,22 @@ public void testScalarCollections() throws Exception
2929
{
3030
final JavaType listType = VANILLA_MAPPER.getTypeFactory()
3131
.constructType(new TypeReference<List<Double>>() { });
32-
_verifyNoCoercion(listType);
32+
33+
// 03-Aug-2022, tatu: Due to [databind#3418] message changed; not
34+
// 100% sure how it should work but let's try this
35+
36+
// _verifyNoCoercion(listType);
37+
try {
38+
VANILLA_MAPPER.readerFor(listType).readValue(JSON_EMPTY);
39+
fail("Should not pass");
40+
} catch (DatabindException e) {
41+
// verifyException(e, "Cannot coerce empty String");
42+
verifyException(e, "Cannot deserialize value of type");
43+
verifyException(e, "from String value");
44+
e.printStackTrace();
45+
}
46+
47+
3348
List<Double> result = _readWithCoercion(listType);
3449
assertNotNull(result);
3550
assertEquals(0, result.size());
@@ -159,7 +174,7 @@ private void _verifyNoCoercion(JavaType targetType) throws Exception {
159174
try {
160175
VANILLA_MAPPER.readerFor(targetType).readValue(JSON_EMPTY);
161176
fail("Should not pass");
162-
} catch (Exception e) {
177+
} catch (DatabindException e) {
163178
// 06-Nov-2020, tatu: tests for failure get rather fragile unfortunately,
164179
// but this seems to be what we should be getting
165180

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.fasterxml.jackson.databind.convert;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
import com.fasterxml.jackson.annotation.JsonCreator;
7+
8+
import com.fasterxml.jackson.core.type.TypeReference;
9+
10+
import com.fasterxml.jackson.databind.BaseMapTest;
11+
import com.fasterxml.jackson.databind.DeserializationFeature;
12+
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import com.fasterxml.jackson.databind.cfg.CoercionAction;
14+
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
15+
16+
// [databind#3418]: Coercion from empty String to Collection<String>, with
17+
// `DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY`
18+
public class EmptyStringAsSingleValueTest extends BaseMapTest
19+
{
20+
static final class StringWrapper {
21+
private final String s;
22+
23+
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
24+
public StringWrapper(String s) {
25+
this.s = s;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "StringWrapper{" + s + "}";
31+
}
32+
33+
@Override
34+
public boolean equals(Object obj) {
35+
return obj instanceof StringWrapper && ((StringWrapper) obj).s.equals(s);
36+
}
37+
38+
@Override
39+
public int hashCode() {
40+
return s.hashCode();
41+
}
42+
}
43+
44+
private final ObjectMapper NORMAL_MAPPER = jsonMapperBuilder()
45+
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
46+
.build();
47+
48+
private final ObjectMapper COERCION_MAPPER = jsonMapperBuilder()
49+
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
50+
// same as XmlMapper
51+
.withCoercionConfigDefaults(h -> {
52+
h.setAcceptBlankAsEmpty(true)
53+
.setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty);
54+
})
55+
.build();
56+
57+
public void testEmptyToList() throws Exception {
58+
// NO coercion + empty string input + StringCollectionDeserializer
59+
assertEquals(Collections.singletonList(""),
60+
NORMAL_MAPPER.readValue("\"\"", new TypeReference<List<String>>() {}));
61+
}
62+
63+
public void testEmptyToListWrapper() throws Exception {
64+
// NO coercion + empty string input + normal CollectionDeserializer
65+
assertEquals(Collections.singletonList(new StringWrapper("")),
66+
NORMAL_MAPPER.readValue("\"\"", new TypeReference<List<StringWrapper>>() {}));
67+
}
68+
69+
public void testCoercedEmptyToList() throws Exception {
70+
// YES coercion + empty string input + StringCollectionDeserializer
71+
assertEquals(Collections.emptyList(), COERCION_MAPPER.readValue("\"\"",
72+
new TypeReference<List<String>>() {}));
73+
}
74+
75+
public void testCoercedEmptyToListWrapper() throws Exception {
76+
// YES coercion + empty string input + normal CollectionDeserializer
77+
assertEquals(Collections.emptyList(),
78+
COERCION_MAPPER.readValue("\"\"", new TypeReference<List<StringWrapper>>() {}));
79+
}
80+
81+
public void testCoercedListToList() throws Exception {
82+
// YES coercion + empty LIST input + StringCollectionDeserializer
83+
assertEquals(Collections.emptyList(),
84+
COERCION_MAPPER.readValue("[]", new TypeReference<List<String>>() {}));
85+
}
86+
87+
public void testCoercedListToListWrapper() throws Exception {
88+
// YES coercion + empty LIST input + normal CollectionDeserializer
89+
assertEquals(Collections.emptyList(),
90+
COERCION_MAPPER.readValue("[]", new TypeReference<List<StringWrapper>>() {}));
91+
}
92+
93+
public void testBlankToList() throws Exception {
94+
// NO coercion + empty string input + StringCollectionDeserializer
95+
assertEquals(Collections.singletonList(" "),
96+
NORMAL_MAPPER.readValue("\" \"", new TypeReference<List<String>>() {}));
97+
}
98+
99+
public void testBlankToListWrapper() throws Exception {
100+
// NO coercion + empty string input + normal CollectionDeserializer
101+
assertEquals(Collections.singletonList(new StringWrapper(" ")),
102+
NORMAL_MAPPER.readValue("\" \"", new TypeReference<List<StringWrapper>>() {}));
103+
}
104+
105+
public void testCoercedBlankToList() throws Exception {
106+
// YES coercion + empty string input + StringCollectionDeserializer
107+
assertEquals(Collections.emptyList(),
108+
COERCION_MAPPER.readValue("\" \"", new TypeReference<List<String>>() {}));
109+
}
110+
111+
public void testCoercedBlankToListWrapper() throws Exception {
112+
// YES coercion + empty string input + normal CollectionDeserializer
113+
assertEquals(Collections.emptyList(),
114+
COERCION_MAPPER.readValue("\" \"", new TypeReference<List<StringWrapper>>() {}));
115+
}
116+
}

src/test/java/com/fasterxml/jackson/databind/deser/std/EmptyStringAsSingleValueTest.java

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)