Skip to content

Commit b254aff

Browse files
committed
Fix #1203
1 parent f1f4023 commit b254aff

File tree

6 files changed

+61
-11
lines changed

6 files changed

+61
-11
lines changed

release-notes/VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Project: jackson-databind
1616
#1195: `JsonMappingException` not Serializable due to 2.7 reference to source (parser)
1717
(reported by mjustin@github)
1818
#1198: Problem with `@JsonTypeInfo.As.EXTERNAL_PROPERTY`, `defaultImpl`, missing type id, NPE
19+
#1203: `@JsonTypeInfo` does not work correctly for ReferenceTypes like `AtomicReference`
1920
- Improve handling of custom content (de)serializers for `AtomicReference`
2021

2122
2.7.3 (16-Mar-2016)

src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,7 @@ protected JavaType resolveType(DeserializationContext ctxt,
18691869
}
18701870
}
18711871

1872-
if (type.getContentType() != null) { // container type or reference type
1872+
if (type.isContainerType() || type.isReferenceType()) {
18731873
Object cdDef = intr.findContentDeserializer(member);
18741874
JsonDeserializer<?> cd = ctxt.deserializerInstance(member, cdDef);
18751875
if (cd != null) {

src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,10 @@ public TypeResolverBuilder<?> findPropertyTypeResolver(MapperConfig<?> config,
467467
/* As per definition of @JsonTypeInfo, should only apply to contents of container
468468
* (collection, map) types, not container types themselves:
469469
*/
470-
if (baseType.isContainerType()) return null;
470+
// 17-Apr-2016, tatu: For 2.7.4 make sure ReferenceType also included
471+
if (baseType.isContainerType() || baseType.isReferenceType()) {
472+
return null;
473+
}
471474
// No per-member type overrides (yet)
472475
return _findTypeResolver(config, am, baseType);
473476
}
@@ -1170,11 +1173,11 @@ protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config,
11701173
JsonTypeIdResolver idResInfo = _findAnnotation(ann, JsonTypeIdResolver.class);
11711174
TypeIdResolver idRes = (idResInfo == null) ? null
11721175
: config.typeIdResolverInstance(ann, idResInfo.value());
1173-
if (idRes != null) { // [JACKSON-359]
1176+
if (idRes != null) {
11741177
idRes.init(baseType);
11751178
}
11761179
b = b.init(info.use(), idRes);
1177-
/* 13-Aug-2011, tatu: One complication wrt [JACKSON-453]; external id
1180+
/* 13-Aug-2011, tatu: One complication; external id
11781181
* only works for properties; so if declared for a Class, we will need
11791182
* to map it to "PROPERTY" instead of "EXTERNAL_PROPERTY"
11801183
*/
@@ -1186,7 +1189,7 @@ protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config,
11861189
b = b.typeProperty(info.property());
11871190
Class<?> defaultImpl = info.defaultImpl();
11881191

1189-
// 08-Dec-2014, tatu: To deprecated `JsonTypeInfo.None` we need to use other placeholder(s);
1192+
// 08-Dec-2014, tatu: To deprecate `JsonTypeInfo.None` we need to use other placeholder(s);
11901193
// and since `java.util.Void` has other purpose (to indicate "deser as null"), we'll instead
11911194
// use `JsonTypeInfo.class` itself. But any annotation type will actually do, as they have no
11921195
// valid use (can not instantiate as default)

src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerFactory.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -787,9 +787,8 @@ protected BeanPropertyWriter _constructWriter(SerializerProvider prov,
787787
// Does member specify a serializer? If so, let's use it.
788788
JsonSerializer<?> annotatedSerializer = findSerializerFromAnnotation(prov,
789789
accessor);
790-
/* 02-Feb-2012, tatu: Unlike most other code paths, serializer produced
791-
* here will NOT be resolved or contextualized, unless done here, so:
792-
*/
790+
// Unlike most other code paths, serializer produced
791+
// here will NOT be resolved or contextualized, unless done here, so:
793792
if (annotatedSerializer instanceof ResolvableSerializer) {
794793
((ResolvableSerializer) annotatedSerializer).resolve(prov);
795794
}
@@ -798,8 +797,7 @@ protected BeanPropertyWriter _constructWriter(SerializerProvider prov,
798797
// And how about polymorphic typing? First special to cover JAXB per-field settings:
799798
TypeSerializer contentTypeSer = null;
800799
// 16-Feb-2014, cgc: contentType serializers for collection-like and map-like types
801-
if (ClassUtil.isCollectionMapOrArray(type.getRawClass())
802-
|| type.isCollectionLikeType() || type.isMapLikeType()) {
800+
if (type.isContainerType() || type.isReferenceType()) {
803801
contentTypeSer = findPropertyContentTypeSerializer(type, prov.getConfig(), accessor);
804802
}
805803
// and if not JAXB collection/array with annotations, maybe regular type info?
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fasterxml.jackson.databind.jsontype;
2+
3+
import java.util.concurrent.atomic.AtomicReference;
4+
5+
import com.fasterxml.jackson.annotation.JsonSubTypes;
6+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
7+
import com.fasterxml.jackson.databind.*;
8+
9+
public class PolymorphicViaRefTypeTest extends BaseMapTest
10+
{
11+
12+
@JsonSubTypes({
13+
@JsonSubTypes.Type(name = "impl5", value = ImplForAtomic.class)
14+
})
15+
static class BaseForAtomic {
16+
}
17+
18+
static class ImplForAtomic extends BaseForAtomic {
19+
public int x;
20+
21+
protected ImplForAtomic() { }
22+
public ImplForAtomic(int x) { this.x = x; }
23+
}
24+
25+
static class TypeInfoAtomic {
26+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "$type")
27+
public AtomicReference<BaseForAtomic> value;
28+
}
29+
30+
/*
31+
/**********************************************************************
32+
/* Test methods
33+
/**********************************************************************
34+
*/
35+
36+
private final ObjectMapper MAPPER = objectMapper();
37+
38+
public void testOptionalWithAtomic() throws Exception
39+
{
40+
TypeInfoAtomic data = new TypeInfoAtomic();
41+
data.value = new AtomicReference<BaseForAtomic>(new ImplForAtomic(42));
42+
String json = MAPPER.writeValueAsString(data);
43+
System.err.println("JSON = "+json);
44+
TypeInfoAtomic result = MAPPER.readValue(json, TypeInfoAtomic.class);
45+
assertNotNull(result);
46+
}
47+
48+
}

src/test/java/com/fasterxml/jackson/databind/jsontype/TestAbstractContainers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.fasterxml.jackson.databind.*;
77

88
/**
9-
* For [Issue#292]
9+
* For [databind#292]
1010
*/
1111
@SuppressWarnings("serial")
1212
public class TestAbstractContainers extends BaseMapTest

0 commit comments

Comments
 (0)