Skip to content
This repository was archived by the owner on Nov 7, 2019. It is now read-only.

Commit 2763e2b

Browse files
committed
Merge pull request #34 from nbauernfeind/OptionalIsEmpty
Fix OptionalSerializer.isEmpty() from an incorrect class cast exception.
2 parents 8d7df15 + ec74aba commit 2763e2b

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/main/java/com/fasterxml/jackson/datatype/jdk8/OptionalSerializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public boolean isEmpty(SerializerProvider provider, Optional<?> value)
215215
JsonSerializer<Object> ser = _valueSerializer;
216216
if (ser == null) {
217217
try {
218-
ser = _findCachedSerializer(provider, value.getClass());
218+
ser = _findCachedSerializer(provider, contents.getClass());
219219
} catch (JsonMappingException e) { // nasty but necessary
220220
throw new RuntimeJsonMappingException(e);
221221
}

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

+45
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ public OptionalNonEmptyStringBean() { }
2929
}
3030
}
3131

32+
public static final class OptionalGenericData<T> {
33+
public Optional<T> myData;
34+
public static <T> OptionalGenericData<T> construct(T data) {
35+
OptionalGenericData<T> ret = new OptionalGenericData<T>();
36+
ret.myData = Optional.of(data);
37+
return ret;
38+
}
39+
}
40+
3241
private final ObjectMapper MAPPER = mapperWithModule();
3342

3443
public void testSerOptNonEmpty() throws Exception
@@ -58,4 +67,40 @@ public void testExcludeEmptyStringViaOptional() throws Exception
5867
json = MAPPER.writeValueAsString(new OptionalNonEmptyStringBean(""));
5968
assertEquals("{}", json);
6069
}
70+
71+
public void testSerPropInclusionAlways() throws Exception
72+
{
73+
JsonInclude.Value incl =
74+
JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.ALWAYS);
75+
ObjectMapper mapper = mapperWithModule().setPropertyInclusion(incl);
76+
assertEquals("{\"myData\":true}",
77+
mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE)));
78+
}
79+
80+
public void testSerPropInclusionNonNull() throws Exception
81+
{
82+
JsonInclude.Value incl =
83+
JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.NON_NULL);
84+
ObjectMapper mapper = mapperWithModule().setPropertyInclusion(incl);
85+
assertEquals("{\"myData\":true}",
86+
mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE)));
87+
}
88+
89+
public void testSerPropInclusionNonAbsent() throws Exception
90+
{
91+
JsonInclude.Value incl =
92+
JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.NON_ABSENT);
93+
ObjectMapper mapper = mapperWithModule().setPropertyInclusion(incl);
94+
assertEquals("{\"myData\":true}",
95+
mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE)));
96+
}
97+
98+
public void testSerPropInclusionNonEmpty() throws Exception
99+
{
100+
JsonInclude.Value incl =
101+
JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.NON_EMPTY);
102+
ObjectMapper mapper = mapperWithModule().setPropertyInclusion(incl);
103+
assertEquals("{\"myData\":true}",
104+
mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE)));
105+
}
61106
}

0 commit comments

Comments
 (0)