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

Fix OptionalSerializer.isEmpty() from an incorrect class cast exception. #34

Merged
merged 1 commit into from
Apr 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public boolean isEmpty(SerializerProvider provider, Optional<?> value)
JsonSerializer<Object> ser = _valueSerializer;
if (ser == null) {
try {
ser = _findCachedSerializer(provider, value.getClass());
ser = _findCachedSerializer(provider, contents.getClass());
} catch (JsonMappingException e) { // nasty but necessary
throw new RuntimeJsonMappingException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ public OptionalNonEmptyStringBean() { }
}
}

public static final class OptionalGenericData<T> {
public Optional<T> myData;
public static <T> OptionalGenericData<T> construct(T data) {
OptionalGenericData<T> ret = new OptionalGenericData<T>();
ret.myData = Optional.of(data);
return ret;
}
}

private final ObjectMapper MAPPER = mapperWithModule();

public void testSerOptNonEmpty() throws Exception
Expand Down Expand Up @@ -58,4 +67,40 @@ public void testExcludeEmptyStringViaOptional() throws Exception
json = MAPPER.writeValueAsString(new OptionalNonEmptyStringBean(""));
assertEquals("{}", json);
}

public void testSerPropInclusionAlways() throws Exception
{
JsonInclude.Value incl =
JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.ALWAYS);
ObjectMapper mapper = mapperWithModule().setPropertyInclusion(incl);
assertEquals("{\"myData\":true}",
mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE)));
}

public void testSerPropInclusionNonNull() throws Exception
{
JsonInclude.Value incl =
JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.NON_NULL);
ObjectMapper mapper = mapperWithModule().setPropertyInclusion(incl);
assertEquals("{\"myData\":true}",
mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE)));
}

public void testSerPropInclusionNonAbsent() throws Exception
{
JsonInclude.Value incl =
JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.NON_ABSENT);
ObjectMapper mapper = mapperWithModule().setPropertyInclusion(incl);
assertEquals("{\"myData\":true}",
mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE)));
}

public void testSerPropInclusionNonEmpty() throws Exception
{
JsonInclude.Value incl =
JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.NON_EMPTY);
ObjectMapper mapper = mapperWithModule().setPropertyInclusion(incl);
assertEquals("{\"myData\":true}",
mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE)));
}
}