Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit 71bed6b

Browse files
committed
Fix #82
1 parent 50751cf commit 71bed6b

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Project: jackson-datatype-guava
88

99
#80: Relax OSGi version constraints for Guava dependency.
1010
(requested by Benson M)
11+
#82: Problem with polymorphic value types for `Optional`, with 2.6
1112

1213
2.6.1 (09-Aug-2015)
1314

src/main/java/com/fasterxml/jackson/datatype/guava/ser/GuavaOptionalSerializer.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
9090
(provider.isEnabled(MapperFeature.USE_STATIC_TYPING)
9191
|| _referredType.isFinal())) {
9292
return withResolved(property,
93-
provider.findPrimaryPropertySerializer(_referredType, property),
93+
_findSerializer(provider, _referredType, _property),
9494
_unwrapper);
9595
}
9696
} else {
@@ -130,6 +130,7 @@ public boolean isEmpty(SerializerProvider prov, Optional<?> value) {
130130
return (value == null) || !value.isPresent();
131131
}
132132

133+
@Override
133134
public boolean isUnwrappingSerializer() {
134135
return (_unwrapper != null);
135136
}
@@ -212,12 +213,26 @@ protected final JsonSerializer<Object> _findSerializer(SerializerProvider provid
212213
{
213214
JsonSerializer<Object> ser = _dynamicSerializers.serializerFor(type);
214215
if (ser == null) {
215-
ser = provider.findPrimaryPropertySerializer(type, _property);
216+
ser = _findSerializer(provider, type, _property);
216217
if (_unwrapper != null) {
217218
ser = ser.unwrappingSerializer(_unwrapper);
218219
}
219220
_dynamicSerializers = _dynamicSerializers.newWith(type, ser);
220221
}
221222
return ser;
222223
}
224+
225+
private final JsonSerializer<Object> _findSerializer(SerializerProvider provider,
226+
Class<?> type, BeanProperty prop) throws JsonMappingException
227+
{
228+
// Important: ask for TYPED serializer, in case polymorphic handling is needed!
229+
return provider.findTypedValueSerializer(type, true, prop);
230+
}
231+
232+
private final JsonSerializer<Object> _findSerializer(SerializerProvider provider,
233+
JavaType type, BeanProperty prop) throws JsonMappingException
234+
{
235+
// Important: ask for TYPED serializer, in case polymorphic handling is needed!
236+
return provider.findTypedValueSerializer(type, true, prop);
237+
}
223238
}

src/test/java/com/fasterxml/jackson/datatype/guava/OptionalBasicTest.java

+32-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import com.fasterxml.jackson.annotation.*;
66
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
7+
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
78
import com.fasterxml.jackson.core.type.TypeReference;
8-
import com.fasterxml.jackson.databind.JavaType;
9-
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.databind.*;
10+
1011
import com.google.common.base.Optional;
1112

1213
public class OptionalBasicTest extends ModuleTestBase
@@ -37,6 +38,20 @@ public void link(Unit u) {
3738
}
3839
}
3940

41+
// To test handling of polymorphic value types
42+
43+
public static class Container {
44+
public Optional<Contained> contained;
45+
}
46+
47+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY)
48+
@JsonSubTypes({
49+
@JsonSubTypes.Type(name = "ContainedImpl", value = ContainedImpl.class),
50+
})
51+
public static interface Contained { }
52+
53+
public static class ContainedImpl implements Contained { }
54+
4055
/*
4156
/**********************************************************************
4257
/* Test methods
@@ -228,9 +243,23 @@ public void testOptionalCollection() throws Exception {
228243
}
229244
}
230245

231-
// [Issue#48]
246+
// [datatype-guava#48]
232247
public void testDeserNull() throws Exception {
233248
Optional<?> value = MAPPER.readValue("\"\"", new TypeReference<Optional<Integer>>() {});
234249
assertFalse(value.isPresent());
235250
}
251+
252+
// [datatype-guava#81]
253+
public void testPolymorphic() throws Exception
254+
{
255+
final Container dto = new Container();
256+
dto.contained = Optional.of((Contained) new ContainedImpl());
257+
258+
final String json = MAPPER.writeValueAsString(dto);
259+
260+
final Container fromJson = MAPPER.readValue(json, Container.class);
261+
assertNotNull(fromJson.contained);
262+
assertTrue(fromJson.contained.isPresent());
263+
assertSame(ContainedImpl.class, fromJson.contained.get().getClass());
264+
}
236265
}

0 commit comments

Comments
 (0)