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

davsclaus 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 a896dc884567 CAMEL-27258: camel-kamelet - the output data type of a 
source Kamelet is applied before the consuming route (#30453)
a896dc884567 is described below

commit a896dc884567491043544633ec1c7db198643e08
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Nov 19 22:34:27 2026 +0100

    CAMEL-27258: camel-kamelet - the output data type of a source Kamelet is 
applied before the consuming route (#30453)
    
    The YAML loader turned spec.dataTypes.out.default into the output type of
    the template route, which ContractAdvice applies when that route completes.
    A source hands each message on with kamelet:sink, which runs the consuming
    route inside the template route, so the consuming route never saw the type,
    and the exchange failed at the end when it left a body the transformer
    could not convert, such as a Map.
    
    For a Kamelet labelled camel.apache.org/kamelet.type: source, the loader
    now records the type as the route property kameletSourceOutputType, and
    KameletProducer applies it with a DataTypeProcessor when the message is
    handed to kamelet:sink. Sinks and actions are unchanged. When no
    transformer for the type is on the classpath, a Java type is converted
    with the type converter and any other type is handed on unchanged with one
    WARN, as the output type of the route did before.
    
    The binary (application-octet-stream) transformer keeps a stream cache
    body and only sets the Content-Type: source Kamelets such as aws-s3-source
    declare binary for objects that may be large. Any other InputStream, which
    may be readable only once, is still read into a byte[].
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../transformers/application-octet-stream.json     |   5 +-
 .../apache/camel/component/kamelet/Kamelet.java    |   5 +
 .../camel/component/kamelet/KameletProducer.java   |  71 +++++++++
 .../transformer/application-octet-stream.json      |   5 +-
 .../transformer/ByteArrayDataTypeTransformer.java  |  11 +-
 .../BinaryDataTypeOneShotStreamTest.java           |  62 ++++++++
 .../ByteArrayDataTypeTransformerTest.java          |  38 ++++-
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |  21 +++
 .../camel/dsl/yaml/KameletRoutesBuilderLoader.java |  34 +++-
 .../apache/camel/dsl/yaml/KameletLoaderTest.java   | 174 +++++++++++++++++++++
 10 files changed, 409 insertions(+), 17 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/application-octet-stream.json
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/application-octet-stream.json
index a876671279db..441d22a5f079 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/application-octet-stream.json
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/application-octet-stream.json
@@ -21,9 +21,10 @@
     },
     "transformerOut": {
       "carriers": [
-        "bytes"
+        "bytes",
+        "stream"
       ],
-      "javaType": "byte[]"
+      "description": "A stream cache body is kept as it is; any other body 
becomes a byte array"
     }
   }
 }
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 94ad037801eb..c236158da284 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
@@ -66,6 +66,11 @@ public final class Kamelet {
     public static final String PARENT_PROCESSOR_ID = "parentProcessorId";
     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.
+     */
+    public static final String SOURCE_OUTPUT_TYPE_PROPERTY = 
"kameletSourceOutputType";
     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 3ea1fb117c92..61adebc9ee8c 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
@@ -20,12 +20,17 @@ import org.apache.camel.AsyncCallback;
 import org.apache.camel.AsyncProcessor;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
 import org.apache.camel.Route;
 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.ManagementStrategy;
 import org.apache.camel.spi.RouteIdAware;
 import org.apache.camel.support.DefaultAsyncProducer;
 import org.apache.camel.support.ExchangeHelper;
+import org.apache.camel.support.service.ServiceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -45,6 +50,9 @@ 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;
 
     public KameletProducer(KameletEndpoint endpoint, String key) {
         super(endpoint);
@@ -81,6 +89,20 @@ 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;
+                        }
+                    }
                     // when calling a kamelet:sink then lookup any waiting 
processor
                     // from the Kamelet EIP to continue routing
                     AsyncProcessor eip = (AsyncProcessor) 
