This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.22.x by this push:
new 7fe48f7b1feb [backport camel-4.22.x] CAMEL-26975: camel-csv - an empty
header is handled like no header (#30025)
7fe48f7b1feb is described below
commit 7fe48f7b1febaae4159726858158d28f544f5519
Author: Guillaume Nodet <[email protected]>
AuthorDate: Sun Nov 15 16:32:05 2026 +0100
[backport camel-4.22.x] CAMEL-26975: camel-csv - an empty header is handled
like no header (#30025)
---
.../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 5e443a58c644..66b5e1ea0f4b 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
@@ -155,7 +155,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 {
@@ -384,7 +385,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
*/
@@ -393,9 +394,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");
+ }
+ };
+ }
+}