Skip to content

Commit a2456ff

Browse files
committed
Ensure JDK serializability of AvroMapper
1 parent ed6e93d commit a2456ff

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroAnnotationIntrospector.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@
3838
* the {@link #toString()} method. </li>
3939
* <li>{@link Union @Union} - Alias for <code>JsonSubTypes</code></li>
4040
* </ul>
41-
*
42-
* @since 2.9
4341
*/
4442
public class AvroAnnotationIntrospector extends AnnotationIntrospector
43+
implements java.io.Serializable
4544
{
4645
private static final long serialVersionUID = 1L;
4746

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroModule.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131
* of {@link java.lang.Object}.
3232
* </li>
3333
*</ul>
34-
*
35-
* @since 2.5
3634
*/
3735
public class AvroModule extends Module
36+
implements java.io.Serializable
3837
{
38+
private static final long serialVersionUID = 3L;
39+
3940
protected final static AvroAnnotationIntrospector INTR
4041
= new AvroAnnotationIntrospector();
4142

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.fasterxml.jackson.dataformat.avro;
2+
3+
import java.io.*;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
public class JDKSerializabilityTest extends AvroTestBase
8+
{
9+
public void testNativeMapperWithModule() throws Exception {
10+
_testModule(newMapper());
11+
}
12+
13+
public void testApacheMapperWithModule() throws Exception {
14+
_testModule(newApacheMapper());
15+
}
16+
17+
private void _testModule(ObjectMapper mapper) throws Exception
18+
{
19+
// very simple validation: should still work wrt serialization
20+
ObjectMapper unfrozenMapper = serializeAndDeserialize(mapper);
21+
22+
// and then simple verification that write+read still works
23+
24+
Employee empl = _simpleEmployee();
25+
byte[] avro = toAvro(empl, unfrozenMapper);
26+
Employee result = unfrozenMapper.readerFor(Employee.class)
27+
.with(getEmployeeSchema())
28+
.readValue(avro);
29+
assertEquals(empl.name, result.name);
30+
}
31+
32+
private ObjectMapper serializeAndDeserialize(ObjectMapper mapper) throws Exception {
33+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
34+
ObjectOutputStream outputStream = new ObjectOutputStream(byteArrayOutputStream);
35+
36+
outputStream.writeObject(mapper);
37+
byte[] serializedBytes = byteArrayOutputStream.toByteArray();
38+
39+
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serializedBytes);
40+
ObjectInputStream inputStream = new ObjectInputStream(byteArrayInputStream);
41+
Object deserializedObject = inputStream.readObject();
42+
assertTrue("Deserialized object should be an instance of ObjectMapper",
43+
deserializedObject instanceof ObjectMapper);
44+
return (ObjectMapper) deserializedObject;
45+
}
46+
47+
private Employee _simpleEmployee() {
48+
Employee boss = new Employee();
49+
boss.name = "Pointy";
50+
boss.age = 25;
51+
boss.emails = new String[] { "[email protected]" };
52+
boss.boss = null;
53+
54+
Employee empl = new Employee();
55+
empl.name = "Bob";
56+
empl.age = 39;
57+
empl.emails = new String[] { "[email protected]", "[email protected]" };
58+
59+
// NOTE: currently problematic (gives us a union...)
60+
empl.boss = boss;
61+
return empl;
62+
}
63+
}

0 commit comments

Comments
 (0)