You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2022/10/19 16:58:00 UTC

[jira] [Work logged] (CSV-302) CSVFormat.duplicateHeaderMode requires default for backward compatibility

     [ https://issues.apache.org/jira/browse/CSV-302?focusedWorklogId=818531&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-818531 ]

ASF GitHub Bot logged work on CSV-302:
--------------------------------------

                Author: ASF GitHub Bot
            Created on: 19/Oct/22 16:57
            Start Date: 19/Oct/22 16:57
    Worklog Time Spent: 10m 
      Work Description: garydgregory commented on code in PR #276:
URL: https://github.com/apache/commons-csv/pull/276#discussion_r999720402


##########
src/main/java/org/apache/commons/csv/CSVFormat.java:
##########
@@ -2280,6 +2279,27 @@ private void validate() throws IllegalArgumentException {
         }
     }
 
+    private Object readResolve() throws ObjectStreamException {
+        // check whether field duplicateHeaderMode (as of 1.10.0) was found in serialized form
+        // if not, set from boolean allowDuplicateHeaderNames
+        if (duplicateHeaderMode == null) {
+            setFieldValue("duplicateHeaderMode",
+                    allowDuplicateHeaderNames ? DuplicateHeaderMode.ALLOW_ALL : DuplicateHeaderMode.DISALLOW);
+        }
+        return this;
+    }
+
+    private void setFieldValue(String fieldName, Object value) {
+        try {
+            Field fld = getClass().getDeclaredField(fieldName);
+            fld.setAccessible(true); // make field non-final
+            fld.set(this, value);
+            fld.setAccessible(false);
+        } catch (Exception ex) {
+            throw new RuntimeException("Failed to set value of field " + fieldName, ex);

Review Comment:
   RuntimeException is an anti-pattern, using IAE or ISE or another more descriptive exception.



##########
src/test/java/org/apache/commons/csv/CSVFormatSerializationTest.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.csv;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+/**
+ * Test deserialization of class {@link CSVFormat}.
+ * @author Markus Spann

Review Comment:
   We don't use `author` tags. Credit is given in the changes.xml file post-merge.



##########
src/main/java/org/apache/commons/csv/CSVFormat.java:
##########
@@ -2280,6 +2279,27 @@ private void validate() throws IllegalArgumentException {
         }
     }
 
+    private Object readResolve() throws ObjectStreamException {
+        // check whether field duplicateHeaderMode (as of 1.10.0) was found in serialized form
+        // if not, set from boolean allowDuplicateHeaderNames
+        if (duplicateHeaderMode == null) {
+            setFieldValue("duplicateHeaderMode",
+                    allowDuplicateHeaderNames ? DuplicateHeaderMode.ALLOW_ALL : DuplicateHeaderMode.DISALLOW);
+        }
+        return this;
+    }
+
+    private void setFieldValue(String fieldName, Object value) {

Review Comment:
   Use `final` where you can.





Issue Time Tracking
-------------------

            Worklog Id:     (was: 818531)
    Remaining Estimate: 0h
            Time Spent: 10m

> CSVFormat.duplicateHeaderMode requires default for backward compatibility
> -------------------------------------------------------------------------
>
>                 Key: CSV-302
>                 URL: https://issues.apache.org/jira/browse/CSV-302
>             Project: Commons CSV
>          Issue Type: Bug
>          Components: Parser
>    Affects Versions: 1.10.0
>            Reporter: Markus Spann
>            Priority: Minor
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> The member in class {{CSVFormat}}
> {code:java}
> boolean allowDuplicateHeaderNames{code}
>  was recently replaced by 
> {code:java}
> DuplicateHeaderMode duplicateHeaderMode{code}
> The boolean defaults to {{{}false{}}}, while the member of type {{DuplicateHeaderMode}} defaults to {{{}null{}}}.
> {{duplicateHeaderMode}} must be initialized with {{DISALLOW}} for backward compatibility.
> The change is also problematic with regards to serialization. The class is serializable and {{serialVersionUID}} is unchanged between versions. The boolean setting {{allowDuplicateHeaderNames}} in an object serialized in version 1.9.0 or earlier would always be de-serialized to {{duplicateHeaderMode = null}} in the current head.
> To handle the code changes correctly, customized de-serialization would need to be implemented.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)