Croway commented on code in PR #27609:
URL: https://github.com/apache/camel/pull/27609#discussion_r4240764171


##########
core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultDumpRoutesStrategy.java:
##########
@@ -523,7 +523,9 @@ protected void doDumpRoutesAsYaml(CamelContext 
camelContext) {
                         continue;
                     }
                     Resource res = route.getResource();
-                    if (res == null) {
+                    if (res == null || outputFileName != null) {

Review Comment:
   Done in 5412c0a1a8c7:
   - routes are merged into one group only when they come from **more than 
one** resource. A single file dumped into a file keeps its resource, so the 
`Source:` header is back in the log;
   - when they are merged, `DefaultDumpRoutesStrategy` logs a WARN that names 
the files whose top-level `onException` now applies to the routes of the other 
files (route-configuration `onException`s are not counted);
   - upgrade guide notes in 88f123c0afaf (4.23, and 4.22.2 for #27610).
   
   Writing one file per input file from `camel validate normalize --output 
dir/` is the proper fix for the scope. I'll file that as a follow-up rather 
than grow this PR. Note that normalize runs with logging silenced, so the WARN 
helps `camel.main.dumpRoutes` users more than normalize users until then.
   
   _Claude Code on behalf of Croway_



##########
core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java:
##########
@@ -178,6 +203,9 @@ String resolve(String value, Properties properties) {
 
         List<JsonObject> roots = new ArrayList<>();
         try {
+            for (OnExceptionDefinition oe : topLevel) {

Review Comment:
   Yes, it's intended: the dump of a single route should load as is, so the 
`onException` that applies to it has to come before it, and the 
route-configuration one belongs to its route configuration (the YAML DSL has no 
place for it in the route). Pinned by 
`singleRouteIsDumpedWithTheTopLevelOnException` in 5412c0a1a8c7, which dumps 
`first` alone as `dumpRouteAsYaml` does and reloads it.
   
   _Claude Code on behalf of Croway_



##########
dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/OnExceptionYamlDumpTest.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.dsl.yaml;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+
+import org.apache.camel.dsl.yaml.support.YamlTestSupport;
+import org.apache.camel.impl.DefaultDumpRoutesStrategy;
+import org.apache.camel.model.OnExceptionDefinition;
+import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.model.RoutesDefinition;
+import org.apache.camel.yaml.LwModelToYAMLDumper;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class OnExceptionYamlDumpTest extends YamlTestSupport {
+
+    @ParameterizedTest
+    @ValueSource(booleans = { false, true })
+    void topLevelOnExceptionIsDumpedAsTopLevel(boolean started) throws 
Exception {
+        loadRoutes("""
+                - onException:
+                    exception:
+                      - java.lang.Exception
+                    handled:
+                      constant: "true"
+                    steps:
+                      - to: mock:error
+                - route:
+                    id: first
+                    from:
+                      uri: direct:first
+                      steps:
+                        - to: mock:first
+                - route:
+                    id: second
+                    from:
+                      uri: direct:second
+                      steps:
+                        - to: mock:second
+                """);
+        if (started) {
+            context.start();
+        }
+        String yaml = dumpRoutes();
+        if (started) {
+            context.stop();
+        }
+
+        // written once, before the routes, and not in the steps of each route
+        assertThat(yaml).startsWith("- onException:");
+        assertThat(yaml.split("onException:", -1)).hasSize(2);
+
+        // the dump is valid for the YAML DSL schema, and the onException 
still applies to all the routes
+        
context.removeRouteDefinitions(List.copyOf(context.getRouteDefinitions()));
+        loadRoutes(yaml);
+        for (String id : List.of("first", "second")) {
+            OnExceptionDefinition oe = (OnExceptionDefinition) 
context.getRouteDefinition(id).getOutputs().get(0);
+            assertThat(oe.isRouteScoped()).isFalse();
+            
assertThat(oe.getExceptions()).containsExactly("java.lang.Exception");
+        }
+    }
+
+    @Test
+    void routeConfigurationOnExceptionIsNotDumpedInTheRoutes() throws 
Exception {
+        loadRoutes("""
+                - routeConfiguration:
+                    onException:
+                      - onException:
+                          exception:
+                            - java.lang.Exception
+                          handled:
+                            constant: "true"
+                          steps:
+                            - to: mock:error
+                """);
+        loadRoutes("""
+                - route:
+                    id: myRoute
+                    from:
+                      uri: direct:start
+                      steps:
+                        - to: mock:result
+                """);
+        
assertThat(context.getRouteDefinition("myRoute").getOutputs().get(0)).isInstanceOf(OnExceptionDefinition.class);
+
+        // the onException is dumped with the route configuration
+        String yaml = dumpRoutes();
+        assertThat(yaml).doesNotContain("onException").contains("id: myRoute");
+        context.removeRouteDefinition(context.getRouteDefinition("myRoute"));
+        loadRoutes(yaml);
+    }
+
+    @Test
+    void onExceptionOfAnotherFileIsDumpedBeforeTheRoutesInOneFile(@TempDir 
Path dir) throws Exception {
+        // two files normalized into one, as camel validate normalize a.yaml 
b.yaml does: the onException of the
+        // second file must come before the route of the first one, or the 
dump does not load
+        loadRoutes("""
+                - route:
+                    id: first
+                    from:
+                      uri: direct:first
+                      steps:
+                        - to: mock:first
+                """, """
+                - onException:
+                    exception:
+                      - java.lang.Exception
+                    handled:
+                      constant: "true"
+                    steps:
+                      - to: mock:error
+                - route:
+                    id: second
+                    from:
+                      uri: direct:second
+                      steps:
+                        - to: mock:second
+                """);
+
+        Path output = dir.resolve("normalized.yaml");
+        DefaultDumpRoutesStrategy dump = new DefaultDumpRoutesStrategy();
+        dump.setCamelContext(context);
+        dump.setInclude("routes");
+        dump.setLog(false);
+        dump.setUriAsParameters(true);
+        dump.setOutput(output.toString());
+        dump.dumpRoutes("yaml");
+        String yaml = Files.readString(output);
+
+        assertThat(yaml).startsWith("- onException:");
+        assertThat(yaml.split("onException:", -1)).hasSize(2);
+        assertThat(yaml).contains("id: first", "id: second");
+
+        // the combined dump is valid for the YAML DSL schema and loads
+        
context.removeRouteDefinitions(List.copyOf(context.getRouteDefinitions()));
+        loadRoutes(yaml);
+        assertThat(context.getRouteDefinitions()).hasSize(2);

Review Comment:
   Applied in 5412c0a1a8c7, together with an assertion that the route of the 
first file has no `onException` before the dump. The test also checks the new 
WARN.
   
   _Claude Code on behalf of Croway_



##########
dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/OnExceptionYamlDumpTest.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * 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.dsl.yaml;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+
+import org.apache.camel.dsl.yaml.support.YamlTestSupport;
+import org.apache.camel.impl.DefaultDumpRoutesStrategy;
+import org.apache.camel.model.OnExceptionDefinition;
+import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.model.RoutesDefinition;
+import org.apache.camel.yaml.LwModelToYAMLDumper;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class OnExceptionYamlDumpTest extends YamlTestSupport {
+
+    @ParameterizedTest
+    @ValueSource(booleans = { false, true })
+    void topLevelOnExceptionIsDumpedAsTopLevel(boolean started) throws 
Exception {
+        loadRoutes("""
+                - onException:
+                    exception:
+                      - java.lang.Exception
+                    handled:
+                      constant: "true"

Review Comment:
   Applied in 5412c0a1a8c7 (all three, and in the new tests).
   
   _Claude Code on behalf of Croway_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to