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 e8655033dd00 CAMEL-23024: Fix yaml-io dumper for 
setHeaders/setVariables
e8655033dd00 is described below

commit e8655033dd00f259b7844ab099982c8dfbdb209c
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Feb 26 21:18:21 2026 +0100

    CAMEL-23024: Fix yaml-io dumper for setHeaders/setVariables
---
 .../java/org/apache/camel/yaml/io/EipNode.java     | 12 ++++++
 .../java/org/apache/camel/yaml/io/YamlWriter.java  | 16 ++++++++
 .../org/apache/camel/yaml/out/ModelWriterTest.java | 46 ++++++++++++++++++++++
 core/camel-yaml-io/src/test/resources/route12.yaml | 34 ++++++++++++++++
 core/camel-yaml-io/src/test/resources/route13.yaml | 34 ++++++++++++++++
 5 files changed, 142 insertions(+)

diff --git 
a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/io/EipNode.java 
b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/io/EipNode.java
index 2ba31f113679..bd85a0f461a8 100644
--- a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/io/EipNode.java
+++ b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/io/EipNode.java
@@ -162,6 +162,18 @@ class EipNode {
                 EipNode o = outputs.get(0);
                 JsonObject jo = o.asJsonObject();
                 answer.put(o.getName(), jo);
+            } else if ("setHeaders".equals(name)) {
+                JsonArray arr = new JsonArray();
+                for (EipNode o : outputs) {
+                    arr.add(o.asJsonObject());
+                }
+                answer.put("headers", arr);
+            } else if ("setVariables".equals(name)) {
+                JsonArray arr = new JsonArray();
+                for (EipNode o : outputs) {
+                    arr.add(o.asJsonObject());
+                }
+                answer.put("variables", arr);
             } else if ("choice".equals(name)) {
                 // special for choice
                 JsonArray arr = new JsonArray();
diff --git 
a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/io/YamlWriter.java 
b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/io/YamlWriter.java
index 28a8083e4887..c1fe972597cc 100644
--- a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/io/YamlWriter.java
+++ b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/io/YamlWriter.java
@@ -210,6 +210,12 @@ public class YamlWriter extends ServiceSupport implements 
CamelContextAware {
                 } else if ("choice".equals(parent.getName())) {
                     // special for choice/doCatch/doFinally
                     setMetadata(parent, name, last);
+                } else if ("setHeaders".equals(parent.getName())) {
+                    // special for setHeaders
+                    setMetadata(parent, "headers", last);
+                } else if ("setVariables".equals(parent.getName())) {
+                    // special for setVariables
+                    setMetadata(parent, "variables", last);
                 } else if (parent.isOutput()) {
                     List<EipModel> list = (List<EipModel>) 
parent.getMetadata().get("_output");
                     if (list == null) {
@@ -320,6 +326,16 @@ public class YamlWriter extends ServiceSupport implements 
CamelContextAware {
                 for (EipModel m : list) {
                     node.addOutput(asNode(m));
                 }
+            } else if ("setHeaders".equals(node.getName()) && 
"headers".equals(key)) {
+                List<EipModel> list = (List) entry.getValue();
+                for (EipModel m : list) {
+                    node.addOutput(asNode(m));
+                }
+            } else if ("setVariables".equals(node.getName()) && 
"variables".equals(key)) {
+                List<EipModel> list = (List) entry.getValue();
+                for (EipModel m : list) {
+                    node.addOutput(asNode(m));
+                }
             } else if ("choice".equals(node.getName()) && 
"otherwise".equals(key)) {
                 EipModel other = (EipModel) entry.getValue();
                 node.addOutput(asNode(other));
diff --git 
a/core/camel-yaml-io/src/test/java/org/apache/camel/yaml/out/ModelWriterTest.java
 
b/core/camel-yaml-io/src/test/java/org/apache/camel/yaml/out/ModelWriterTest.java
index 4f27542b62ac..8d1968c3d3dc 100644
--- 
a/core/camel-yaml-io/src/test/java/org/apache/camel/yaml/out/ModelWriterTest.java
+++ 
b/core/camel-yaml-io/src/test/java/org/apache/camel/yaml/out/ModelWriterTest.java
@@ -33,6 +33,10 @@ import org.apache.camel.model.ModelCamelContext;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.model.RoutesDefinition;
 import org.apache.camel.model.SetBodyDefinition;
+import org.apache.camel.model.SetHeaderDefinition;
+import org.apache.camel.model.SetHeadersDefinition;
+import org.apache.camel.model.SetVariableDefinition;
+import org.apache.camel.model.SetVariablesDefinition;
 import org.apache.camel.model.SplitDefinition;
 import org.apache.camel.model.ToDefinition;
 import org.apache.camel.model.dataformat.CsvDataFormat;
@@ -327,4 +331,46 @@ public class ModelWriterTest {
         Assertions.assertEquals(expected, out);
     }
 
+    @Test
+    public void testSetHeaders() throws Exception {
+        StringWriter sw = new StringWriter();
+        ModelWriter writer = new ModelWriter(sw);
+
+        RouteDefinition route = new RouteDefinition();
+        route.setId("myRout12");
+        route.setInput(new FromDefinition("timer:foo"));
+        SetHeadersDefinition sh = new SetHeadersDefinition();
+        sh.getHeaders().add(new SetHeaderDefinition("foo", new 
ConstantExpression("hello world")));
+        sh.getHeaders().add(new SetHeaderDefinition("bar", new 
SimpleExpression("bye ${body}")));
+        route.addOutput(sh);
+        route.addOutput(new LogDefinition("${body}"));
+
+        writer.writeRouteDefinition(route);
+
+        String out = sw.toString();
+        String expected = 
stripLineComments(Paths.get("src/test/resources/route12.yaml"), "#", true);
+        Assertions.assertEquals(expected, out);
+    }
+
+    @Test
+    public void testSetVariables() throws Exception {
+        StringWriter sw = new StringWriter();
+        ModelWriter writer = new ModelWriter(sw);
+
+        RouteDefinition route = new RouteDefinition();
+        route.setId("myRout13");
+        route.setInput(new FromDefinition("timer:foo"));
+        SetVariablesDefinition sv = new SetVariablesDefinition();
+        sv.getVariables().add(new SetVariableDefinition("foo", new 
ConstantExpression("hello2 world")));
+        sv.getVariables().add(new SetVariableDefinition("bar", new 
SimpleExpression("bye2 ${body}")));
+        route.addOutput(sv);
+        route.addOutput(new LogDefinition("${body}"));
+
+        writer.writeRouteDefinition(route);
+
+        String out = sw.toString();
+        String expected = 
stripLineComments(Paths.get("src/test/resources/route13.yaml"), "#", true);
+        Assertions.assertEquals(expected, out);
+    }
+
 }
diff --git a/core/camel-yaml-io/src/test/resources/route12.yaml 
b/core/camel-yaml-io/src/test/resources/route12.yaml
new file mode 100644
index 000000000000..aa6daa8a1a46
--- /dev/null
+++ b/core/camel-yaml-io/src/test/resources/route12.yaml
@@ -0,0 +1,34 @@
+#
+# 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.
+#
+
+- route:
+    id: myRout12
+    from:
+      uri: timer:foo
+      steps:
+        - setHeaders:
+            headers:
+              - name: foo
+                expression:
+                  constant:
+                    expression: hello world
+              - name: bar
+                expression:
+                  simple:
+                    expression: "bye ${body}"
+        - log:
+            message: "${body}"
diff --git a/core/camel-yaml-io/src/test/resources/route13.yaml 
b/core/camel-yaml-io/src/test/resources/route13.yaml
new file mode 100644
index 000000000000..ba9b7ff69089
--- /dev/null
+++ b/core/camel-yaml-io/src/test/resources/route13.yaml
@@ -0,0 +1,34 @@
+#
+# 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.
+#
+
+- route:
+    id: myRout13
+    from:
+      uri: timer:foo
+      steps:
+        - setVariables:
+            variables:
+              - name: foo
+                expression:
+                  constant:
+                    expression: hello2 world
+              - name: bar
+                expression:
+                  simple:
+                    expression: "bye2 ${body}"
+        - log:
+            message: "${body}"

Reply via email to