component.getKameletEip(key);
@@ -126,6 +148,55 @@ 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.
+     */
+    private Processor sourceOutputType(Exchange exchange) {
+        if (!sourceOutputTypeResolved) {
+            synchronized (this) {
+                if (!sourceOutputTypeResolved) {
+                    Route route = ExchangeHelper.getRoute(exchange);
+                    Object type = route != null ? 
route.getProperties().get(Kamelet.SOURCE_OUTPUT_TYPE_PROPERTY) : null;
+                    if (type != null) {
+                        sourceOutputType = createSourceOutputType(route, 
type.toString());
+                    }
+                    sourceOutputTypeResolved = true;
+                }
+            }
+        }
+        return sourceOutputType;
+    }
+
+    private Processor createSourceOutputType(Route route, String type) {
+        DataTypeProcessor processor = new DataTypeProcessor(null, type);
+        processor.setCamelContext(getEndpoint().getCamelContext());
+        try {
+            ServiceHelper.startService(processor);
+            return processor;
+        } catch (IllegalArgumentException e) {
+            // no transformer for the type: as the output type of the route 
did, convert a Java type with the type
+            // converter, and take any other body as already of the type
+            DataType dataType = new DataType(type);
+            if (dataType.isJavaType() && dataType.getName() != null) {
+                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"
+                     + " the classpath, so messages are handed on unchanged: 
{}",
+                    route.getRouteId(), type, e.getMessage());
+            return null;
+        }
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        ServiceHelper.stopService(sourceOutputType);
+        sourceOutputType = null;
+        sourceOutputTypeResolved = false;
+        super.doStop();
+    }
+
     @Override
     public String getRouteId() {
         return routeId;
diff --git 
a/core/camel-core-processor/src/generated/resources/META-INF/services/org/apache/camel/transformer/application-octet-stream.json
 
b/core/camel-core-processor/src/generated/resources/META-INF/services/org/apache/camel/transformer/application-octet-stream.json
index a876671279db..441d22a5f079 100644
--- 
a/core/camel-core-processor/src/generated/resources/META-INF/services/org/apache/camel/transformer/application-octet-stream.json
+++ 
b/core/camel-core-processor/src/generated/resources/META-INF/services/org/apache/camel/transformer/application-octet-stream.json
@@ -21,9 +21,10 @@
     },
     "transformerOut": {
       "carriers": [
-        "bytes"
+        "bytes",
+        "stream"
       ],
-      "javaType": "byte[]"
+      "description": "A stream cache body is kept as it is; any other body 
becomes a byte array"
     }
   }
 }
diff --git 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/transformer/ByteArrayDataTypeTransformer.java
 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/transformer/ByteArrayDataTypeTransformer.java
index cc740012f293..211fb9a13955 100644
--- 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/transformer/ByteArrayDataTypeTransformer.java
+++ 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/transformer/ByteArrayDataTypeTransformer.java
@@ -18,6 +18,7 @@ package org.apache.camel.processor.transformer;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
+import org.apache.camel.StreamCache;
 import org.apache.camel.spi.BodyType;
 import org.apache.camel.spi.BodyType.Carrier;
 import org.apache.camel.spi.BodyTypes;
@@ -27,19 +28,25 @@ import org.apache.camel.spi.Transformer;
 
 /**
  * Generic binary data type uses Camel message body converter mechanism to 
convert content to byte array representation.
+ * A stream cache body is binary already and can be read again, so it is kept 
as it is: a large body, such as an object
+ * read from a bucket, is not read into memory. Any other stream is read into 
a byte array, as it may be read only once.
  */
 @DataTypeTransformer(name = "application-octet-stream")
 @BodyTypes(transformerIn = @BodyType(value = { Carrier.TEXT, Carrier.BYTES, 
Carrier.STREAM, Carrier.FILE },
                                      description = "A body Camel can convert 
to a byte array: a number or a null body"
                                                    + " fails"),
-           transformerOut = @BodyType(value = Carrier.BYTES, javaType = 
"byte[]"))
+           transformerOut = @BodyType(value = { Carrier.BYTES, Carrier.STREAM 
},
+                                      description = "A stream cache body is 
kept as it is; any other body becomes a"
+                                                    + " byte array"))
 public class ByteArrayDataTypeTransformer extends Transformer {
 
     private static final Transformer DELEGATE = new 
TypeConverterTransformer(byte[].class);
 
     @Override
     public void transform(Message message, DataType from, DataType to) throws 
Exception {
-        DELEGATE.transform(message, from, to);
+        if (!(message.getBody() instanceof StreamCache)) {
+            DELEGATE.transform(message, from, to);
+        }
 
         message.setHeader(Exchange.CONTENT_TYPE, "application/octet-stream");
     }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/transformer/BinaryDataTypeOneShotStreamTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/transformer/BinaryDataTypeOneShotStreamTest.java
new file mode 100644
index 000000000000..4649161d1c8f
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/transformer/BinaryDataTypeOneShotStreamTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.processor.transformer;
+
+import java.io.ByteArrayInputStream;
+import java.io.SequenceInputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The binary data type reads a stream that is not a stream cache into a byte 
array, so a stream that can be read only
+ * once is still there for the steps after it.
+ */
+public class BinaryDataTypeOneShotStreamTest extends ContextTestSupport {
+
+    @Test
+    public void oneShotStreamIsStillReadableAfterBinary() throws Exception {
+        getMockEndpoint("mock:out").expectedBodiesReceived("payload");
+        getMockEndpoint("mock:out").expectedHeaderReceived("Content-Type", 
"application/octet-stream");
+
+        template.sendBody("direct:start", "x");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start").noStreamCaching()
+                        // a stream that can be read only once (no 
mark/reset), as a bean or a component can give
+                        .process(e -> e.getMessage().setBody(new 
SequenceInputStream(
+                                Collections.enumeration(
+                                        List.of(new 
ByteArrayInputStream("payload".getBytes(StandardCharsets.UTF_8)))))))
+                        .transformDataType("application-octet-stream")
+                        .log("${body}")
+                        .convertBodyTo(String.class)
+                        .to("mock:out");
+            }
+        };
+    }
+}
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/transformer/ByteArrayDataTypeTransformerTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/transformer/ByteArrayDataTypeTransformerTest.java
index 5374b2a0be96..f76820620bf7 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/processor/transformer/ByteArrayDataTypeTransformerTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/transformer/ByteArrayDataTypeTransformerTest.java
@@ -22,6 +22,8 @@ import java.util.List;
 
 import org.apache.camel.CamelExecutionException;
 import org.apache.camel.Exchange;
