From e3eca25d1329a202db4e63fb623117b63c74aa71 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Mon, 20 Jan 2020 18:36:22 -0500 Subject: [PATCH] [CSV-248] CSVRecord is not Serializable. Make field transient. --- src/changes/changes.xml | 1 + .../java/org/apache/commons/csv/CSVRecord.java | 2 +- .../org/apache/commons/csv/CSVRecordTest.java | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 3cc48b23..cd44e5bd 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -48,6 +48,7 @@ Post 1.7 release fixes. Upgrade test framework to JUnit 5 Jupiter #49, #50. A single empty header is allowed when not allowing empty column headers. #47. + CSVRecord is not Serializable. Use test scope for supercsv #48. Update tests from H2 1.4.199 to 1.4.200. Update tests from Hamcrest 2.1 to 2.2. diff --git a/src/main/java/org/apache/commons/csv/CSVRecord.java b/src/main/java/org/apache/commons/csv/CSVRecord.java index e75403e7..b7bf53e7 100644 --- a/src/main/java/org/apache/commons/csv/CSVRecord.java +++ b/src/main/java/org/apache/commons/csv/CSVRecord.java @@ -46,7 +46,7 @@ public final class CSVRecord implements Serializable, Iterable { private final String[] values; /** The parser that originates this record. */ - private final CSVParser parser; + private final transient CSVParser parser; CSVRecord(final CSVParser parser, final String[] values, final String comment, final long recordNumber, final long characterPosition) { diff --git a/src/test/java/org/apache/commons/csv/CSVRecordTest.java b/src/test/java/org/apache/commons/csv/CSVRecordTest.java index 97bb6fce..d13e84a7 100644 --- a/src/test/java/org/apache/commons/csv/CSVRecordTest.java +++ b/src/test/java/org/apache/commons/csv/CSVRecordTest.java @@ -23,7 +23,9 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.ObjectOutputStream; import java.io.StringReader; import java.util.ArrayList; import java.util.Collections; @@ -58,7 +60,7 @@ public class CSVRecordTest { headerMap = parser.getHeaderMap(); } } - + @Test public void testGetInt() { assertEquals(values[0], record.get(0)); @@ -184,6 +186,18 @@ public class CSVRecordTest { } } + @Test + public void testSerialization() throws IOException { + CSVRecord shortRec; + try (final CSVParser parser = CSVParser.parse("a,b", CSVFormat.newFormat(','))) { + shortRec = parser.iterator().next(); + } + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (ObjectOutputStream oos = new ObjectOutputStream(out)) { + oos.writeObject(shortRec); + } + } + @Test public void testToMap() { final Map map = this.recordWithHeader.toMap();