Skip to content

Commit b7820c3

Browse files
committed
Merge branch '2.15' into 2.16
2 parents e6a05b2 + 5895931 commit b7820c3

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-9
lines changed

release-notes/VERSION-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ Project: jackson-databind
132132
does not work
133133
(reported by @jonasho)
134134
(fix contributed by Joo-Hyuk K)
135+
#4303: `ObjectReader` is not serializable if it's configured for polymorphism
136+
(reported by @asardaes)
137+
(fix contributed by Joo-Hyuk K)
138+
135139

136140
2.15.3 (12-Oct-2023)
137141

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
*/
1818
public class ClassNameIdResolver
1919
extends TypeIdResolverBase
20+
implements java.io.Serializable // @since 2.17
2021
{
22+
private static final long serialVersionUID = 1L;
23+
2124
private final static String JAVA_UTIL_PKG = "java.util.";
2225

2326
protected final PolymorphicTypeValidator _subTypeValidator;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
* defaults to fully-qualified {@link Class} names (obtained with {@link Class#getName()}
1919
*/
2020
public class TypeNameIdResolver extends TypeIdResolverBase
21+
implements java.io.Serializable // @since 2.17
2122
{
23+
private static final long serialVersionUID = 1L;
24+
2225
protected final MapperConfig<?> _config;
2326

2427
/**

src/test/java/com/fasterxml/jackson/databind/TestJDKSerialization.java

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.fasterxml.jackson.databind;
22

3-
import java.io.*;
43
import java.util.*;
54

6-
import com.fasterxml.jackson.annotation.JsonAnyGetter;
7-
import com.fasterxml.jackson.annotation.JsonAnySetter;
8-
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
5+
import com.fasterxml.jackson.annotation.*;
96

107
import com.fasterxml.jackson.databind.type.TypeFactory;
118

@@ -59,6 +56,37 @@ public Map<String,Object> properties() {
5956
}
6057
}
6158

59+
60+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
61+
@JsonSubTypes({@JsonSubTypes.Type(value = FooClassImpl.class)})
62+
public class FooClass { }
63+
class FooClassImpl extends FooClass { }
64+
65+
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
66+
@JsonSubTypes({@JsonSubTypes.Type(value = FooDeductionImpl.class)})
67+
public class FooDeduction { }
68+
class FooDeductionImpl extends FooDeduction { }
69+
70+
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
71+
@JsonSubTypes({@JsonSubTypes.Type(value = FooNoneImpl.class)})
72+
public class FooNone { }
73+
class FooNoneImpl extends FooNone { }
74+
75+
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM)
76+
@JsonSubTypes({@JsonSubTypes.Type(value = FooCustomImpl.class)})
77+
public class FooCustom { }
78+
class FooCustomImpl extends FooCustom { }
79+
80+
@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS)
81+
@JsonSubTypes({@JsonSubTypes.Type(value = FooMinimalClassImpl.class)})
82+
public class FooMinimalClass { }
83+
class FooMinimalClassImpl extends FooMinimalClass { }
84+
85+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
86+
@JsonSubTypes({@JsonSubTypes.Type(value = FooNameImpl.class)})
87+
public class FooName { }
88+
class FooNameImpl extends FooName { }
89+
6290
/*
6391
/**********************************************************
6492
/* Tests for individual objects
@@ -71,7 +99,7 @@ public Map<String,Object> properties() {
7199
*/
72100
private final ObjectMapper MAPPER = newJsonMapper();
73101

74-
public void testConfigs() throws IOException
102+
public void testConfigs() throws Exception
75103
{
76104
byte[] base = jdkSerialize(MAPPER.getDeserializationConfig().getBaseSettings());
77105
assertNotNull(jdkDeserialize(base));
@@ -92,7 +120,7 @@ public void testConfigs() throws IOException
92120
}
93121

94122
// for [databind#899]
95-
public void testEnumHandlers() throws IOException
123+
public void testEnumHandlers() throws Exception
96124
{
97125
ObjectMapper mapper = newJsonMapper();
98126
// ensure we have serializers and/or deserializers, first
@@ -125,7 +153,7 @@ public void testEnumHandlers() throws IOException
125153
assertNotNull(result2);
126154
}
127155

128-
public void testObjectWriter() throws IOException
156+
public void testObjectWriter() throws Exception
129157
{
130158
ObjectWriter origWriter = MAPPER.writer();
131159
final String EXP_JSON = "{\"x\":2,\"y\":3}";
@@ -139,7 +167,7 @@ public void testObjectWriter() throws IOException
139167
assertEquals(EXP_JSON, writer2.writeValueAsString(p));
140168
}
141169

142-
public void testObjectReader() throws IOException
170+
public void testObjectReader() throws Exception
143171
{
144172
ObjectReader origReader = MAPPER.readerFor(MyPojo.class);
145173
String JSON = "{\"x\":1,\"y\":2}";
@@ -159,7 +187,7 @@ public void testObjectReader() throws IOException
159187
assertEquals(Integer.valueOf(2), any2.properties().get("y"));
160188
}
161189

162-
public void testObjectMapper() throws IOException
190+
public void testObjectMapper() throws Exception
163191
{
164192
final String EXP_JSON = "{\"x\":2,\"y\":3}";
165193
final MyPojo p = new MyPojo(2, 3);
@@ -191,4 +219,29 @@ public void testTypeFactory() throws Exception
191219
t = orig.constructType(JavaType.class);
192220
assertEquals(JavaType.class, t.getRawClass());
193221
}
222+
223+
// [databind#4303]
224+
public void testObjectReaderSerializationWithPolymorphism()
225+
throws Exception
226+
{
227+
Class<?>[] classes = new Class<?>[] {
228+
FooClass.class,
229+
FooDeduction.class,
230+
FooNone.class,
231+
FooCustom.class,
232+
FooMinimalClass.class,
233+
FooName.class
234+
};
235+
236+
for (Class<?> clazz : classes) {
237+
// Should be enough to ask for reader for polymorphic type
238+
// (no need to actually serialize/deserialize)
239+
ObjectReader reader = newJsonMapper()
240+
.readerFor(clazz);
241+
242+
byte[] bytes = jdkSerialize(reader);
243+
ObjectReader result = jdkDeserialize(bytes);
244+
assertNotNull(result);
245+
}
246+
}
194247
}

0 commit comments

Comments
 (0)