Skip to content

Commit 468eba8

Browse files
committed
Fix #727
1 parent a06883b commit 468eba8

File tree

4 files changed

+50
-20
lines changed

4 files changed

+50
-20
lines changed

release-notes/VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Project: jackson-databind
1313
#700: Cannot Change Default Abstract Type Mapper from LinkedHashMap
1414
(reported by wealdtech@github)
1515
#725: Auto-detect multi-argument constructor with implicit names if it is the only visible creator
16+
#727: Improve `ObjectWriter.forType()` to avoid forcing base type for container types
1617
- Remove old cglib compatibility tests; cause problems in Eclipse
1718

1819
2.5.2 (not yet released)

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,16 @@ public ObjectWriter forType(JavaType rootType)
449449
pf = Prefetch.empty;
450450
} else {
451451
// 15-Mar-2013, tatu: Important! Indicate that static typing is needed:
452-
rootType = rootType.withStaticTyping();
452+
/* 19-Mar-2015, tatu: Except when dealing with Collection, Map types, where
453+
* this does more harm than help.
454+
*/
455+
if (!rootType.isContainerType()) {
456+
rootType = rootType.withStaticTyping();
457+
}
453458
pf = _prefetchRootSerializer(_config, rootType);
454459
}
455460
return (pf == _prefetch) ? this : _new(_generatorSettings, pf);
456-
}
461+
}
457462

458463
/**
459464
* Method that will construct a new instance that uses specific type

src/test/java/com/fasterxml/jackson/databind/ser/TestGenericTypes.java

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22

33
import java.util.*;
44

5-
5+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
6+
import com.fasterxml.jackson.core.type.TypeReference;
67
import com.fasterxml.jackson.databind.BaseMapTest;
78
import com.fasterxml.jackson.databind.ObjectMapper;
89

910
public class TestGenericTypes extends BaseMapTest
1011
{
11-
/*
12-
/**********************************************************
13-
/* Helper types
14-
/**********************************************************
15-
*/
16-
1712
static class Account {
1813
private Long id;
1914
private String name;
@@ -90,25 +85,41 @@ class Element {
9085
public Element(T v) { value = v; }
9186
}
9287
}
88+
89+
// For [databind#728]
90+
static class Base727 {
91+
public int a;
92+
}
9393

94+
@JsonPropertyOrder(alphabetic=true)
95+
static class Impl727 extends Base727 {
96+
public int b;
97+
98+
public Impl727(int a, int b) {
99+
this.a = a;
100+
this.b = b;
101+
}
102+
}
94103
/*
95104
/**********************************************************
96105
/* Unit tests
97106
/**********************************************************
98107
*/
99108

109+
// final ObjectMapper MAPPER = new ObjectMapper();
110+
final ObjectMapper MAPPER = objectMapper();
111+
100112
@SuppressWarnings("unchecked")
101113
public void testIssue468a() throws Exception
102114
{
103115
Person1 p1 = new Person1("John");
104116
p1.setAccount(new Key<Account>(new Account("something", 42L)));
105117

106118
// First: ensure we can serialize (pre 1.7 this failed)
107-
ObjectMapper mapper = new ObjectMapper();
108-
String json = mapper.writeValueAsString(p1);
119+
String json = MAPPER.writeValueAsString(p1);
109120

110121
// and then verify that results make sense
111-
Map<String,Object> map = mapper.readValue(json, Map.class);
122+
Map<String,Object> map = MAPPER.readValue(json, Map.class);
112123
assertEquals("John", map.get("name"));
113124
Object ob = map.get("account");
114125
assertNotNull(ob);
@@ -131,11 +142,10 @@ public void testIssue468b() throws Exception
131142
p2.setAccounts(accounts);
132143

133144
// serialize without error:
134-
ObjectMapper mapper = new ObjectMapper();
135-
String json = mapper.writeValueAsString(p2);
145+
String json = MAPPER.writeValueAsString(p2);
136146

137147
// then verify output
138-
Map<String,Object> map = mapper.readValue(json, Map.class);
148+
Map<String,Object> map = MAPPER.readValue(json, Map.class);
139149
assertEquals("John", map.get("name"));
140150
Object ob = map.get("accounts");
141151
assertNotNull(ob);
@@ -145,14 +155,28 @@ public void testIssue468b() throws Exception
145155
}
146156

147157
/**
148-
* Issue [JACKSON-572] is about unbound type variables, usually resulting
158+
* Test related to unbound type variables, usually resulting
149159
* from inner classes of generic classes (like Sets).
150160
*/
151-
public void testUnboundIssue572() throws Exception
161+
public void testUnboundTypes() throws Exception
152162
{
153163
GenericBogusWrapper<Integer> list = new GenericBogusWrapper<Integer>(Integer.valueOf(7));
154-
String json = new ObjectMapper().writeValueAsString(list);
164+
String json = MAPPER.writeValueAsString(list);
155165
assertEquals("{\"wrapped\":{\"value\":7}}", json);
156166
}
157-
}
158167

168+
public void testRootTypeForCollections727() throws Exception
169+
{
170+
List<Base727> input = new ArrayList<Base727>();
171+
input.add(new Impl727(1, 2));
172+
173+
final String EXP = aposToQuotes("[{'a':1,'b':2}]");
174+
// Without type enforcement, produces expected output:
175+
assertEquals(EXP, MAPPER.writeValueAsString(input));
176+
assertEquals(EXP, MAPPER.writer().writeValueAsString(input));
177+
178+
// but enforcing type will hinder:
179+
TypeReference<?> typeRef = new TypeReference<List<Base727>>() { };
180+
assertEquals(EXP, MAPPER.writer().forType(typeRef).writeValueAsString(input));
181+
}
182+
}

src/test/java/com/fasterxml/jackson/failing/TestPolymorphicDeserialization.java renamed to src/test/java/com/fasterxml/jackson/failing/TestPolymorphicDeserialization283.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* Whether this is wrong, and if so, can we fix it, is unknown at this point
1818
* (2.3): quite possibly this can not be changed.
1919
*/
20-
public class TestPolymorphicDeserialization extends BaseMapTest
20+
public class TestPolymorphicDeserialization283 extends BaseMapTest
2121
{
2222
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = ClassA.class)
2323
@JsonSubTypes({

0 commit comments

Comments
 (0)