This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 5caacce824e8 CAMEL-26975: camel-csv - an empty header is handled like 
no header (#30005)
5caacce824e8 is described below

commit 5caacce824e8cb36e477bf7fa0b5d3953f75f42c
Author: Federico Mariani <[email protected]>
AuthorDate: Sun Nov 15 15:34:33 2026 +0100

    CAMEL-26975: camel-csv - an empty header is handled like no header (#30005)
    
    * CAMEL-26975: camel-csv - an empty header is handled like no header
    
    An empty or blank header became one fixed column named "", so Map rows were 
marshalled as empty lines
    (regression of CAMEL-17767 since CAMEL-21734). It now keeps the header of 
the format, as the Javadoc says.
    
    
    Co-Authored-By: Claude <[email protected]>
---
 .../apache/camel/dataformat/csv/CsvDataFormat.java |  9 +-
 .../camel/dataformat/csv/CsvEmptyHeaderTest.java   | 99 ++++++++++++++++++++++
 2 files changed, 104 insertions(+), 4 deletions(-)

diff --git 
a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
 
b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
index 911b89cef6fd..22234b8ae4a5 100644
--- 
a/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
+++ 
b/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
@@ -168,7 +168,8 @@ public class CsvDataFormat extends ServiceSupport 
implements DataFormat, DataFor
 
         if (headerDisabled) {
             answer = answer.builder().setHeader((String[]) null).get(); // 
null disables the header
-        } else if (header != null) {
+        } else if (header != null && !header.isBlank()) {
+            // a blank header is no header, not one column named ""
             if (header.indexOf(',') != -1) {
                 answer = answer.builder().setHeader(header.split(",")).get();
             } else {
@@ -397,7 +398,7 @@ public class CsvDataFormat extends ServiceSupport 
implements DataFormat, DataFor
     /**
      * Gets the header. Multiple values can be separated by comma.
      *
-     * If {@code null} then the default one of the format used. If empty, then 
it will be automatically handled.
+     * If {@code null}, empty or blank, then the header of the format is used.
      *
      * @return Header
      */
@@ -406,9 +407,9 @@ public class CsvDataFormat extends ServiceSupport 
implements DataFormat, DataFor
     }
 
     /**
-     * Gets the header. Multiple values can be separated by comma.
+     * Sets the header. Multiple values can be separated by comma.
      *
-     * If {@code null} then the default one of the format used. If empty, then 
it will be automatically handled.
+     * If {@code null}, empty or blank, then the header of the format is used.
      *
      * @param  header Header
      * @return        Current {@code CsvDataFormat}, fluent API
diff --git 
a/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvEmptyHeaderTest.java
 
b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvEmptyHeaderTest.java
new file mode 100644
index 000000000000..4f545516e6d8
--- /dev/null
+++ 
b/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvEmptyHeaderTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.camel.dataformat.csv;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * An empty or blank header is handled like no header: the columns of a Map 
row are its keys, and no header line is
+ * written.
+ */
+class CsvEmptyHeaderTest extends CamelTestSupport {
+    @EndpointInject("mock:output")
+    MockEndpoint output;
+
+    @Test
+    void emptyHeaderMarshalsTheMapValues() throws Exception {
+        assertEquals("abc,123\r\n", marshal("direct:empty"));
+    }
+
+    @Test
+    void emptyHeaderOnTheUriMarshalsTheMapValues() throws Exception {
+        assertEquals("abc,123\r\n", marshal("direct:emptyUri"));
+    }
+
+    @Test
+    void blankHeaderMarshalsTheMapValues() throws Exception {
+        assertEquals("abc,123\r\n", marshal("direct:blank"));
+    }
+
+    @Test
+    void emptyHeaderUnmarshalsTheFirstLineAsHeader() throws Exception {
+        output.expectedMessageCount(1);
+
+        template.sendBody("direct:emptyUnmarshal", "foo,bar\r\nabc,123\r\n");
+        output.assertIsSatisfied();
+
+        assertEquals(List.of(Map.of("foo", "abc", "bar", "123")), 
output.getExchanges().get(0).getIn().getBody(List.class));
+    }
+
+    private String marshal(String uri) throws Exception {
+        output.expectedMessageCount(1);
+
+        Map<String, Object> row = new LinkedHashMap<>();
+        row.put("foo", "abc");
+        row.put("bar", 123);
+        template.sendBody(uri, row);
+        output.assertIsSatisfied();
+
+        return output.getExchanges().get(0).getIn().getBody(String.class);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:empty")
+                        .marshal(new CsvDataFormat().setHeader(""))
+                        .to("mock:output");
+
+                from("direct:emptyUri")
+                        .to("dataformat:csv:marshal?header=")
+                        .to("mock:output");
+
+                from("direct:blank")
+                        .marshal(new CsvDataFormat().setHeader(" "))
+                        .to("mock:output");
+
+                from("direct:emptyUnmarshal")
+                        .unmarshal(new 
CsvDataFormat().setHeader("").setUseMaps(true))
+                        .to("mock:output");
+            }
+        };
+    }
+}

Reply via email to