|
| 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 | +} |
0 commit comments