Skip to content

Commit 1d94c72

Browse files
committed
Merge branch '2.9'
2 parents cd9720f + 57f5e70 commit 1d94c72

File tree

4 files changed

+51
-65
lines changed

4 files changed

+51
-65
lines changed

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

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/test/java/com/fasterxml/jackson/databind/jsontype/deftyping/TestDefaultForEnums.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ protected static class TimeUnitBean {
2525
}
2626

2727
/*
28-
/**********************************************************
28+
/**********************************************************************
2929
/* Test methods
30-
/**********************************************************
30+
/**********************************************************************
3131
*/
3232

33+
private final ObjectMapper DEFTYPING_MAPPER = ObjectMapper.builder()
34+
.enableDefaultTyping()
35+
.build();
36+
3337
public void testSimpleEnumBean() throws Exception
3438
{
3539
TimeUnitBean bean = new TimeUnitBean();
@@ -42,39 +46,29 @@ public void testSimpleEnumBean() throws Exception
4246
assertEquals(TimeUnit.SECONDS, result.timeUnit);
4347

4448
// then with type info
45-
m = ObjectMapper.builder()
46-
.enableDefaultTyping()
47-
.build();
48-
json = m.writeValueAsString(bean);
49-
result = m.readValue(json, TimeUnitBean.class);
49+
json = DEFTYPING_MAPPER.writeValueAsString(bean);
50+
result = DEFTYPING_MAPPER.readValue(json, TimeUnitBean.class);
5051

5152
assertEquals(TimeUnit.SECONDS, result.timeUnit);
5253
}
5354

5455
public void testSimpleEnumsInObjectArray() throws Exception
5556
{
56-
ObjectMapper mapper = ObjectMapper.builder()
57-
.enableDefaultTyping()
58-
.build();
59-
6057
// Typing is needed for enums
61-
String json = mapper.writeValueAsString(new Object[] { TestEnum.A });
62-
assertEquals("[[\"com.fasterxml.jackson.databind.jsontype.TestDefaultForEnums$TestEnum\",\"A\"]]", json);
58+
String json = DEFTYPING_MAPPER.writeValueAsString(new Object[] { TestEnum.A });
59+
assertEquals("[[\"com.fasterxml.jackson.databind.jsontype.deftyping.TestDefaultForEnums$TestEnum\",\"A\"]]", json);
6360

6461
// and let's verify we get it back ok as well:
65-
Object[] value = mapper.readValue(json, Object[].class);
62+
Object[] value = DEFTYPING_MAPPER.readValue(json, Object[].class);
6663
assertEquals(1, value.length);
6764
assertSame(TestEnum.A, value[0]);
6865
}
6966

7067
public void testSimpleEnumsAsField() throws Exception
7168
{
72-
ObjectMapper mapper = ObjectMapper.builder()
73-
.enableDefaultTyping()
74-
.build();
75-
String json = mapper.writeValueAsString(new EnumHolder(TestEnum.B));
76-
assertEquals("{\"value\":[\"com.fasterxml.jackson.databind.jsontype.TestDefaultForEnums$TestEnum\",\"B\"]}", json);
77-
EnumHolder holder = mapper.readValue(json, EnumHolder.class);
69+
String json = DEFTYPING_MAPPER.writeValueAsString(new EnumHolder(TestEnum.B));
70+
assertEquals("{\"value\":[\"com.fasterxml.jackson.databind.jsontype.deftyping.TestDefaultForEnums$TestEnum\",\"B\"]}", json);
71+
EnumHolder holder = DEFTYPING_MAPPER.readValue(json, EnumHolder.class);
7872
assertSame(TestEnum.B, holder.value);
7973
}
8074
}

src/test/java/com/fasterxml/jackson/databind/jsontype/deftyping/TestDefaultForObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public void testFeature432() throws Exception
344344
.enableDefaultTypingAsProperty(DefaultTyping.OBJECT_AND_NON_CONCRETE, "*CLASS*")
345345
.build();
346346
String json = mapper.writeValueAsString(new BeanHolder(new StringBean("punny")));
347-
assertEquals("{\"bean\":{\"*CLASS*\":\"com.fasterxml.jackson.databind.jsontype.TestDefaultForObject$StringBean\",\"name\":\"punny\"}}", json);
347+
assertEquals("{\"bean\":{\"*CLASS*\":\"com.fasterxml.jackson.databind.jsontype.deftyping.TestDefaultForObject$StringBean\",\"name\":\"punny\"}}", json);
348348
}
349349

350350
public void testNoGoWithExternalProperty() throws Exception

src/test/java/com/fasterxml/jackson/databind/jsontype/deftyping/TestDefaultForScalars.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.util.*;
44

5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
56
import com.fasterxml.jackson.databind.*;
7+
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;
68

79
/**
810
* Unit tests to verify that Java/JSON scalar values (non-structured values)
@@ -16,10 +18,15 @@ static class Jackson417Bean {
1618
public java.io.Serializable bar = new Integer(13);
1719
}
1820

21+
// [databind#1395]: prevent attempts at including type info for primitives
22+
static class Data {
23+
public long key;
24+
}
25+
1926
/*
20-
/**********************************************************
21-
/* Unit tests
22-
/**********************************************************
27+
/**********************************************************************
28+
/* Test methods
29+
/**********************************************************************
2330
*/
2431

2532
private final ObjectMapper DEFAULT_TYPING_MAPPER = ObjectMapper.builder()
@@ -95,4 +102,30 @@ public void test417() throws Exception
95102
assertEquals(input.foo, result.foo);
96103
assertEquals(input.bar, result.bar);
97104
}
105+
106+
// [databind#1395]: prevent attempts at including type info for primitives
107+
public void testDefaultTypingWithLong() throws Exception
108+
{
109+
Data data = new Data();
110+
data.key = 1L;
111+
Map<String, Object> mapData = new HashMap<String, Object>();
112+
mapData.put("longInMap", 2L);
113+
mapData.put("longAsField", data);
114+
115+
// Configure Jackson to preserve types
116+
StdTypeResolverBuilder resolver = new StdTypeResolverBuilder(JsonTypeInfo.Id.CLASS,
117+
JsonTypeInfo.As.PROPERTY, "__t");
118+
ObjectMapper mapper = ObjectMapper.builder()
119+
.enable(SerializationFeature.INDENT_OUTPUT)
120+
.setDefaultTyping(resolver)
121+
.build();
122+
123+
// Serialize
124+
String json = mapper.writeValueAsString(mapData);
125+
126+
// Deserialize
127+
Map<?,?> result = mapper.readValue(json, Map.class);
128+
assertNotNull(result);
129+
assertEquals(2, result.size());
130+
}
98131
}

0 commit comments

Comments
 (0)