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