diff --git a/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/builder/SpecificRecordTest.java b/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/builder/SpecificRecordTest.java index 9a53a58a..ebdcc492 100644 --- a/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/builder/SpecificRecordTest.java +++ b/avro-builder/tests/tests-allavro/src/test/java/com/linkedin/avroutil1/builder/SpecificRecordTest.java @@ -11,10 +11,15 @@ import com.linkedin.avroutil1.compatibility.RandomRecordGenerator; import com.linkedin.avroutil1.compatibility.RecordGenerationConfig; import com.linkedin.avroutil1.compatibility.StringConverterUtil; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Parameter; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -1946,6 +1951,20 @@ public void testCharSeqAccessorForNoUtf8() { Assert.assertEquals((int) instance.intAr.get(instance.getIntAr().size() - 1), Integer.MAX_VALUE); } + @Test + public void testIfSerializable() throws IOException { + Path tempFile = Files.createTempFile(null, ".tmp"); + try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile.toFile()); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) { + vs19.TestCollections instance = new RandomRecordGenerator().randomSpecific(vs19.TestCollections.class, + RecordGenerationConfig.newConfig().withAvoidNulls(true)); + + objectOutputStream.writeObject(instance.getStrAr()); + } finally { + Files.delete(tempFile); + } + } + @BeforeClass public void setup() { System.setProperty("org.apache.avro.specific.use_custom_coders", "true"); diff --git a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/StringListView.java b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/StringListView.java index 965db29c..0e44ccdd 100644 --- a/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/StringListView.java +++ b/helper/helper/src/main/java/com/linkedin/avroutil1/compatibility/collectiontransformer/StringListView.java @@ -5,15 +5,17 @@ */ package com.linkedin.avroutil1.compatibility.collectiontransformer; +import java.io.Serializable; import java.util.AbstractList; import java.util.Iterator; +import java.util.stream.Collectors; import org.apache.avro.util.Utf8; /** * View of Utf8 List to allow get as String while still allowing set to reflect on the original object. */ -public class StringListView extends AbstractList { +public class StringListView extends AbstractList implements Serializable { // Not final to allow addition private java.util.List _utf8List; @@ -102,4 +104,12 @@ public String next() { } }; } + + /** + * Custom serialization to write the list of strings instead of the list of Utf8. + * ObjectOutputStream seeks this method to serialize the object. + */ + private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { + out.writeObject(_utf8List.stream().map(Utf8::toString).collect(Collectors.toList())); + } }