+import org.apache.camel.StreamCache;
+import org.apache.camel.converter.stream.ByteArrayInputStreamCache;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.engine.DefaultTransformerRegistry;
 import org.apache.camel.spi.BodyType.Carrier;
@@ -35,6 +37,7 @@ import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class ByteArrayDataTypeTransformerTest {
@@ -80,15 +83,31 @@ public class ByteArrayDataTypeTransformerTest {
     }
 
     @Test
-    void shouldTransformFromInputStreamToBytesModel() throws Exception {
+    void shouldKeepStreamCache() throws Exception {
+        // a stream cache is binary already and can be read again: it is kept, 
so a large body is not read into memory
         Exchange exchange = new DefaultExchange(camelContext);
 
+        StreamCache stream = new ByteArrayInputStreamCache(new 
ByteArrayInputStream("Test3".getBytes(StandardCharsets.UTF_8)));
         exchange.getMessage().setHeader("file", "test3.txt");
-        exchange.getMessage().setBody(new 
ByteArrayInputStream("Test3".getBytes(StandardCharsets.UTF_8)));
+        exchange.getMessage().setBody(stream);
         transformer.transform(exchange.getMessage(), DataType.ANY, 
DataType.ANY);
 
-        Assertions.assertTrue(exchange.getMessage().hasHeaders());
-        assertBinaryBody(exchange, "test3.txt", "Test3");
+        assertSame(stream, exchange.getMessage().getBody());
+        assertEquals("application/octet-stream", 
exchange.getMessage().getHeader(Exchange.CONTENT_TYPE));
+        assertEquals("test3.txt", exchange.getMessage().getHeader("file"));
+    }
+
+    @Test
+    void shouldReadOtherInputStreamToBytes() throws Exception {
+        // a stream that is not a stream cache may be read only once: it 
becomes a byte array every later step can read
+        Exchange exchange = new DefaultExchange(camelContext);
+
+        exchange.getMessage().setHeader("file", "test4.txt");
+        exchange.getMessage().setBody(new 
ByteArrayInputStream("Test4".getBytes(StandardCharsets.UTF_8)));
+        transformer.transform(exchange.getMessage(), DataType.ANY, 
DataType.ANY);
+
+        assertBinaryBody(exchange, "test4.txt", "Test4");
+        assertEquals("application/octet-stream", 
exchange.getMessage().getHeader(Exchange.CONTENT_TYPE));
     }
 
     @Test
@@ -110,15 +129,18 @@ public class ByteArrayDataTypeTransformerTest {
         BodyTypes bodyTypes = 
ByteArrayDataTypeTransformer.class.getAnnotation(BodyTypes.class);
         assertEquals(List.of(Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, 
Carrier.FILE),
                 List.of(bodyTypes.transformerIn().value()));
-        assertEquals(Carrier.BYTES, bodyTypes.transformerOut().value()[0]);
-        assertEquals(byte[].class.getCanonicalName(), 
bodyTypes.transformerOut().javaType());
-        for (Object body : List.of("Test", 
"Test".getBytes(StandardCharsets.UTF_8),
-                new 
ByteArrayInputStream("Test".getBytes(StandardCharsets.UTF_8)))) {
+        assertEquals(List.of(Carrier.BYTES, Carrier.STREAM), 
List.of(bodyTypes.transformerOut().value()));
+        for (Object body : List.of("Test", 
"Test".getBytes(StandardCharsets.UTF_8))) {
             Exchange exchange = new DefaultExchange(camelContext);
             exchange.getMessage().setBody(body);
             transformer.transform(exchange.getMessage(), DataType.ANY, 
DataType.ANY);
             assertInstanceOf(byte[].class, exchange.getMessage().getBody(), 
body.getClass().getName());
         }
+        Exchange stream = new DefaultExchange(camelContext);
+        stream.getMessage()
+                .setBody(new ByteArrayInputStreamCache(new 
ByteArrayInputStream("Test".getBytes(StandardCharsets.UTF_8))));
+        transformer.transform(stream.getMessage(), DataType.ANY, DataType.ANY);
+        assertInstanceOf(ByteArrayInputStreamCache.class, 
stream.getMessage().getBody());
         // a number is not a payload
         Exchange exchange = new DefaultExchange(camelContext);
         exchange.getMessage().setBody(5);
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 e5136556deaf..8940732dceab 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
@@ -207,6 +207,27 @@ 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.
+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.
+
+=== camel-core - the binary data type keeps a stream cache body
+
+The `application-octet-stream` (`binary`) data type transformer keeps a body 
that is a stream cache as it is, and only
+sets the `Content-Type` header to `application/octet-stream`. Before, it read 
the stream into a `byte[]`. A stream cache
+is binary already and can be read again, and a source Kamelet such as 
`aws-s3-source` or `azure-storage-blob-source`
+declares `binary` and gives the object as a stream, which may be large. Any 
other body, including an `InputStream` that is
+not a stream cache (stream caching turned off), is still converted to a 
`byte[]`.
+
 === camel-core-model - doTry keeps doCatch and doFinally in its outputs
 
 `doCatch` and `doFinally` written as properties of `doTry` in the YAML DSL are 
now kept in the outputs of the
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 735695552ea4..02ef373a4994 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
@@ -46,6 +46,12 @@ import static 
org.apache.camel.dsl.yaml.common.YamlDeserializerSupport.setDeseri
 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}).
+     */
+    public static final String SOURCE_OUTPUT_TYPE_PROPERTY = 
"kameletSourceOutputType";
+    private static final String KAMELET_TYPE_LABEL = 
"camel.apache.org/kamelet.type";
 
     public KameletRoutesBuilderLoader() {
         super(EXTENSION);
@@ -71,7 +77,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);
+            configureDataTypes(asMappingNode(dataTypesNode), rtd, 
isSource(root));
         }
 
         Node properties = nodeAt(root, "/spec/definition/properties");
@@ -108,7 +114,22 @@ public class KameletRoutesBuilderLoader extends 
YamlRoutesBuilderLoaderSupport {
         };
     }
 
