|
| 1 | +package com.fasterxml.jackson.databind.deser.std; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 4 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 5 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 6 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import com.fasterxml.jackson.databind.cfg.CoercionAction; |
| 9 | +import com.fasterxml.jackson.databind.cfg.CoercionInputShape; |
| 10 | +import org.junit.Assert; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +public class EmptyStringAsSingleValueTest { |
| 17 | + private static ObjectMapper normalMapper() { |
| 18 | + ObjectMapper mapper = new ObjectMapper(); |
| 19 | + mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); |
| 20 | + return mapper; |
| 21 | + } |
| 22 | + |
| 23 | + private static ObjectMapper coercionMapper() { |
| 24 | + ObjectMapper mapper = normalMapper(); |
| 25 | + // same as XmlMapper |
| 26 | + mapper.coercionConfigDefaults() |
| 27 | + .setAcceptBlankAsEmpty(true) |
| 28 | + .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty); |
| 29 | + return mapper; |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void emptyToList() throws JsonProcessingException { |
| 34 | + // NO coercion + empty string input + StringCollectionDeserializer |
| 35 | + Assert.assertEquals(Collections.singletonList(""), normalMapper().readValue("\"\"", new TypeReference<List<String>>() {})); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void emptyToListWrapper() throws JsonProcessingException { |
| 40 | + // NO coercion + empty string input + normal CollectionDeserializer |
| 41 | + Assert.assertEquals(Collections.singletonList(new StringWrapper("")), normalMapper().readValue("\"\"", new TypeReference<List<StringWrapper>>() {})); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void coercedEmptyToList() throws JsonProcessingException { |
| 46 | + // YES coercion + empty string input + StringCollectionDeserializer |
| 47 | + Assert.assertEquals(Collections.emptyList(), coercionMapper().readValue("\"\"", new TypeReference<List<String>>() {})); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void coercedEmptyToListWrapper() throws JsonProcessingException { |
| 52 | + // YES coercion + empty string input + normal CollectionDeserializer |
| 53 | + Assert.assertEquals(Collections.emptyList(), coercionMapper().readValue("\"\"", new TypeReference<List<StringWrapper>>() {})); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void coercedListToList() throws JsonProcessingException { |
| 58 | + // YES coercion + empty LIST input + StringCollectionDeserializer |
| 59 | + Assert.assertEquals(Collections.emptyList(), coercionMapper().readValue("[]", new TypeReference<List<String>>() {})); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void coercedListToListWrapper() throws JsonProcessingException { |
| 64 | + // YES coercion + empty LIST input + normal CollectionDeserializer |
| 65 | + Assert.assertEquals(Collections.emptyList(), coercionMapper().readValue("[]", new TypeReference<List<StringWrapper>>() {})); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void blankToList() throws JsonProcessingException { |
| 70 | + // NO coercion + empty string input + StringCollectionDeserializer |
| 71 | + Assert.assertEquals(Collections.singletonList(" "), normalMapper().readValue("\" \"", new TypeReference<List<String>>() {})); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void blankToListWrapper() throws JsonProcessingException { |
| 76 | + // NO coercion + empty string input + normal CollectionDeserializer |
| 77 | + Assert.assertEquals(Collections.singletonList(new StringWrapper(" ")), normalMapper().readValue("\" \"", new TypeReference<List<StringWrapper>>() {})); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void coercedBlankToList() throws JsonProcessingException { |
| 82 | + // YES coercion + empty string input + StringCollectionDeserializer |
| 83 | + Assert.assertEquals(Collections.emptyList(), coercionMapper().readValue("\" \"", new TypeReference<List<String>>() {})); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void coercedBlankToListWrapper() throws JsonProcessingException { |
| 88 | + // YES coercion + empty string input + normal CollectionDeserializer |
| 89 | + Assert.assertEquals(Collections.emptyList(), coercionMapper().readValue("\" \"", new TypeReference<List<StringWrapper>>() {})); |
| 90 | + } |
| 91 | + |
| 92 | + private static final class StringWrapper { |
| 93 | + private final String s; |
| 94 | + |
| 95 | + @JsonCreator(mode = JsonCreator.Mode.DELEGATING) |
| 96 | + public StringWrapper(String s) { |
| 97 | + this.s = s; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public String toString() { |
| 102 | + return "StringWrapper{" + s + "}"; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public boolean equals(Object obj) { |
| 107 | + return obj instanceof StringWrapper && ((StringWrapper) obj).s.equals(s); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public int hashCode() { |
| 112 | + return s.hashCode(); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments