Skip to content

Commit 002a9a3

Browse files
committed
Fixed #1451
1 parent 1bef91c commit 002a9a3

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

release-notes/CREDITS

+3
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,6 @@ Josh Caplan (jecaplan@github)
485485
* Reported, suggested fix for #1368: Problem serializing `JsonMappingException` due to addition
486486
of non-ignored `processor` property (added in 2.7)
487487
(2.7.8)
488+
489+
Frédéric Camblor (fcamblor@github)
490+
* Reported #1451: Type parameter not passed by `ObjectWriter` if serializer pre-fetch disabled

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project: jackson-databind
1313
#1432: Off by 1 bug in PropertyValueBuffer
1414
(reported by Kevin D)
1515
#1439: NPE when using with filter id, serializing `java.util.Map` types
16+
#1451: Type parameter not passed by `ObjectWriter` if serializer pre-fetch disabled
17+
(reported by Frédéric C)
1618
#1456: `TypeFactory` type resolution broken in 2.7 for generic types
1719
when using `constructType` with context
1820
(reported by Dmitry S)

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ public Prefetch forRootType(ObjectWriter parent, JavaType newType) {
13991399
;
14001400
}
14011401
}
1402-
return new Prefetch(null, null, typeSerializer);
1402+
return new Prefetch(newType, null, typeSerializer);
14031403
}
14041404

14051405
public final JsonSerializer<Object> getValueSerializer() {
@@ -1419,13 +1419,13 @@ public void serialize(JsonGenerator gen, Object value, DefaultSerializerProvider
14191419
{
14201420
if (typeSerializer != null) {
14211421
prov.serializePolymorphic(gen, value, rootType, valueSerializer, typeSerializer);
1422-
return;
1423-
}
1424-
if (valueSerializer != null) {
1422+
} else if (valueSerializer != null) {
14251423
prov.serializeValue(gen, value, rootType, valueSerializer);
1426-
return;
1424+
} else if (rootType != null) {
1425+
prov.serializeValue(gen, value, rootType);
1426+
} else {
1427+
prov.serializeValue(gen, value);
14271428
}
1428-
prov.serializeValue(gen, value);
14291429
}
14301430
}
14311431
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fasterxml.jackson.databind.jsontype;
2+
3+
import java.util.*;
4+
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
import com.fasterxml.jackson.core.type.TypeReference;
7+
import com.fasterxml.jackson.databind.*;
8+
9+
public class PolymorphicList1451SerTest extends BaseMapTest
10+
{
11+
@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
12+
public static class A {
13+
public String a;
14+
}
15+
public static class B extends A {
16+
public String b;
17+
}
18+
19+
private final String CLASS_NAME = getClass().getSimpleName();
20+
21+
public void testCollectionWithTypeInfo() throws Exception {
22+
ObjectMapper mapper = new ObjectMapper()
23+
.disable(SerializationFeature.EAGER_SERIALIZER_FETCH)
24+
// .disable(DeserializationFeature.EAGER_DESERIALIZER_FETCH)
25+
;
26+
27+
List<A> input = new ArrayList<A>();
28+
A a = new A();
29+
a.a = "a1";
30+
input.add(a);
31+
32+
B b = new B();
33+
b.b = "b";
34+
b.a = "a2";
35+
input.add(b);
36+
37+
final TypeReference<?> typeRef =
38+
new TypeReference<Collection<A>>(){};
39+
ObjectWriter writer = mapper.writerFor(typeRef);
40+
41+
String result = writer.writeValueAsString(input);
42+
43+
assertEquals(aposToQuotes(
44+
"[{'@class':'."+CLASS_NAME+"$A','a':'a1'},{'@class':'."+CLASS_NAME+"$B','a':'a2','b':'b'}]"
45+
), result);
46+
47+
List<A> output = mapper.readerFor(typeRef)
48+
.readValue(result);
49+
assertEquals(2, output.size());
50+
assertEquals(A.class, output.get(0).getClass());
51+
assertEquals(B.class, output.get(1).getClass());
52+
}
53+
}

0 commit comments

Comments
 (0)