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 aba286cf7209 CAMEL-27154: camel-yaml-dsl-validator - a field read on a 
body of bytes is reported, as on a body of text (#30328)
aba286cf7209 is described below

commit aba286cf720914ed2fd965dc9e841e7238d0033b
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Nov 18 13:21:36 2026 +0100

    CAMEL-27154: camel-yaml-dsl-validator - a field read on a body of bytes is 
reported, as on a body of text (#30328)
    
    The validator reports a field read on a body that is not parsed yet 
(CAMEL-24844), with what a
    running app saw at an endpoint where the catalog does not know 
(CAMEL-25750). Two things kept it
    quiet on a byte[]:
    
    - It took bytes for text, so names that text has, such as lines, were let 
through. A byte[] has
      none of them.
    - A jms, sjms or amqp consumer written as uri: jms with destinationType and 
destinationName was
      known as jms:stock, while the YAML DSL builds it, and the running app 
names it, jms:queue:stock.
      So what the running app saw there was never found.
    
    In the local-model benchmark (connect-service-jms-request-reply step 3, r32 
to r35), the stock
    service gets the order as a byte[] from JMS, and in 8 of 21 runs a Groovy 
expression read
    body.lines and failed with "No such property: lines for class: [B".
    
    The validator now names such an endpoint as the YAML DSL does, knows a body 
of bytes (what the
    running app saw, or a consumer the catalog says gives only bytes), and on 
bytes reports the names
    of text as field reads: "..., but the body here is still bytes (a byte[]), 
not parsed data - ...".
    
    Claude-Session: https://claude.ai/code/session_016Y4dDi5iQNgtHrDdz2NpwF
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../camel/dsl/yaml/validator/RouteGraph.java       |   9 +-
 .../camel/dsl/yaml/validator/TextBodyFlow.java     |  72 ++++++++---
 .../camel/dsl/yaml/validator/TextBodyFlowTest.java | 133 +++++++++++++++++++++
 3 files changed, 196 insertions(+), 18 deletions(-)

diff --git 
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/RouteGraph.java
 
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/RouteGraph.java
index 618f8563c59a..5d47b8636e70 100644
--- 
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/RouteGraph.java
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/RouteGraph.java
@@ -105,7 +105,14 @@ final class RouteGraph {
         }
         for (String key : new String[] { "name", "destinationName", "topic", 
"queue", "path", "address" }) {
             if (parameters.has(key) && parameters.get(key).isValueNode()) {
-                return uri + ":" + parameters.get(key).asString("");
+                String path = parameters.get(key).asString("");
+                // jms, sjms and amqp: the path is 
destinationType:destinationName, as the YAML DSL builds the uri, and
+                // as a running app names the endpoint (jms:queue:stock)
+                JsonNode type = parameters.get("destinationType");
+                if ("destinationName".equals(key) && type != null && 
type.isValueNode()) {
+                    path = type.asString("") + ":" + path;
+                }
+                return uri + ":" + path;
             }
         }
         return uri;
diff --git 
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/TextBodyFlow.java
 
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/TextBodyFlow.java
index fb18d83a8abc..914684b59469 100644
--- 
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/TextBodyFlow.java
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/TextBodyFlow.java
@@ -85,6 +85,13 @@ final class TextBodyFlow {
     private static final Set<String> TEXT_PROPERTIES = Set.of("bytes", "text", 
"class", "empty", "blank", "length",
             "size", "toString", "trim", "lines", "chars", "hashCode");
 
+    /**
+     * What can be read from a byte[] without parsing it: its length or size, 
whether it is empty, and what any object
+     * has. Not the properties of text, such as lines, text or trim 
(CAMEL-27154).
+     */
+    private static final Set<String> BYTES_PROPERTIES = Set.of("class", 
"length", "size", "empty", "toString",
+            "hashCode");
+
     /** What the file consumer's GenericFile has besides: ${body.fileName} 
reads the file, not its content. */
     private static final Set<String> FILE_PROPERTIES = Set.of("file", 
"fileName", "fileNameOnly", "fileLength",
             "lastModified", "charset", "extendedAttributes", "body", "parent", 
"absoluteFilePath", "relativeFilePath",
@@ -235,14 +242,19 @@ final class TextBodyFlow {
     /**
      * Text: the payload as it was read or written; marshalled when a marshal 
wrote it from parsed data; the resource it
      * was loaded from (file:orders.json) when a setBody with constant: 
resource: loaded it; emptied when a setBody set
-     * it to null or to empty text, which has no field to read; and when a 
pollEnrich polled it, the data that was the
-     * body before it.
+     * it to null or to empty text, which has no field to read; when a 
pollEnrich polled it, the data that was the body
+     * before it; and bytes when it is known to be a byte[], which has none of 
the properties of text (CAMEL-27154).
      */
     record Text(String origin, String format, boolean file, boolean 
marshalled, String resource, Emptied emptied,
-            Replaced replaced)
+            Replaced replaced, boolean bytes)
             implements
                 Body {
 
+        Text(String origin, String format, boolean file, boolean marshalled, 
String resource, Emptied emptied,
+             Replaced replaced) {
+            this(origin, format, file, marshalled, resource, emptied, 
replaced, false);
+        }
+
         Text(String origin, String format) {
             this(origin, format, false, false, null, null, null);
         }
@@ -265,6 +277,9 @@ final class TextBodyFlow {
 
         /** The names that can be read from this body without parsing it. */
         Set<String> readable() {
+            if (bytes) {
+                return BYTES_PROPERTIES;
+            }
             if (!file) {
                 return TEXT_PROPERTIES;
             }
@@ -459,7 +474,9 @@ final class TextBodyFlow {
             // the file as read has the format of the files it reads, and the 
data of the files in its directory
             return out.file()
                     ? new Text(consumer, fileFormat(from), true, false, 
directoryKey(uri, from))
-                    : new Text(consumer + " as " + out.inWords(), 
out.dataFormat());
+                    : new Text(
+                            consumer + " as " + out.inWords(), 
out.dataFormat(), false, false, null, null, null,
+                            onlyBytes(out));
         }
         return out.data() ? new Parsed(consumer + " as " + out.inWords(), 
out.dataFormat()) : null;
     }
@@ -503,7 +520,9 @@ final class TextBodyFlow {
             return null;
         }
         if (out.payload()) {
-            return new Text(origin + " as " + out.inWords(), out.dataFormat(), 
out.file());
+            return new Text(
+                    origin + " as " + out.inWords(), out.dataFormat(), 
out.file(), false, null, null, null,
+                    onlyBytes(out));
         }
         return out.data() ? new Parsed(origin + " as " + out.inWords(), 
out.dataFormat()) : null;
     }
@@ -692,8 +711,13 @@ final class TextBodyFlow {
             case "convertBodyTo" -> {
                 String type = value != null && value.isObject() && 
value.has("type") ? value.get("type").asString("")
                         : value != null && value.isString() ? value.asString() 
: "";
-                return "String".equals(type) || 
"java.lang.String".equals(type) || "byte[]".equals(type)
-                        ? new Text("convertBodyTo " + type + " makes it text", 
body != null ? body.format() : null)
+                String format = body != null ? body.format() : null;
+                if ("byte[]".equals(type)) {
+                    // a byte[] has none of the properties of text, such as 
lines or trim (CAMEL-27154)
+                    return new Text("convertBodyTo byte[] makes it bytes", 
format, false, false, null, null, null, true);
+                }
+                return "String".equals(type) || "java.lang.String".equals(type)
+                        ? new Text("convertBodyTo " + type + " makes it text", 
format)
                         : null;
             }
             case "transformDataType" -> {
@@ -718,7 +742,7 @@ final class TextBodyFlow {
                 return polled instanceof Text t && body instanceof Parsed data
                         ? new Text(
                                 t.origin(), t.format(), t.file(), 
t.marshalled(), t.resource(), t.emptied(),
-                                new Replaced(data, name, 
variableFor(t.resource())))
+                                new Replaced(data, name, 
variableFor(t.resource())), t.bytes())
                         : polled;
             }
             case "kamelet" -> {
@@ -768,9 +792,10 @@ final class TextBodyFlow {
         Body end = walk(steps, body, new NodePath(PathType.JSON_POINTER), 
w.called());
         w.seen().remove(normalize(uri));
         if (end instanceof Text t) {
+            // a byte[] stays bytes, which has none of the properties of text 
(CAMEL-27154)
             return new Text(
-                    "to: " + normalize(uri) + " returns it as text (" + 
t.origin() + ")", t.format(),
-                    t.file());
+                    "to: " + normalize(uri) + " returns it as " + (t.bytes() ? 
"bytes" : "text") + " (" + t.origin() + ")",
+                    t.format(), t.file(), false, null, null, null, t.bytes());
         } else if (end instanceof Parsed p) {
             return new Parsed("to: " + normalize(uri) + " returns it as data 
(" + p.origin() + ")", p.format());
         }
@@ -789,12 +814,19 @@ final class TextBodyFlow {
         }
         String origin = "in the running app, " + what + " " + 
inWords(seen.types());
         return switch (seen.kinds().get(0)) {
-            case "payload" -> new Text(origin, null, 
seen.types().stream().anyMatch(TextBodyFlow::isFile));
+            case "payload" -> new Text(
+                    origin, null, 
seen.types().stream().anyMatch(TextBodyFlow::isFile), false, null,
+                    null, null, 
seen.types().stream().allMatch("byte[]"::equals));
             case "data", "collection" -> new Parsed(origin, null);
             default -> null;
         };
     }
 
+    /** Whether the catalog says the body is bytes, and nothing else but no 
body: a byte[] (CAMEL-27154). */
+    private static boolean onlyBytes(CatalogBodyTypes.Answer out) {
+        return out.carriers().contains("bytes") && 
out.carriers().stream().allMatch(c -> "bytes".equals(c) || "null".equals(c));
+    }
+
     /** Whether a body type is the file as a file consumer reads it. */
     private static boolean isFile(String type) {
         String name = simpleName(type);
@@ -1837,13 +1869,21 @@ final class TextBodyFlow {
                 .messageKey("type")
                 .format(new MessageFormat("{0}"))
                 .arguments((routeId != null ? "route " + routeId + ": " : "") 
+ read
-                           + (text.file()
-                                   ? ", but the body here is the file as it 
was read (a GenericFile), not parsed data - "
-                                   : ", but the body here is still text, not 
parsed data - ")
+                           + unparsed(text)
                            + text.origin() + ": " + fix)
                 .build();
     }
 
+    /** The body in a report of a field read on it: the file as it was read, 
bytes or text, none of them parsed. */
+    private static String unparsed(Text text) {
+        if (text.file()) {
+            return ", but the body here is the file as it was read (a 
GenericFile), not parsed data - ";
+        }
+        return text.bytes()
+                ? ", but the body here is still bytes (a byte[]), not parsed 
data - "
+                : ", but the body here is still text, not parsed data - ";
+    }
+
     private static Error error(NodePath at, String routeId, String read, Text 
text) {
         if (text.emptied() != null) {
             return Error.builder()
@@ -1885,9 +1925,7 @@ final class TextBodyFlow {
                 .messageKey("type")
                 .format(new MessageFormat("{0}"))
                 .arguments((routeId != null ? "route " + routeId + ": " : "") 
+ read
-                           + (text.file()
-                                   ? ", but the body here is the file as it 
was read (a GenericFile), not parsed data - "
-                                   : ", but the body here is still text, not 
parsed data - ")
+                           + unparsed(text)
                            + text.origin() + ": " + fix)
                 .build();
     }
diff --git 
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/test/java/org/apache/camel/dsl/yaml/validator/TextBodyFlowTest.java
 
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/test/java/org/apache/camel/dsl/yaml/validator/TextBodyFlowTest.java
index b6b7efb3a36f..9bc8cfccee8b 100644
--- 
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/test/java/org/apache/camel/dsl/yaml/validator/TextBodyFlowTest.java
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/test/java/org/apache/camel/dsl/yaml/validator/TextBodyFlowTest.java
@@ -825,6 +825,139 @@ public class TextBodyFlowTest {
         });
     }
 
+    @Test
+    public void testAFieldReadOnTheBytesAJmsConsumerGave() {
+        // r32 to r35 (connect-service-jms-request-reply step 3): the stock 
service gets the order as a byte[] from JMS,
+        // and a script read body.lines: "No such property: lines for class: 
[B". lines is a property of text, so the
+        // walk let it through while it took bytes for text (CAMEL-27154)
+        String yaml = """
+                - route:
+                    id: stock-service
+                    from:
+                      uri: jms
+                      parameters:
+                        destinationType: queue
+                        destinationName: stock
+                      steps:
+                        - setBody:
+                            expression:
+                              groovy:
+                                expression: "def orderLines = body.lines; 
orderLines ? 'reserved' : 'empty'"
+                """;
+        assertThat(lines(yaml)).isEmpty();
+
+        ProjectFacts bytes
+                = observed(ProjectFacts.consumerKey("jms:queue:stock"), 
List.of("byte[]"), List.of("payload"));
+        assertThat(lines(yaml, bytes)).singleElement().satisfies(m -> {
+            assertThat(m).contains("groovy reads fields of the body 
(body.lines), but the body here is still bytes"
+                                   + " (a byte[]), not parsed data - in the 
running app, from: jms:queue:stock gave a byte[]");
+            assertThat(m).contains("unmarshal it first with the data format of 
the payload");
+        });
+
+        // text has lines, and the length of the bytes is not a field
+        ProjectFacts text
+                = observed(ProjectFacts.consumerKey("jms:queue:stock"), 
List.of("java.lang.String"), List.of("payload"));
+        assertThat(lines(yaml, text)).isEmpty();
+        assertThat(lines(yaml.replace("body.lines", "body.length"), 
bytes)).isEmpty();
+    }
+
+    @Test
+    public void testAFieldReadAfterConvertBodyToBytes() {
+        // convertBodyTo byte[] makes bytes, not text: lines is not a property 
of a byte[] (CAMEL-27154)
+        String yaml = """
+                - route:
+                    id: bytes
+                    from:
+                      uri: timer:tick
+                      steps:
+                        - setBody:
+                            constant: hello
+                        - convertBodyTo:
+                            type: byte[]
+                        - setBody:
+                            expression:
+                              groovy:
+                                expression: "body.lines.size()"
+                """;
+        assertThat(lines(yaml)).singleElement().satisfies(m -> {
+            assertThat(m).contains("groovy reads fields of the body 
(body.lines), but the body here is still bytes"
+                                   + " (a byte[]), not parsed data - 
convertBodyTo byte[] makes it bytes");
+        });
+
+        // text has lines, and the length of the bytes is not a field
+        assertThat(lines(yaml.replace("type: byte[]", "type: 
String"))).isEmpty();
+        assertThat(lines(yaml.replace("body.lines.size()", 
"body.length"))).isEmpty();
+    }
+
+    @Test
+    public void testAFieldReadOnTheBytesAPollEnrichGave() {
+        // the pollEnrich replaced parsed data with an event, which the 
azure-eventhubs consumer gives as a byte[]: the
+        // bytes stay bytes, with the data they replaced (CAMEL-27154)
+        String yaml = """
+                - route:
+                    id: restock
+                    from:
+                      uri: timer:tick
+                      steps:
+                        - setBody:
+                            constant: "{\\"sku\\": \\"A1\\"}"
+                        - unmarshal:
+                            json: {}
+                        - pollEnrich:
+                            expression:
+                              constant:
+                                expression: azure-eventhubs:shop/stock
+                            timeout: 5000
+                        - setBody:
+                            expression:
+                              groovy:
+                                expression: "body.lines.size()"
+                """;
+        assertThat(lines(yaml)).singleElement().satisfies(m -> {
+            assertThat(m).contains("groovy reads fields of the body 
(body.lines), but the body here is still bytes"
+                                   + " (a byte[]), not parsed data - 
pollEnrich: azure-eventhubs:shop/stock");
+        });
+
+        // a file as it was read has lines, and the length of the bytes is not 
a field
+        assertThat(lines(yaml.replace("azure-eventhubs:shop/stock", 
"file:stock"))).isEmpty();
+        assertThat(lines(yaml.replace("body.lines.size()", 
"body.length"))).isEmpty();
+    }
+
+    @Test
+    public void testTheCalledRouteReturnsBytes() {
+        // a direct: route that ends with convertBodyTo byte[] returns bytes, 
not text (CAMEL-27154)
+        String yaml = """
+                - route:
+                    id: lookup
+                    from:
+                      uri: direct:lookup
+                      steps:
+                        - setBody:
+                            constant: hello
+                        - convertBodyTo:
+                            type: byte[]
+                - route:
+                    id: caller
+                    from:
+                      uri: timer:tick
+                      steps:
+                        - to:
+                            uri: direct:lookup
+                        - setBody:
+                            expression:
+                              groovy:
+                                expression: "body.lines.size()"
+                """;
+        assertThat(lines(yaml)).singleElement().satisfies(m -> {
+            assertThat(m).contains("route caller: groovy reads fields of the 
body (body.lines), but the body here is"
+                                   + " still bytes (a byte[]), not parsed data 
- to: direct:lookup returns it as bytes"
+                                   + " (convertBodyTo byte[] makes it bytes)");
+        });
+
+        // text has lines
+        assertThat(lines(yaml.replace("type: byte[]", "type: 
String"))).isEmpty();
+    }
+
     @Test
     public void testTheBodyAProducerReturnedInTheRunningApp() {
         // a sql call returns a List of rows, which a file write cannot write 
as it is

Reply via email to