|
| 1 | +package tools.jackson.databind.tofix; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 4 | + |
| 5 | +import tools.jackson.databind.ObjectMapper; |
| 6 | +import tools.jackson.databind.testutil.DatabindTestUtil; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import tools.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 12 | + |
| 13 | +// [databind#5515] Need to support JsonInclude for Arrays as well |
| 14 | +public class JsonIncludeForArray5515Test |
| 15 | + extends DatabindTestUtil |
| 16 | +{ |
| 17 | + /* |
| 18 | + /********************************************************** |
| 19 | + /* Filters |
| 20 | + /********************************************************** |
| 21 | + */ |
| 22 | + |
| 23 | + static class Foo5515Filter { |
| 24 | + @Override |
| 25 | + public boolean equals(Object other) { |
| 26 | + if (other == null) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + return "foo".equals(other); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /* |
| 34 | + /********************************************************** |
| 35 | + /* POJOs — one per array type |
| 36 | + /********************************************************** |
| 37 | + */ |
| 38 | + |
| 39 | + static class ObjectArray5155Pojo { |
| 40 | + @JsonInclude(content = JsonInclude.Include.CUSTOM, |
| 41 | + contentFilter = Foo5515Filter.class) |
| 42 | + public Object[] values; |
| 43 | + |
| 44 | + ObjectArray5155Pojo(Object... v) { |
| 45 | + values = v; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static class StringArray5515Pojo { |
| 50 | + @JsonInclude(content = JsonInclude.Include.CUSTOM, |
| 51 | + contentFilter = Foo5515Filter.class) |
| 52 | + public String[] values; |
| 53 | + |
| 54 | + StringArray5515Pojo(String... v) { |
| 55 | + values = v; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + static class BooleanArray5515Pojo { |
| 60 | + @JsonInclude(content = JsonInclude.Include.NON_DEFAULT) |
| 61 | + public boolean[] values; |
| 62 | + |
| 63 | + BooleanArray5515Pojo(boolean... v) { |
| 64 | + values = v; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + static class IntArray5515Pojo { |
| 69 | + @JsonInclude(content = JsonInclude.Include.NON_DEFAULT) |
| 70 | + public int[] values; |
| 71 | + |
| 72 | + IntArray5515Pojo(int... v) { |
| 73 | + values = v; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + static class LongArray5515Pojo { |
| 78 | + @JsonInclude(content = JsonInclude.Include.NON_DEFAULT) |
| 79 | + public long[] values; |
| 80 | + |
| 81 | + LongArray5515Pojo(long... v) { |
| 82 | + values = v; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + static class DoubleArray5515Pojo { |
| 87 | + @JsonInclude(content = JsonInclude.Include.NON_DEFAULT) |
| 88 | + public double[] values; |
| 89 | + |
| 90 | + DoubleArray5515Pojo(double... v) { |
| 91 | + values = v; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /* |
| 96 | + /********************************************************** |
| 97 | + /* Mapper |
| 98 | + /********************************************************** |
| 99 | + */ |
| 100 | + |
| 101 | + private final ObjectMapper MAPPER = jsonMapperBuilder() |
| 102 | + // We need something like this. |
| 103 | + // .enable(SerializationFeature.APPLY_JSON_INCLUDE_FOR_ARRAYS) |
| 104 | + .build(); |
| 105 | + |
| 106 | + /* |
| 107 | + /********************************************************** |
| 108 | + /* Tests — reference arrays (expect filtering, FAIL today) |
| 109 | + /********************************************************** |
| 110 | + */ |
| 111 | + |
| 112 | + @JacksonTestFailureExpected |
| 113 | + @Test |
| 114 | + public void testCustomFilterWithObjectArray() throws Exception { |
| 115 | + ObjectArray5155Pojo input = new ObjectArray5155Pojo( |
| 116 | + "1", "foo", "2" |
| 117 | + ); |
| 118 | + |
| 119 | + // EXPECT foo to be filtered out — FAILS today |
| 120 | + assertEquals( |
| 121 | + a2q("{'values':['1','2']}"), |
| 122 | + MAPPER.writeValueAsString(input) |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + @JacksonTestFailureExpected |
| 127 | + @Test |
| 128 | + public void testCustomFilterWithStringArray() throws Exception { |
| 129 | + StringArray5515Pojo input = new StringArray5515Pojo( |
| 130 | + "1", "foo", "2" |
| 131 | + ); |
| 132 | + |
| 133 | + // EXPECT foo to be filtered out — FAILS today |
| 134 | + assertEquals( |
| 135 | + a2q("{'values':['1','2']}"), |
| 136 | + MAPPER.writeValueAsString(input) |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + /* |
| 141 | + /********************************************************** |
| 142 | + /* Tests — primitive arrays (expect NON_DEFAULT filtering, FAIL today) |
| 143 | + /********************************************************** |
| 144 | + */ |
| 145 | + |
| 146 | + @JacksonTestFailureExpected |
| 147 | + @Test |
| 148 | + public void testNonDefaultWithBooleanArray() throws Exception { |
| 149 | + BooleanArray5515Pojo input = new BooleanArray5515Pojo( |
| 150 | + true, false, true |
| 151 | + ); |
| 152 | + |
| 153 | + // EXPECT default 'false' to be filtered out — FAILS today |
| 154 | + assertEquals( |
| 155 | + a2q("{'values':[true,true]}"), |
| 156 | + MAPPER.writeValueAsString(input) |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + @JacksonTestFailureExpected |
| 161 | + @Test |
| 162 | + public void testNonDefaultWithIntArray() throws Exception { |
| 163 | + IntArray5515Pojo input = new IntArray5515Pojo( |
| 164 | + 0, 1, 0, 2 |
| 165 | + ); |
| 166 | + |
| 167 | + // EXPECT default '0' to be filtered out — FAILS today |
| 168 | + assertEquals( |
| 169 | + a2q("{'values':[1,2]}"), |
| 170 | + MAPPER.writeValueAsString(input) |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + @JacksonTestFailureExpected |
| 175 | + @Test |
| 176 | + public void testNonDefaultWithLongArray() throws Exception { |
| 177 | + LongArray5515Pojo input = new LongArray5515Pojo( |
| 178 | + 0L, 1L, 0L, 2L |
| 179 | + ); |
| 180 | + |
| 181 | + // EXPECT default '0L' to be filtered out — FAILS today |
| 182 | + assertEquals( |
| 183 | + a2q("{'values':[1,2]}"), |
| 184 | + MAPPER.writeValueAsString(input) |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + @JacksonTestFailureExpected |
| 189 | + @Test |
| 190 | + public void testNonDefaultWithDoubleArray() throws Exception { |
| 191 | + DoubleArray5515Pojo input = new DoubleArray5515Pojo( |
| 192 | + 0.0, 1.5, 0.0 |
| 193 | + ); |
| 194 | + |
| 195 | + // EXPECT default '0.0' to be filtered out — FAILS today |
| 196 | + assertEquals( |
| 197 | + a2q("{'values':[1.5]}"), |
| 198 | + MAPPER.writeValueAsString(input) |
| 199 | + ); |
| 200 | + } |
| 201 | +} |
0 commit comments