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

gnodet 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 c760b1f6314f CAMEL-22745: Add transformer/validator support to YAML 
DSL root (#21752)
c760b1f6314f is described below

commit c760b1f6314fcbf36217757013ea3d690c7d8207
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Mar 6 20:56:04 2026 +0100

    CAMEL-22745: Add transformer/validator support to YAML DSL root (#21752)
    
    Add transformers and validators as root-level elements in the YAML DSL,
    bringing feature parity with the XML DSL.
    
    - Mark TransformersDefinition and ValidatorsDefinition with @YamlIn in
      the code generator so they appear as root-level elements in the schema
    - Handle TransformersDefinition and ValidatorsDefinition in
      YamlRoutesBuilderLoader.doConfigure()
    - Add tests for transformers and validators in YAML DSL
---
 .../dsl/yaml/deserializers/ModelDeserializers.java |   2 +
 .../dsl/yaml/GenerateYamlDeserializersMojo.java    |   6 +
 .../generated/resources/schema/camelYamlDsl.json   |   6 +
 .../camel/dsl/yaml/YamlRoutesBuilderLoader.java    |  17 +++
 .../apache/camel/dsl/yaml/TransformersTest.groovy  | 128 +++++++++++++++++++++
 .../apache/camel/dsl/yaml/ValidatorsTest.groovy    | 100 ++++++++++++++++
 6 files changed, 259 insertions(+)

diff --git 
a/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/generated/java/org/apache/camel/dsl/yaml/deserializers/ModelDeserializers.java
 
b/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/generated/java/org/apache/camel/dsl/yaml/deserializers/ModelDeserializers.java
index 853c9d66ea45..1e366f785328 100644
--- 
a/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/generated/java/org/apache/camel/dsl/yaml/deserializers/ModelDeserializers.java
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl-deserializers/src/generated/java/org/apache/camel/dsl/yaml/deserializers/ModelDeserializers.java
@@ -18580,6 +18580,7 @@ public final class ModelDeserializers extends 
YamlDeserializerSupport {
         }
     }
 
+    @YamlIn
     @YamlType(
             nodes = "transformers",
             types = 
org.apache.camel.model.transformer.TransformersDefinition.class,
@@ -19645,6 +19646,7 @@ public final class ModelDeserializers extends 
YamlDeserializerSupport {
         }
     }
 
+    @YamlIn
     @YamlType(
             nodes = "validators",
             types = 
org.apache.camel.model.validator.ValidatorsDefinition.class,
diff --git 
a/dsl/camel-yaml-dsl/camel-yaml-dsl-maven-plugin/src/main/java/org/apache/camel/maven/dsl/yaml/GenerateYamlDeserializersMojo.java
 
b/dsl/camel-yaml-dsl/camel-yaml-dsl-maven-plugin/src/main/java/org/apache/camel/maven/dsl/yaml/GenerateYamlDeserializersMojo.java
index bfb04486f184..f78fdb998cca 100644
--- 
a/dsl/camel-yaml-dsl/camel-yaml-dsl-maven-plugin/src/main/java/org/apache/camel/maven/dsl/yaml/GenerateYamlDeserializersMojo.java
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl-maven-plugin/src/main/java/org/apache/camel/maven/dsl/yaml/GenerateYamlDeserializersMojo.java
@@ -436,6 +436,12 @@ public class GenerateYamlDeserializersMojo extends 
GenerateYamlSupportMojo {
         if 
(info.name().toString().equals("org.apache.camel.model.rest.RestConfigurationDefinition"))
 {
             builder.addAnnotation(CN_YAML_IN);
         }
+        if 
(info.name().toString().equals("org.apache.camel.model.transformer.TransformersDefinition"))
 {
+            builder.addAnnotation(CN_YAML_IN);
+        }
+        if 
(info.name().toString().equals("org.apache.camel.model.validator.ValidatorsDefinition"))
 {
+            builder.addAnnotation(CN_YAML_IN);
+        }
 
         final AtomicReference<String> modelName = new AtomicReference<>();
         annotationValue(info, XML_ROOT_ELEMENT_ANNOTATION_CLASS, "name")
diff --git 
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/generated/resources/schema/camelYamlDsl.json
 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/generated/resources/schema/camelYamlDsl.json
index cd51c7139364..b917b8388fbf 100644
--- 
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/generated/resources/schema/camelYamlDsl.json
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/generated/resources/schema/camelYamlDsl.json
@@ -17107,6 +17107,12 @@
       },
       "rest" : {
         "$ref" : 
"#/items/definitions/org.apache.camel.model.rest.RestDefinition"
+      },
+      "transformers" : {
+        "$ref" : 
"#/items/definitions/org.apache.camel.model.transformer.TransformersDefinition"
+      },
+      "validators" : {
+        "$ref" : 
"#/items/definitions/org.apache.camel.model.validator.ValidatorsDefinition"
       }
     }
   }
