Skip to content

Commit 82c346a

Browse files
committed
Fix #1395
1 parent be114a9 commit 82c346a

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project: jackson-databind
1313
(reported by TomMarkuske@github)
1414
#1389 Problem with handling of multi-argument creator with Enums
1515
(fix contributed by Pavel P)
16+
#1395: Problems deserializing primitive `long` field while using `TypeResolverBuilder`
17+
(reported by UghZan3@github)
1618

1719
2.8.3 (17-Sep-2016)
1820

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ public class ObjectMapper
133133
* Since 2.4 there are special exceptions for JSON Tree model
134134
* types (sub-types of {@link TreeNode}: default typing is never
135135
* applied to them
136-
* (see <a href="https://github.com/FasterXML/jackson-databind/issues/88">Issue#88</a> for details)
136+
* (see <a href="https://github.com/FasterXML/jackson-databind/issues/88">databind#88</a> for details)
137+
*<p>
138+
* Since 2.8(.4) additional checks are made to avoid attempts at default
139+
* typing primitive-valued properties.
137140
*/
138141
public enum DefaultTyping {
139142
/**
@@ -224,6 +227,12 @@ public TypeSerializer buildTypeSerializer(SerializationConfig config,
224227
*/
225228
public boolean useForType(JavaType t)
226229
{
230+
// 03-Oct-2016, tatu: As per [databind#1395], need to skip
231+
// primitive types too, regardless
232+
if (t.isPrimitive()) {
233+
return false;
234+
}
235+
227236
switch (_appliesFor) {
228237
case NON_CONCRETE_AND_ARRAYS:
229238
while (t.isArrayType()) {

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/StdTypeResolverBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public TypeSerializer buildTypeSerializer(SerializationConfig config,
6969
JavaType baseType, Collection<NamedType> subtypes)
7070
{
7171
if (_idType == JsonTypeInfo.Id.NONE) { return null; }
72+
// 03-Oct-2016, tatu: As per [databind#1395] better prevent use for primitives,
73+
// regardless of setting
74+
if (baseType.isPrimitive()) {
75+
return null;
76+
}
7277
TypeIdResolver idRes = idResolver(config, baseType, subtypes, true, false);
7378
switch (_includeAs) {
7479
case WRAPPER_ARRAY:
@@ -97,6 +102,11 @@ public TypeDeserializer buildTypeDeserializer(DeserializationConfig config,
97102
JavaType baseType, Collection<NamedType> subtypes)
98103
{
99104
if (_idType == JsonTypeInfo.Id.NONE) { return null; }
105+
// 03-Oct-2016, tatu: As per [databind#1395] better prevent use for primitives,
106+
// regardless of setting
107+
if (baseType.isPrimitive()) {
108+
return null;
109+
}
100110

101111
TypeIdResolver idRes = idResolver(config, baseType, subtypes, false, true);
102112

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fasterxml.jackson.databind.jsontype;
2+
3+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
4+
5+
import com.fasterxml.jackson.databind.*;
6+
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;
7+
8+
import java.util.*;
9+
10+
// [databind#1395]: prevent attempts at including type info for primitives
11+
public class DefaultTypingWithPrimitivesTest extends BaseMapTest
12+
{
13+
static class Data {
14+
public long key;
15+
}
16+
17+
public void testDefaultTypingWithLong() throws Exception
18+
{
19+
Data data = new Data();
20+
data.key = 1L;
21+
Map<String, Object> mapData = new HashMap<String, Object>();
22+
mapData.put("longInMap", 2L);
23+
mapData.put("longAsField", data);
24+
25+
// Configure Jackson to preserve types
26+
ObjectMapper mapper = new ObjectMapper();
27+
StdTypeResolverBuilder resolver = new StdTypeResolverBuilder();
28+
resolver.init(JsonTypeInfo.Id.CLASS, null);
29+
resolver.inclusion(JsonTypeInfo.As.PROPERTY);
30+
resolver.typeProperty("__t");
31+
mapper.setDefaultTyping(resolver);
32+
mapper.enable(SerializationFeature.INDENT_OUTPUT);
33+
34+
// Serialize
35+
String json = mapper.writeValueAsString(mapData);
36+
37+
// Deserialize
38+
Map<?,?> result = mapper.readValue(json, Map.class);
39+
assertNotNull(result);
40+
assertEquals(2, result.size());
41+
}
42+
}

0 commit comments

Comments
 (0)