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

Croway 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 cd745ed8f55e [camel-4.22.x] CAMEL-25494: camel-yaml-io - write the 
onException, onCompletion and interceptors of a route configuration as the YAML 
DSL loads them (#27683)
cd745ed8f55e is described below

commit cd745ed8f55ea1f982fd14068a42b4f628d5773d
Author: Federico Mariani <[email protected]>
AuthorDate: Sun Oct 11 10:38:29 2026 +0200

    [camel-4.22.x] CAMEL-25494: camel-yaml-io - write the onException, 
onCompletion and interceptors of a route configuration as the YAML DSL loads 
them (#27683)
    
    CAMEL-25494: camel-yaml-io - write the onException, onCompletion and 
interceptors of a route configuration as the YAML DSL loads them
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../org/apache/camel/yaml/out/YamlModelWriter.java | 10 ++++-----
 .../org/apache/camel/yaml/LwModelToYAMLDumper.java | 22 ++++++++++++++++++++
 .../camel/yaml/out/YamlModelWriterSupport.java     | 19 +++++++++++++++++
 .../src/test/resources/yaml-routeconfig.yaml       | 24 ++++++++++++----------
 .../main/resources/velocity/model-yaml-writer.vm   |  5 +++++
 5 files changed, 64 insertions(+), 16 deletions(-)

diff --git 
a/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/YamlModelWriter.java
 
b/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/YamlModelWriter.java
index c7aef4342955..c50d216caba3 100644
--- 
a/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/YamlModelWriter.java
+++ 
b/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/YamlModelWriter.java
@@ -1584,11 +1584,11 @@ public class YamlModelWriter extends 
YamlModelWriterSupport {
         JsonObject jo = new JsonObject();
         doWriteOptionalIdentifiedDefinitionAttributes(jo, def);
         doWriteAttribute(jo, "precondition", def.getPrecondition(), null);
-        doWriteChildList(jo, null, "onException", def.getOnExceptions(), 
this::doWriteOnExceptionDefinition);
-        doWriteChildList(jo, null, "onCompletion", def.getOnCompletions(), 
this::doWriteOnCompletionDefinition);
-        doWriteChildList(jo, null, "interceptSendToEndpoint", 
def.getInterceptSendTos(), this::doWriteInterceptSendToEndpointDefinition);
-        doWriteChildList(jo, null, "interceptFrom", def.getInterceptFroms(), 
this::doWriteInterceptFromDefinition);
-        doWriteChildList(jo, null, "intercept", def.getIntercepts(), 
this::doWriteInterceptDefinition);
+        doWriteWrappedChildList(jo, "onException", def.getOnExceptions(), 
this::doWriteOnExceptionDefinition);
+        doWriteWrappedChildList(jo, "onCompletion", def.getOnCompletions(), 
this::doWriteOnCompletionDefinition);
+        doWriteWrappedChildList(jo, "interceptSendToEndpoint", 
def.getInterceptSendTos(), this::doWriteInterceptSendToEndpointDefinition);
+        doWriteWrappedChildList(jo, "interceptFrom", def.getInterceptFroms(), 
this::doWriteInterceptFromDefinition);
+        doWriteWrappedChildList(jo, "intercept", def.getIntercepts(), 
this::doWriteInterceptDefinition);
         doWriteChildElement(jo, "errorHandler", def.getErrorHandler(), 
this::doWriteErrorHandlerDefinition);
         return jo;
     }
diff --git 
a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java
 
b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java
index b675e5f38599..bb95367d7d79 100644
--- 
a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java
+++ 
b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java
@@ -38,6 +38,8 @@ import org.apache.camel.model.BeanFactoryDefinition;
 import org.apache.camel.model.DataFormatDefinition;
 import org.apache.camel.model.ExpressionNode;
 import org.apache.camel.model.FromDefinition;
+import org.apache.camel.model.InterceptFromDefinition;
+import org.apache.camel.model.InterceptSendToEndpointDefinition;
 import org.apache.camel.model.Model;
 import org.apache.camel.model.OnExceptionDefinition;
 import org.apache.camel.model.OptionalIdentifiedDefinition;
@@ -160,6 +162,26 @@ public class LwModelToYAMLDumper implements 
ModelToYAMLDumper {
                 super.doWriteAttribute(jo, key, value, defaultValue);
             }
 
+            @Override
+            protected JsonObject 
doWriteInterceptFromDefinition(InterceptFromDefinition def) {
+                return 
keepUriPattern(super.doWriteInterceptFromDefinition(def), def.getUri());
+            }
+
+            @Override
+            protected JsonObject 
doWriteInterceptSendToEndpointDefinition(InterceptSendToEndpointDefinition def) 
{
+                return 
keepUriPattern(super.doWriteInterceptSendToEndpointDefinition(def), 
def.getUri());
+            }
+
+            // the uri of an interceptor is a pattern (such as mock:*), not an 
endpoint uri, and the YAML DSL has no
+            // parameters for it, so it is written as it is even when the uris 
are written as parameters
+            private JsonObject keepUriPattern(JsonObject jo, String uri) {
+                if (uriAsParameters && uri != null) {
+                    jo.remove("parameters");
+                    jo.put("uri", resolvePlaceholders ? resolve(uri, 
properties) : uri);
+                }
+                return jo;
+            }
+
             @Override
             protected JsonObject 
doWriteThrowExceptionDefinition(ThrowExceptionDefinition def) {
                 JsonObject jo = super.doWriteThrowExceptionDefinition(def);
diff --git 
a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/out/YamlModelWriterSupport.java
 
b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/out/YamlModelWriterSupport.java
index 3c024975df00..8e3a1e8aceb1 100644
--- 
a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/out/YamlModelWriterSupport.java
+++ 
b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/out/YamlModelWriterSupport.java
@@ -159,6 +159,25 @@ public abstract class YamlModelWriterSupport {
         }
     }
 
+    /**
+     * A list whose items the YAML DSL reads as one-key maps named after the 
list, such as the onException of a route
+     * configuration: <code>onException: [ { onException: { ... } } ]</code>
+     */
+    protected <T> void doWriteWrappedChildList(JsonObject jo, String key, 
List<T> list, Function<T, JsonObject> writer) {
+        if (list != null && !list.isEmpty()) {
+            JsonArray arr = new JsonArray();
+            for (T item : list) {
+                JsonObject child = writer.apply(item);
+                if (child != null) {
+                    arr.add(wrapNode(key, child));
+                }
+            }
+            if (!arr.isEmpty()) {
+                jo.put(key, arr);
+            }
+        }
+    }
+
     protected JsonObject wrapNode(String key, JsonObject value) {
         JsonObject wrapper = new JsonObject();
         if (value != null) {
diff --git a/core/camel-yaml-io/src/test/resources/yaml-routeconfig.yaml 
b/core/camel-yaml-io/src/test/resources/yaml-routeconfig.yaml
index daeb77d2ddc2..f7bfd73f89b2 100644
--- a/core/camel-yaml-io/src/test/resources/yaml-routeconfig.yaml
+++ b/core/camel-yaml-io/src/test/resources/yaml-routeconfig.yaml
@@ -19,15 +19,17 @@
     customId: true
     id: myConfig
     onException:
-      - exception:
-          - java.lang.Exception
-        handled:
-          constant:
-            expression: "true"
-        steps:
-          - to:
-              uri: mock:error
+      - onException:
+          exception:
+            - java.lang.Exception
+          handled:
+            constant:
+              expression: "true"
+          steps:
+            - to:
+                uri: mock:error
     intercept:
-      - steps:
-          - log:
-              message: intercepted
+      - intercept:
+          steps:
+            - log:
+                message: intercepted
diff --git 
a/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-yaml-writer.vm
 
b/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-yaml-writer.vm
index 8b3da44d0e4c..f630ee56b3c1 100644
--- 
a/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-yaml-writer.vm
+++ 
b/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-yaml-writer.vm
@@ -15,6 +15,9 @@
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
 #set( $default = "#[[##default]]#" )
+## the YAML DSL reads each item of some lists as a one-key map named after the 
list (wrapItem = true in the YAML DSL
+## deserializer), such as - onException: ... in a route configuration (keyed 
by class.member)
+#set( $wrapItems = [ "RouteConfigurationDefinition.intercepts", 
"RouteConfigurationDefinition.interceptFroms", 
"RouteConfigurationDefinition.interceptSendTos", 
"RouteConfigurationDefinition.onCompletions", 
"RouteConfigurationDefinition.onExceptions" ] )
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -193,6 +196,8 @@ public class YamlModelWriter extends YamlModelWriterSupport 
{
                             #end
                             #if( $t == "String" )
         doWriteStringList(jo, #if($w)"${w}"#{else}null#end, "${n}", 
def.${member.getter}());
+                            #elseif( 
$wrapItems.contains("${clazz.simpleName}.${member.name}") )
+        doWriteWrappedChildList(jo, "${n}", def.${member.getter}(), 
this::doWrite${t});
                             #else
         doWriteChildList(jo, #if($w)"${w}"#{else}null#end, "${n}", 
def.${member.getter}(), this::doWrite${t});
                             #end

Reply via email to