diff --git 
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/YamlRoutesBuilderLoader.java
 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/YamlRoutesBuilderLoader.java
index ca6499b2891d..7c323fac5515 100644
--- 
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/YamlRoutesBuilderLoader.java
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/YamlRoutesBuilderLoader.java
@@ -41,6 +41,7 @@ import org.apache.camel.model.InterceptDefinition;
 import org.apache.camel.model.InterceptFromDefinition;
 import org.apache.camel.model.InterceptSendToEndpointDefinition;
 import org.apache.camel.model.KameletDefinition;
+import org.apache.camel.model.Model;
 import org.apache.camel.model.OnCompletionDefinition;
 import org.apache.camel.model.OnExceptionDefinition;
 import org.apache.camel.model.ProcessorDefinition;
@@ -55,6 +56,8 @@ import 
org.apache.camel.model.errorhandler.NoErrorHandlerDefinition;
 import org.apache.camel.model.rest.RestConfigurationDefinition;
 import org.apache.camel.model.rest.RestDefinition;
 import org.apache.camel.model.rest.VerbDefinition;
+import org.apache.camel.model.transformer.TransformersDefinition;
+import org.apache.camel.model.validator.ValidatorsDefinition;
 import org.apache.camel.spi.CamelContextCustomizer;
 import org.apache.camel.spi.DataType;
 import org.apache.camel.spi.Resource;
@@ -252,6 +255,20 @@ public class YamlRoutesBuilderLoader extends 
YamlRoutesBuilderLoaderSupport {
                             getCamelContext(),
                             getCamelContext().getRestConfiguration());
                     return true;
+                } else if (item instanceof TransformersDefinition definition) {
+                    if (definition.getTransformers() != null) {
+                        getCamelContext().getCamelContextExtension()
+                                .getContextPlugin(Model.class)
+                                
.getTransformers().addAll(definition.getTransformers());
+                    }
+                    return true;
+                } else if (item instanceof ValidatorsDefinition definition) {
+                    if (definition.getValidators() != null) {
+                        getCamelContext().getCamelContextExtension()
+                                .getContextPlugin(Model.class)
+                                
.getValidators().addAll(definition.getValidators());
+                    }
+                    return true;
                 }
 
                 return false;
diff --git 
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/TransformersTest.groovy
 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/TransformersTest.groovy
