This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27290-kamelet-eip-output-type in repository https://gitbox.apache.org/repos/asf/camel.git
commit d34724ae47d4c2106c3198aa1d52a034fce0dbb0 Author: Claus Ibsen <[email protected]> AuthorDate: Fri Nov 20 10:29:31 2026 +0100 CAMEL-27290: camel-kamelet - the output data type of a Kamelet is applied when it hands the message on With the Kamelet EIP, the output data type of an action Kamelet was applied after the steps that follow the EIP: the loader made it the output type of the template route, which ContractAdvice applies when that route completes, and kamelet:sink continues the EIP inside the template route. With to("kamelet:...") the steps came after the route had completed, so they got the type. CAMEL-27258 moved the type of a source Kamelet to the route property kameletSourceOutputType, which KameletProducer applies at kamelet:sink. The loader now does this for every Kamelet, under the route property kameletOutputType, so the type is applied once, at kamelet:sink, with the EIP and with to(). The producer also sets it as the data type of the message, as ContractAdvice did, so the input data type of the next Kamelet is converted from it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../apache/camel/component/kamelet/Kamelet.java | 7 +- .../camel/component/kamelet/KameletProducer.java | 82 ++++++---- .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 26 +-- .../camel/dsl/yaml/KameletRoutesBuilderLoader.java | 38 ++--- .../apache/camel/dsl/yaml/KameletLoaderTest.java | 178 ++++++++++++++++++++- 5 files changed, 254 insertions(+), 77 deletions(-) diff --git a/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/Kamelet.java b/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/Kamelet.java index c236158da284..3d87c1319b66 100644 --- a/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/Kamelet.java +++ b/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/Kamelet.java @@ -67,10 +67,11 @@ public final class Kamelet { public static final String NO_ERROR_HANDLER = "noErrorHandler"; public static final String BRIDGE_ERROR_HANDLER = "bridgeErrorHandler"; /** - * The route property with the output data type of a source Kamelet, set by the Kamelet YAML loader. The producer of - * {@code kamelet:sink} applies it when the message is handed on, before the consuming route runs. + * The route property with the output data type of a Kamelet, set by the Kamelet YAML loader. The producer of + * {@code kamelet:sink} applies it when the message is handed on, before the consuming route, or the steps after a + * Kamelet EIP, run. */ - public static final String SOURCE_OUTPUT_TYPE_PROPERTY = "kameletSourceOutputType"; + public static final String OUTPUT_TYPE_PROPERTY = "kameletOutputType"; static final String ADVANCED_OPTIONS = "internal.advanced.options"; // use a running counter as uuid diff --git a/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletProducer.java b/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletProducer.java index 61adebc9ee8c..d95afc2bfef7 100644 --- a/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletProducer.java +++ b/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletProducer.java @@ -26,6 +26,7 @@ import org.apache.camel.ServiceStatus; import org.apache.camel.processor.transformer.DataTypeProcessor; import org.apache.camel.processor.transformer.TypeConverterTransformer; import org.apache.camel.spi.DataType; +import org.apache.camel.spi.DataTypeAware; import org.apache.camel.spi.ManagementStrategy; import org.apache.camel.spi.RouteIdAware; import org.apache.camel.support.DefaultAsyncProducer; @@ -50,9 +51,10 @@ final class KameletProducer extends DefaultAsyncProducer implements RouteIdAware private final boolean sink; private String routeId; boolean registerKamelets; - // the output data type of the source Kamelet whose route sends to this kamelet:sink, looked up on the first message - private volatile Processor sourceOutputType; - private volatile boolean sourceOutputTypeResolved; + // the output data type of the Kamelet whose route sends to this kamelet:sink, looked up on the first message + private volatile Processor outputType; + private volatile DataType outputDataType; + private volatile boolean outputTypeResolved; public KameletProducer(KameletEndpoint endpoint, String key) { super(endpoint); @@ -89,19 +91,12 @@ final class KameletProducer extends DefaultAsyncProducer implements RouteIdAware // with producer and consumers linked together via the component if (sink) { - // a source Kamelet gives the message its output data type when it hands it on, so the consuming - // route gets it (as an output type of the route, it would be applied only after the consuming route) - Processor outputType = sourceOutputType(exchange); - if (outputType != null) { - try { - outputType.process(exchange); - } catch (Exception e) { - exchange.setException(e); - } - if (exchange.getException() != null) { - callback.done(true); - return true; - } + // a Kamelet gives the message its output data type when it hands it on, so the consuming route of + // a source, or the steps after a Kamelet EIP, get it (as an output type of the route, it would be + // applied only after them, as they run inside the route of the Kamelet) + if (!applyOutputType(exchange)) { + callback.done(true); + return true; } // when calling a kamelet:sink then lookup any waiting processor // from the Kamelet EIP to continue routing @@ -149,26 +144,52 @@ final class KameletProducer extends DefaultAsyncProducer implements RouteIdAware } /** - * The output data type of the source Kamelet whose route the exchange is in, from the route property that the - * Kamelet YAML loader sets, or null when it has none. The outcome is looked up once and kept. + * Applies the output data type of the Kamelet whose route the exchange is in, if it has one, and sets it as the + * data type of the message, as the output type of a route does. + * + * @return false when the exchange failed + */ + private boolean applyOutputType(Exchange exchange) { + Processor processor = outputType(exchange); + if (processor == null) { + return true; + } + try { + processor.process(exchange); + } catch (Exception e) { + exchange.setException(e); + } + if (exchange.getException() != null) { + return false; + } + if (exchange.getMessage() instanceof DataTypeAware message) { + message.setDataType(outputDataType); + } + return true; + } + + /** + * The output data type of the Kamelet whose route the exchange is in, from the route property that the Kamelet YAML + * loader sets, or null when it has none. The outcome is looked up once and kept. */ - private Processor sourceOutputType(Exchange exchange) { - if (!sourceOutputTypeResolved) { + private Processor outputType(Exchange exchange) { + if (!outputTypeResolved) { synchronized (this) { - if (!sourceOutputTypeResolved) { + if (!outputTypeResolved) { Route route = ExchangeHelper.getRoute(exchange); - Object type = route != null ? route.getProperties().get(Kamelet.SOURCE_OUTPUT_TYPE_PROPERTY) : null; + Object type = route != null ? route.getProperties().get(Kamelet.OUTPUT_TYPE_PROPERTY) : null; if (type != null) { - sourceOutputType = createSourceOutputType(route, type.toString()); + outputDataType = new DataType(type.toString()); + outputType = createOutputType(route, type.toString()); } - sourceOutputTypeResolved = true; + outputTypeResolved = true; } } } - return sourceOutputType; + return outputType; } - private Processor createSourceOutputType(Route route, String type) { + private Processor createOutputType(Route route, String type) { DataTypeProcessor processor = new DataTypeProcessor(null, type); processor.setCamelContext(getEndpoint().getCamelContext()); try { @@ -182,7 +203,7 @@ final class KameletProducer extends DefaultAsyncProducer implements RouteIdAware TypeConverterTransformer converter = new TypeConverterTransformer(dataType); return exchange -> converter.transform(exchange.getMessage(), DataType.ANY, dataType); } - LOG.warn("The source Kamelet of route {} declares the output data type {}, but no transformer for it is on" + LOG.warn("The Kamelet of route {} declares the output data type {}, but no transformer for it is on" + " the classpath, so messages are handed on unchanged: {}", route.getRouteId(), type, e.getMessage()); return null; @@ -191,9 +212,10 @@ final class KameletProducer extends DefaultAsyncProducer implements RouteIdAware @Override protected void doStop() throws Exception { - ServiceHelper.stopService(sourceOutputType); - sourceOutputType = null; - sourceOutputTypeResolved = false; + ServiceHelper.stopService(outputType); + outputType = null; + outputDataType = null; + outputTypeResolved = false; super.doStop(); } diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc index 0305fcce72f2..4282ad2f1c6d 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc @@ -228,16 +228,22 @@ This is what `xml` binding already did without its XML data format. Add the JSON (or `camel-jackson` for Jackson 2), or set `bindingMode` to `off`. With `bindingMode` `auto`, a route that finds neither a JSON nor an XML data format still starts, and logs a WARN as it binds nothing. -=== camel-kamelet - the output data type of a source Kamelet is applied before the consuming route - -A source Kamelet that declares a default output data type (`spec.dataTypes.out.default`) now gives the message that type -when it hands it on with `kamelet:sink`, so the route that consumes from the Kamelet gets it. Before, the type was -applied only when the route of the Kamelet completed, after the consuming route had run: the consuming route never saw -it, and an exchange failed at the end when the consuming route left a body that could not be converted to the type, such -as a `Map` for `binary`. This applies to Kamelets labelled `camel.apache.org/kamelet.type: source`, such as -`aws-sqs-source` (`text`), `slack-source` (`json`) or `azure-eventhubs-source` (`binary`) of the Kamelet catalog: the -first step of the consuming route now gets a `String`, JSON or a `byte[]` with the matching `Content-Type` header, where -it used to get the body of the component. +=== camel-kamelet - the output data type of a Kamelet is applied when it hands the message on + +A Kamelet that declares a default output data type (`spec.dataTypes.out.default`) now gives the message that type +when it hands it on with `kamelet:sink`. Before, the type was applied only when the route of the Kamelet completed: + +* For a source Kamelet, that was after the route that consumes from the Kamelet had run: the consuming route never saw + the type, and an exchange failed at the end when the consuming route left a body that could not be converted to the + type, such as a `Map` for `binary`. Sources of the Kamelet catalog such as `aws-sqs-source` (`text`), `slack-source` + (`json`) or `azure-eventhubs-source` (`binary`) now give the first step of the consuming route a `String`, JSON or a + `byte[]` with the matching `Content-Type` header, where it used to get the body of the component. +* For an action or a sink Kamelet called with the Kamelet EIP (`kamelet: name: ...` in YAML, `.kamelet("...")` in + Java), that was after the steps that follow the EIP had run, so these steps got the body before the type was applied. + They now get the body of the type, as the steps after a `to("kamelet:...")` already did. + +The message also gets the type as its data type, so the input data type of the next Kamelet is converted from it. +A message that does not reach `kamelet:sink`, such as one an action Kamelet filters out, no longer gets the type. When no transformer for the type is on the classpath, such as `json` with neither `camel-jackson` nor `camel-jackson3`, the message is handed on unchanged, as before, and a WARN is logged once. diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/KameletRoutesBuilderLoader.java b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/KameletRoutesBuilderLoader.java index 02ef373a4994..5f761047321d 100644 --- a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/KameletRoutesBuilderLoader.java +++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/KameletRoutesBuilderLoader.java @@ -47,11 +47,10 @@ public class KameletRoutesBuilderLoader extends YamlRoutesBuilderLoaderSupport { public static final String EXTENSION = "kamelet.yaml"; /** - * The route property with the output data type of a source Kamelet, which the kamelet producer applies when the - * message is handed to {@code kamelet:sink} (the same key as {@code Kamelet.SOURCE_OUTPUT_TYPE_PROPERTY}). + * The route property with the output data type of a Kamelet, which the kamelet producer applies when the message is + * handed to {@code kamelet:sink} (the same key as {@code Kamelet.OUTPUT_TYPE_PROPERTY}). */ - public static final String SOURCE_OUTPUT_TYPE_PROPERTY = "kameletSourceOutputType"; - private static final String KAMELET_TYPE_LABEL = "camel.apache.org/kamelet.type"; + public static final String OUTPUT_TYPE_PROPERTY = "kameletOutputType"; public KameletRoutesBuilderLoader() { super(EXTENSION); @@ -77,7 +76,7 @@ public class KameletRoutesBuilderLoader extends YamlRoutesBuilderLoaderSupport { // process data types (inputType/outputType) if defined Node dataTypesNode = nodeAt(root, "/spec/dataTypes"); if (dataTypesNode != null) { - configureDataTypes(asMappingNode(dataTypesNode), rtd, isSource(root)); + configureDataTypes(asMappingNode(dataTypesNode), rtd); } Node properties = nodeAt(root, "/spec/definition/properties"); @@ -114,22 +113,7 @@ public class KameletRoutesBuilderLoader extends YamlRoutesBuilderLoaderSupport { }; } - /** - * Whether the Kamelet is a source, as its {@code camel.apache.org/kamelet.type} label says. - */ - private static boolean isSource(Node root) { - MappingNode labels = asMappingNode(nodeAt(root, "/metadata/labels")); - if (labels != null) { - for (NodeTuple label : labels.getValue()) { - if (KAMELET_TYPE_LABEL.equals(asText(label.getKeyNode()))) { - return "source".equals(asText(label.getValueNode())); - } - } - } - return false; - } - - private void configureDataTypes(MappingNode dataTypes, RouteTemplateDefinition rtd, boolean source) { + private void configureDataTypes(MappingNode dataTypes, RouteTemplateDefinition rtd) { MappingNode in = asMappingNode(nodeAt(dataTypes, "/in")); if (in != null) { String dataType = extractKameletDataType(in); @@ -141,14 +125,10 @@ public class KameletRoutesBuilderLoader extends YamlRoutesBuilderLoaderSupport { if (out != null) { String dataType = extractKameletDataType(out); if (dataType != null) { - if (source) { - // a source hands each message on with kamelet:sink, which runs the consuming route inside the - // template route, so an output type of the route would be applied only after the consuming route: - // the kamelet producer applies it when the message is handed to kamelet:sink instead - rtd.route().routeProperty(SOURCE_OUTPUT_TYPE_PROPERTY, dataType); - } else { - rtd.route().outputType(dataType); - } + // a Kamelet hands each message on with kamelet:sink, which runs the consuming route of a source, or + // the steps after a Kamelet EIP, inside the template route, so an output type of the route would be + // applied only after them: the kamelet producer applies it when the message is handed to kamelet:sink + rtd.route().routeProperty(OUTPUT_TYPE_PROPERTY, dataType); } } } diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/KameletLoaderTest.java b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/KameletLoaderTest.java index e4e40757d293..5127ce381f8b 100644 --- a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/KameletLoaderTest.java +++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/KameletLoaderTest.java @@ -16,12 +16,18 @@ */ package org.apache.camel.dsl.yaml; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + import org.apache.camel.Exchange; +import org.apache.camel.Message; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.dsl.yaml.support.YamlTestSupport; import org.apache.camel.model.BeanFactoryDefinition; import org.apache.camel.model.RouteTemplateDefinition; import org.apache.camel.model.ToDefinition; +import org.apache.camel.spi.DataType; +import org.apache.camel.spi.Transformer; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -369,7 +375,7 @@ class KameletLoaderTest extends YamlTestSupport { RouteTemplateDefinition rtd = context.getRouteTemplateDefinitions().get(0); assertThat(rtd.getId()).isEqualTo("my-action"); assertThat(rtd.getRoute().getInputType().getUrn()).isEqualTo("my-component:application-json"); - assertThat(rtd.getRoute().getOutputType().getUrn()).isEqualTo("application-octet-stream"); + assertOutputTypeProperty(rtd, "application-octet-stream"); } @Test @@ -399,7 +405,7 @@ class KameletLoaderTest extends YamlTestSupport { RouteTemplateDefinition rtd = context.getRouteTemplateDefinitions().get(0); assertThat(rtd.getId()).isEqualTo("my-source"); assertThat(rtd.getRoute().getInputType()).isNull(); - assertThat(rtd.getRoute().getOutputType().getUrn()).isEqualTo("application-octet-stream"); + assertOutputTypeProperty(rtd, "application-octet-stream"); } @Test @@ -427,11 +433,173 @@ class KameletLoaderTest extends YamlTestSupport { - to: "kamelet:sink" """); - RouteTemplateDefinition rtd = context.getRouteTemplateDefinitions().get(0); + assertOutputTypeProperty(context.getRouteTemplateDefinitions().get(0), "application-octet-stream"); + } + + private static void assertOutputTypeProperty(RouteTemplateDefinition rtd, String type) { + // not an output type of the route: the kamelet producer applies it when the message is handed to kamelet:sink assertThat(rtd.getRoute().getOutputType()).isNull(); assertThat(rtd.getRoute().getRouteProperties()) - .anyMatch(p -> KameletRoutesBuilderLoader.SOURCE_OUTPUT_TYPE_PROPERTY.equals(p.getKey()) - && "application-octet-stream".equals(p.getValue())); + .anyMatch(p -> KameletRoutesBuilderLoader.OUTPUT_TYPE_PROPERTY.equals(p.getKey()) + && type.equals(p.getValue())); + } + + @Test + void actionKameletOutputTypeIsAppliedBeforeTheStepsAfterTheEip() throws Exception { + loadKamelets(BINARY_ACTION); + // the steps after the EIP get the output type, then leave a body the type could not be converted to + loadRoutes(""" + - from: + uri: "direct:eip" + steps: + - kamelet: + name: my-binary-action + - to: "mock:after" + - setBody: + groovy: "[a: 1]" + - to: "mock:last" + """); + + withMock("mock:after", mock -> { + mock.expectedMessageCount(1); + mock.expectedHeaderReceived("Content-Type", "application/octet-stream"); + mock.message(0).body().isInstanceOf(byte[].class); + }); + withMock("mock:last", mock -> mock.expectedMessageCount(1)); + + context.start(); + + // the output type is no longer applied when the route of the Kamelet completes, so the Map does not fail + Exchange out = context.createProducerTemplate().send("direct:eip", e -> e.getMessage().setBody("hello")); + assertThat(out.getException()).isNull(); + assertThat(out.getMessage().getBody()).isInstanceOf(Map.class); + + MockEndpoint.assertIsSatisfied(context); + } + + @Test + void actionKameletOutputTypeIsAppliedOnceWithTo() throws Exception { + loadKamelets(BINARY_ACTION); + loadRoutes(""" + - from: + uri: "direct:to" + steps: + - to: "kamelet:my-binary-action" + - to: "mock:after" + """); + + withMock("mock:after", mock -> { + mock.expectedMessageCount(1); + mock.expectedHeaderReceived("Content-Type", "application/octet-stream"); + mock.message(0).body().isInstanceOf(byte[].class); + }); + + context.start(); + + Exchange out = context.createProducerTemplate().send("direct:to", e -> e.getMessage().setBody("hello")); + assertThat(out.getException()).isNull(); + + MockEndpoint.assertIsSatisfied(context); + } + + @Test + void actionKameletOutputTypeIsAppliedOnceInBothForms() throws Exception { + CountingTransformer counting = new CountingTransformer(); + context.getRegistry().bind("my-counting", counting); + loadKamelets(""" + apiVersion: camel.apache.org/v1 + kind: Kamelet + metadata: + name: my-counting-action + labels: + camel.apache.org/kamelet.type: action + spec: + definition: + title: "My Action" + dataTypes: + out: + default: counting + types: + counting: + format: my-counting + template: + from: + uri: "kamelet:source" + steps: + - setHeader: + name: action + constant: "yes" + """); + loadRoutes(""" + - from: + uri: "direct:eip" + steps: + - kamelet: + name: my-counting-action + - to: "mock:eip" + - from: + uri: "direct:to" + steps: + - to: "kamelet:my-counting-action" + - to: "mock:to" + """); + + withMock("mock:eip", mock -> mock.expectedBodiesReceived("HELLO")); + withMock("mock:to", mock -> mock.expectedBodiesReceived("WORLD")); + + context.start(); + + Exchange out = context.createProducerTemplate().send("direct:eip", e -> e.getMessage().setBody("hello")); + assertThat(out.getException()).isNull(); + assertThat(counting.count).hasValue(1); + + out = context.createProducerTemplate().send("direct:to", e -> e.getMessage().setBody("world")); + assertThat(out.getException()).isNull(); + assertThat(counting.count).hasValue(2); + + MockEndpoint.assertIsSatisfied(context); + } + + private static final String BINARY_ACTION = """ + apiVersion: camel.apache.org/v1 + kind: Kamelet + metadata: + name: my-binary-action + labels: + camel.apache.org/kamelet.type: action + spec: + definition: + title: "My Action" + dataTypes: + out: + default: binary + types: + binary: + format: application-octet-stream + template: + from: + uri: "kamelet:source" + steps: + - setHeader: + name: action + constant: "yes" + """; + + /** + * Upper cases the body and counts how often it is applied. + */ + private static final class CountingTransformer extends Transformer { + private final AtomicInteger count = new AtomicInteger(); + + CountingTransformer() { + setName("my-counting"); + } + + @Override + public void transform(Message message, DataType from, DataType to) { + count.incrementAndGet(); + message.setBody(message.getBody(String.class).toUpperCase()); + } } @Test
