This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch fix/camel-23521-xml-dump-missing-closing-tags in repository https://gitbox.apache.org/repos/asf/camel.git
commit 76023ca039f7cd8ff8bc8f52c99aa7612cbb912c Author: Claus Ibsen <[email protected]> AuthorDate: Mon May 18 18:57:46 2026 +0200 CAMEL-23521: camel-core - XML route dumping is missing target element closing tags The doDumpXml() method in DefaultDumpRoutesStrategy was stripping the outer container element (routes, rests, routeTemplates) using simple string matching. The opening tag removal only matched bare `<routes>` but the LwModelToXMLDumper (the default xml-io based dumper) always emits a namespace attribute: `<routes xmlns="http://camel.apache.org/schema/xml-io">`. This caused the opening container tag to remain in the output while the closing tag was correctly removed, producing malformed XML in both logged and file output. Fix: use a regex that matches the opening container tag with or without any namespace/xmlns attributes, so both opening and closing tags are consistently stripped regardless of which dumper is in use. Co-Authored-By: Claude Sonnet 4.6 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../camel/impl/DefaultDumpRoutesStrategy.java | 6 +- .../impl/DefaultDumpRoutesStrategyXmlTest.java | 93 ++++++++++++++++++++++ 2 files changed, 95 insertions(+), 4 deletions(-) diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultDumpRoutesStrategy.java b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultDumpRoutesStrategy.java index cf5e5c38db59..52683d13291a 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultDumpRoutesStrategy.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultDumpRoutesStrategy.java @@ -741,12 +741,10 @@ public class DefaultDumpRoutesStrategy extends ServiceSupport implements DumpRou ModelToXMLDumper dumper, String replace, String kind, StringBuilder sbLocal, StringBuilder sbLog) { try { String xml = dumper.dumpModelAsXml(camelContext, def, resolvePlaceholders, generatedIds, false); - // remove spring schema xmlns that camel-jaxb dumper includes - xml = StringHelper.replaceFirst(xml, " xmlns=\"http://camel.apache.org/schema/spring\">", ">"); xml = xml.replace("</" + replace + ">", "</" + replace + ">\n"); - // remove outer tag (routes, rests, etc) + // remove outer tag (routes, rests, etc) including any xmlns attributes replace = replace + "s"; - xml = StringHelper.replaceFirst(xml, "<" + replace + ">", ""); + xml = xml.replaceFirst("<" + replace + "(?:\\s[^>]*)?>", ""); xml = StringHelper.replaceFirst(xml, "</" + replace + ">", ""); sbLocal.append(xml); diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultDumpRoutesStrategyXmlTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultDumpRoutesStrategyXmlTest.java new file mode 100644 index 000000000000..90e890f7cb8a --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultDumpRoutesStrategyXmlTest.java @@ -0,0 +1,93 @@ +/* + * 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.impl; + +import java.io.File; +import java.io.FileInputStream; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.util.IOHelper; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DefaultDumpRoutesStrategyXmlTest extends ContextTestSupport { + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + context.setDumpRoutes("xml"); + + DefaultDumpRoutesStrategy drd = new DefaultDumpRoutesStrategy(); + drd.setInclude("routes,routeTemplates"); + drd.setLog(false); + drd.setOutput(testDirectory().toString()); + drd.setResolvePlaceholders(false); + context.addService(drd); + + return context; + } + + @Test + public void testDumpXmlHasCorrectStructure() throws Exception { + File dir = testDirectory().toFile(); + File[] files = dir.listFiles(); + assertNotNull(files, "Expected dump files in " + dir); + + StringBuilder all = new StringBuilder(); + for (File f : files) { + if (f.getName().endsWith(".xml")) { + all.append(IOHelper.loadText(new FileInputStream(f))); + } + } + String xml = all.toString(); + + // the dump wraps output in <camel> and strips the outer container elements + assertTrue(xml.contains("<camel>"), "Expected <camel> root wrapper"); + assertTrue(xml.contains("</camel>"), "Expected </camel> root wrapper closing tag"); + + // outer container tags must be stripped entirely (both opening and closing) - CAMEL-23521 + assertFalse(xml.contains("<routes"), "Outer <routes> wrapper must be stripped"); + assertFalse(xml.contains("<routeTemplates"), "Outer <routeTemplates> wrapper must be stripped"); + + // individual route elements must be present and properly closed + assertTrue(xml.contains("<route "), "Expected individual <route> elements"); + assertTrue(xml.contains("</route>"), "Expected </route> closing tag"); + assertTrue(xml.contains("<routeTemplate "), "Expected individual <routeTemplate> elements"); + assertTrue(xml.contains("</routeTemplate>"), "Expected </routeTemplate> closing tag"); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + routeTemplate("myTemplate") + .templateParameter("greeting") + .from("direct:template") + .setBody().simple("{{greeting}} ${body}"); + + from("direct:greet").routeId("greet") + .setBody().constant("Hello World"); + } + }; + } +}
