Skip to content

Commit 4091272

Browse files
committed
Add a test for Map-element/Absent combo
1 parent 5f3de07 commit 4091272

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalnclusionTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fasterxml.jackson.datatype.jdk8;
22

3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
35
import java.util.Optional;
46

57
import com.fasterxml.jackson.annotation.JsonAutoDetect;
@@ -38,6 +40,15 @@ public static <T> OptionalGenericData<T> construct(T data) {
3840
}
3941
}
4042

43+
static final class OptMapBean {
44+
public Map<String, Optional<?>> values;
45+
46+
public OptMapBean(String key, Optional<?> v) {
47+
values = new LinkedHashMap<>();
48+
values.put(key, v);
49+
}
50+
}
51+
4152
/*
4253
/**********************************************************
4354
/* Test methods
@@ -118,4 +129,19 @@ public void testSerPropInclusionNonEmpty() throws Exception
118129
assertEquals("{\"myData\":true}",
119130
mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE)));
120131
}
132+
133+
public void testMapElementInclusion() throws Exception
134+
{
135+
ObjectMapper mapper = mapperWithModule().setDefaultPropertyInclusion(
136+
JsonInclude.Value.construct(JsonInclude.Include.NON_NULL, JsonInclude.Include.NON_ABSENT));
137+
// first: Absent entry/-ies should NOT be included
138+
assertEquals("{\"values\":{}}",
139+
mapper.writeValueAsString(new OptMapBean("key", Optional.empty())));
140+
// but non-empty should
141+
assertEquals("{\"values\":{\"key\":\"value\"}}",
142+
mapper.writeValueAsString(new OptMapBean("key", Optional.of("value"))));
143+
// and actually even empty
144+
assertEquals("{\"values\":{\"key\":\"\"}}",
145+
mapper.writeValueAsString(new OptMapBean("key", Optional.of(""))));
146+
}
121147
}

0 commit comments

Comments
 (0)