-    private void configureDataTypes(MappingNode dataTypes, 
RouteTemplateDefinition rtd) {
+    /**
+     * 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) {
         MappingNode in = asMappingNode(nodeAt(dataTypes, "/in"));
         if (in != null) {
             String dataType = extractKameletDataType(in);
@@ -120,7 +141,14 @@ public class KameletRoutesBuilderLoader extends 
YamlRoutesBuilderLoaderSupport {
         if (out != null) {
             String dataType = extractKameletDataType(out);
             if (dataType != null) {
-                rtd.route().outputType(dataType);
+                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);
+                }
             }
         }
     }
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 e5a3bdd49ee2..e4e40757d293 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,6 +16,7 @@
  */
 package org.apache.camel.dsl.yaml;
 
+import org.apache.camel.Exchange;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.dsl.yaml.support.YamlTestSupport;
 import org.apache.camel.model.BeanFactoryDefinition;
@@ -401,6 +402,179 @@ class KameletLoaderTest extends YamlTestSupport {
         
assertThat(rtd.getRoute().getOutputType().getUrn()).isEqualTo("application-octet-stream");
     }
 
+    @Test
+    void sourceKameletOutputTypeIsNotAnOutputTypeOfTheRoute() throws Exception 
{
+        loadKamelets("""
+                apiVersion: camel.apache.org/v1
+                kind: Kamelet
+                metadata:
+                  name: my-labelled-source
+                  labels:
+                    camel.apache.org/kamelet.type: source
+                spec:
+                  definition:
+                    title: "My Source"
+                  dataTypes:
+                    out:
+                      default: binary
+                      types:
+                        binary:
+                          format: application-octet-stream
+                  template:
+                    from:
+                      uri: "direct:feed"
+                      steps:
+                        - to: "kamelet:sink"
+                """);
+
+        RouteTemplateDefinition rtd = 
context.getRouteTemplateDefinitions().get(0);
+        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()));
+    }
+
+    @Test
+    void sourceKameletOutputTypeIsAppliedBeforeTheConsumingRoute() throws 
Exception {
+        loadKamelets("""
+                apiVersion: camel.apache.org/v1
+                kind: Kamelet
+                metadata:
+                  name: my-binary-source
+                  labels:
+                    camel.apache.org/kamelet.type: source
+                spec:
+                  definition:
+                    title: "My Source"
+                  dataTypes:
+                    out:
+                      default: binary
+                      types:
+                        binary:
+                          format: application-octet-stream
+                  template:
+                    from:
+                      uri: "direct:feed"
+                      steps:
+                        - to: "kamelet:sink"
+                """);
+
+        // the consuming route gets the output type, then leaves a body the 
type could not be converted to
+        loadRoutes("""
+                - from:
+                    uri: "kamelet:my-binary-source"
+                    steps:
+                      - to: "mock:first"
+                      - setBody:
+                          groovy: "[a: 1]"
+                      - to: "mock:last"
+                """);
+
+        withMock("mock:first", 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:feed", e 
-> e.getMessage().setBody("hello"));
+        assertThat(out.getException()).isNull();
+
+        MockEndpoint.assertIsSatisfied(context);
+    }
+
+    @Test
+    void sourceKameletOutputTypeWithoutTransformerHandsMessagesOnUnchanged() 
throws Exception {
+        // the transformer of the format is not on the classpath (camel-slack 
is not a test dependency)
+        loadKamelets("""
+                apiVersion: camel.apache.org/v1
+                kind: Kamelet
+                metadata:
+                  name: my-ce-source
+                  labels:
+                    camel.apache.org/kamelet.type: source
+                spec:
+                  definition:
+                    title: "My Source"
+                  dataTypes:
+                    out:
+                      default: cloudevents
+                      types:
+                        cloudevents:
+                          format: "slack:application-cloudevents"
+                  template:
+                    from:
+                      uri: "direct:feed"
+                      steps:
+                        - to: "kamelet:sink"
+                """);
+        loadRoutes("""
+                - from:
+                    uri: "kamelet:my-ce-source"
+                    steps:
+                      - to: "mock:ce"
+                """);
+
+        withMock("mock:ce", mock -> mock.expectedBodiesReceived("hello", 
"hello"));
+
+        context.start();
+
+        for (int i = 0; i < 2; i++) {
+            Exchange out = 
context.createProducerTemplate().send("direct:feed", e -> 
e.getMessage().setBody("hello"));
+            assertThat(out.getException()).isNull();
+        }
+
+        MockEndpoint.assertIsSatisfied(context);
+    }
+
+    @Test
+    void sourceKameletJavaOutputTypeIsConverted() throws Exception {
+        // the transformer registry has no transformer for a Java type: the 
type converter converts the body
+        loadKamelets("""
+                apiVersion: camel.apache.org/v1
+                kind: Kamelet
+                metadata:
+                  name: my-string-source
+                  labels:
+                    camel.apache.org/kamelet.type: source
+                spec:
+                  definition:
+                    title: "My Source"
+                  dataTypes:
+                    out:
+                      default: text
+                      types:
+                        text:
+                          format: "java:java.lang.String"
+                  template:
+                    from:
+                      uri: "direct:feed"
+                      steps:
+                        - to: "kamelet:sink"
+                """);
+        loadRoutes("""
+                - from:
+                    uri: "kamelet:my-string-source"
+                    steps:
+                      - to: "mock:text"
+                """);
+
+        withMock("mock:text", mock -> {
+            mock.expectedBodiesReceived("42");
+            mock.message(0).body().isInstanceOf(String.class);
+        });
+
+        context.start();
+
+        Exchange out = context.createProducerTemplate().send("direct:feed", e 
-> e.getMessage().setBody(42));
+        assertThat(out.getException()).isNull();
+
+        MockEndpoint.assertIsSatisfied(context);
+    }
+
     @Test
     void kameletWithLocalBeanViaBeanRef() throws Exception {
         loadKamelets("""

Reply via email to