|
1 | 1 | package com.fasterxml.jackson.databind.ser;
|
2 | 2 |
|
3 |
| -import java.io.IOException; |
4 |
| -import java.util.*; |
5 |
| - |
6 |
| -import com.fasterxml.jackson.annotation.*; |
| 3 | +import com.fasterxml.jackson.annotation.JsonAnyGetter; |
| 4 | +import com.fasterxml.jackson.annotation.JsonFilter; |
| 5 | +import com.fasterxml.jackson.annotation.JsonInclude; |
7 | 6 | import com.fasterxml.jackson.core.JsonGenerator;
|
8 |
| -import com.fasterxml.jackson.databind.*; |
| 7 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 10 | +import com.fasterxml.jackson.databind.SerializerProvider; |
9 | 11 | import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
| 12 | +import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; |
| 13 | +import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; |
10 | 14 | import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
|
11 | 15 | import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
12 | 16 |
|
| 17 | +import java.io.IOException; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
13 | 22 | public class AnyGetterTest extends BaseMapTest
|
14 | 23 | {
|
15 | 24 | static class Bean
|
@@ -131,6 +140,37 @@ public void serialize(String value, JsonGenerator gen,
|
131 | 140 | }
|
132 | 141 | }
|
133 | 142 |
|
| 143 | + static class Bean2592NoAnnotations |
| 144 | + { |
| 145 | + protected Map<String, String> properties = new HashMap<>(); |
| 146 | + |
| 147 | + @JsonAnyGetter |
| 148 | + public Map<String, String> getProperties() { |
| 149 | + return properties; |
| 150 | + } |
| 151 | + |
| 152 | + public void setProperties(Map<String, String> properties) { |
| 153 | + this.properties = properties; |
| 154 | + } |
| 155 | + |
| 156 | + public void add(String key, String value) { |
| 157 | + properties.put(key, value); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + static class Bean2592PropertyIncludeNonEmpty extends Bean2592NoAnnotations |
| 162 | + { |
| 163 | + @JsonInclude(content = JsonInclude.Include.NON_EMPTY) |
| 164 | + @JsonAnyGetter |
| 165 | + @Override |
| 166 | + public Map<String, String> getProperties() { |
| 167 | + return properties; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @JsonFilter("Bean2592") |
| 172 | + static class Bean2592WithFilter extends Bean2592NoAnnotations {} |
| 173 | + |
134 | 174 | /*
|
135 | 175 | /**********************************************************
|
136 | 176 | /* Test methods
|
@@ -196,4 +236,62 @@ public void testAnyGetterWithValueSerializer() throws Exception
|
196 | 236 | String json = mapper.writeValueAsString(input);
|
197 | 237 | assertEquals("{\"key\":\"VALUE\"}", json);
|
198 | 238 | }
|
| 239 | + |
| 240 | + // [databind#2592] |
| 241 | + public void testAnyGetterWithMapperDefaultIncludeNonEmpty() throws Exception |
| 242 | + { |
| 243 | + ObjectMapper mapper = new ObjectMapper() |
| 244 | + .setSerializationInclusion(JsonInclude.Include.NON_EMPTY); |
| 245 | + Bean2592NoAnnotations input = new Bean2592NoAnnotations(); |
| 246 | + input.add("non-empty", "property"); |
| 247 | + input.add("empty", ""); |
| 248 | + input.add("null", null); |
| 249 | + String json = mapper.writeValueAsString(input); |
| 250 | + assertEquals("{\"non-empty\":\"property\"}", json); |
| 251 | + } |
| 252 | + |
| 253 | + // [databind#2592] |
| 254 | + public void testAnyGetterWithMapperDefaultIncludeNonEmptyAndFilterOnBean() throws Exception |
| 255 | + { |
| 256 | + FilterProvider filters = new SimpleFilterProvider() |
| 257 | + .addFilter("Bean2592", SimpleBeanPropertyFilter.serializeAllExcept("something")); |
| 258 | + ObjectMapper mapper = new ObjectMapper() |
| 259 | + .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) |
| 260 | + .setFilterProvider(filters); |
| 261 | + Bean2592WithFilter input = new Bean2592WithFilter(); |
| 262 | + input.add("non-empty", "property"); |
| 263 | + input.add("empty", ""); |
| 264 | + input.add("null", null); |
| 265 | + String json = mapper.writeValueAsString(input); |
| 266 | + // Unfortunately path for bean with filter is different. It still skips nulls. |
| 267 | + assertEquals("{\"non-empty\":\"property\",\"empty\":\"\"}", json); |
| 268 | + } |
| 269 | + |
| 270 | + // [databind#2592] |
| 271 | + public void testAnyGetterWithPropertyIncludeNonEmpty() throws Exception |
| 272 | + { |
| 273 | + ObjectMapper mapper = new ObjectMapper(); |
| 274 | + Bean2592PropertyIncludeNonEmpty input = new Bean2592PropertyIncludeNonEmpty(); |
| 275 | + input.add("non-empty", "property"); |
| 276 | + input.add("empty", ""); |
| 277 | + input.add("null", null); |
| 278 | + String json = mapper.writeValueAsString(input); |
| 279 | + assertEquals("{\"non-empty\":\"property\"}", json); |
| 280 | + } |
| 281 | + |
| 282 | + // [databind#2592] |
| 283 | + public void testAnyGetterConfigIncludeNonEmpty() throws Exception |
| 284 | + { |
| 285 | + ObjectMapper mapper = new ObjectMapper(); |
| 286 | + mapper.configOverride(Map.class).setInclude(JsonInclude.Value.construct( |
| 287 | + JsonInclude.Include.USE_DEFAULTS, |
| 288 | + JsonInclude.Include.NON_EMPTY |
| 289 | + )); |
| 290 | + Bean2592NoAnnotations input = new Bean2592NoAnnotations(); |
| 291 | + input.add("non-empty", "property"); |
| 292 | + input.add("empty", ""); |
| 293 | + input.add("null", null); |
| 294 | + String json = mapper.writeValueAsString(input); |
| 295 | + assertEquals("{\"non-empty\":\"property\"}", json); |
| 296 | + } |
199 | 297 | }
|
0 commit comments