new file mode 100644
index 000000000000..f51759e03336
--- /dev/null
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/TransformersTest.groovy
@@ -0,0 +1,128 @@
+/*
+ * 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 org.apache.camel.dsl.yaml.support.YamlTestSupport
+import org.apache.camel.model.Model
+import org.apache.camel.model.transformer.DataFormatTransformerDefinition
+import org.apache.camel.model.transformer.EndpointTransformerDefinition
+import org.apache.camel.model.transformer.CustomTransformerDefinition
+import org.apache.camel.model.transformer.LoadTransformerDefinition
+
+class TransformersTest extends YamlTestSupport {
+
+    def "transformers with loadTransformer"() {
+        when:
+        loadRoutes """
+                - transformers:
+                    loadTransformer:
+                      defaults: true
+            """
+
+        then:
+        def transformers = 
context.getCamelContextExtension().getContextPlugin(Model.class).getTransformers()
+        transformers.size() == 1
+        with(transformers[0], LoadTransformerDefinition) {
+            it.defaults == 'true'
+        }
+    }
+
+    def "transformers with endpointTransformer"() {
+        when:
+        loadRoutes """
+                - transformers:
+                    endpointTransformer:
+                      ref: myXmlEndpoint
+                      fromType: xml:XmlXOrder
+                      toType: "java:org.example.XOrder"
+            """
+
+        then:
+        def transformers = 
context.getCamelContextExtension().getContextPlugin(Model.class).getTransformers()
+        transformers.size() == 1
+        with(transformers[0], EndpointTransformerDefinition) {
+            it.ref == 'myXmlEndpoint'
+            it.fromType == 'xml:XmlXOrder'
+            it.toType == 'java:org.example.XOrder'
+        }
+    }
+
+    def "transformers with customTransformer"() {
+        when:
+        loadRoutes """
+                - transformers:
+                    customTransformer:
+                      className: org.example.MyTransformer
+                      fromType: other:OtherXOrder
+                      toType: "java:org.example.XOrder"
+            """
+
+        then:
+        def transformers = 
context.getCamelContextExtension().getContextPlugin(Model.class).getTransformers()
+        transformers.size() == 1
+        with(transformers[0], CustomTransformerDefinition) {
+            it.className == 'org.example.MyTransformer'
+            it.fromType == 'other:OtherXOrder'
+            it.toType == 'java:org.example.XOrder'
+        }
+    }
+
+    def "transformers with dataFormatTransformer"() {
+        when:
+        loadRoutes """
+                - transformers:
+                    dataFormatTransformer:
+                      fromType: xml:XmlXOrder
+                      toType: "java:org.example.XOrder"
+                      json:
+                        library: Jackson
+            """
+
+        then:
+        def transformers = 
context.getCamelContextExtension().getContextPlugin(Model.class).getTransformers()
+        transformers.size() == 1
+        with(transformers[0], DataFormatTransformerDefinition) {
+            it.fromType == 'xml:XmlXOrder'
+            it.toType == 'java:org.example.XOrder'
+            it.dataFormatType != null
+        }
+    }
+
+    def "multiple transformers"() {
+        when:
+        loadRoutes """
+                - transformers:
+                    loadTransformer:
+                      defaults: true
+                    endpointTransformer:
+                      ref: myXmlEndpoint
+                      fromType: xml:XmlXOrder
+                      toType: "java:org.example.XOrder"
+                    customTransformer:
+                      className: org.example.MyTransformer
+                      fromType: other:OtherXOrder
+                      toType: "java:org.example.XOrder"
+            """
+
+        then:
+        def transformers = 
context.getCamelContextExtension().getContextPlugin(Model.class).getTransformers()
+        transformers.size() == 3
+        transformers[0] instanceof LoadTransformerDefinition
+        transformers[1] instanceof EndpointTransformerDefinition
+        transformers[2] instanceof CustomTransformerDefinition
+    }
+}
diff --git 
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/ValidatorsTest.groovy
 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/ValidatorsTest.groovy
new file mode 100644
index 000000000000..5e4e83031ae5
--- /dev/null
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/ValidatorsTest.groovy
@@ -0,0 +1,100 @@
+/*
+ * 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 org.apache.camel.dsl.yaml.support.YamlTestSupport
+import org.apache.camel.model.Model
+import org.apache.camel.model.validator.EndpointValidatorDefinition
+import org.apache.camel.model.validator.CustomValidatorDefinition
+import org.apache.camel.model.validator.PredicateValidatorDefinition
+
+class ValidatorsTest extends YamlTestSupport {
+
+    def "validators with endpointValidator"() {
+        when:
+        loadRoutes """
+                - validators:
+                    endpointValidator:
+                      type: xml:XmlXOrderResponse
+                      uri: "myxml:endpoint"
+            """
+
+        then:
+        def validators = 
context.getCamelContextExtension().getContextPlugin(Model.class).getValidators()
+        validators.size() == 1
+        with(validators[0], EndpointValidatorDefinition) {
+            it.type == 'xml:XmlXOrderResponse'
+            it.uri == 'myxml:endpoint'
+        }
+    }
+
+    def "validators with customValidator"() {
+        when:
+        loadRoutes """
+                - validators:
+                    customValidator:
+                      type: other:OtherXOrder
+                      className: org.example.OtherXOrderValidator
+            """
+
+        then:
+        def validators = 
context.getCamelContextExtension().getContextPlugin(Model.class).getValidators()
+        validators.size() == 1
+        with(validators[0], CustomValidatorDefinition) {
+            it.type == 'other:OtherXOrder'
+            it.className == 'org.example.OtherXOrderValidator'
+        }
+    }
+
+    def "validators with predicateValidator"() {
+        when:
+        loadRoutes """
+                - validators:
+                    predicateValidator:
+                      type: xml:XmlXOrderResponse
+                      expression:
+                        simple: "\${body} != null"
+            """
+
+        then:
+        def validators = 
context.getCamelContextExtension().getContextPlugin(Model.class).getValidators()
+        validators.size() == 1
+        with(validators[0], PredicateValidatorDefinition) {
+            it.type == 'xml:XmlXOrderResponse'
+            it.expression != null
+        }
+    }
+
+    def "multiple validators"() {
+        when:
+        loadRoutes """
+                - validators:
+                    endpointValidator:
+                      type: xml:XmlXOrderResponse
+                      uri: "myxml:endpoint"
+                    customValidator:
+                      type: other:OtherXOrder
+                      className: org.example.OtherXOrderValidator
+            """
+
+        then:
+        def validators = 
context.getCamelContextExtension().getContextPlugin(Model.class).getValidators()
+        validators.size() == 2
+        validators[0] instanceof EndpointValidatorDefinition
+        validators[1] instanceof CustomValidatorDefinition
+    }
+}

Reply via email to