gnodet-bot commented on code in PR #28042:
URL: https://github.com/apache/camel/pull/28042#discussion_r4306123289


##########
components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java:
##########
@@ -681,11 +684,31 @@ private static Object lookupResult(
             throw new IndexOutOfBoundsException(
                     "Key: " + key + " not found in bean: " + bean + " of type: 
"
                                                 + 
ObjectHelper.classCanonicalName(bean) + " using OGNL path [" + ognlPath
-                                                + "]");
+                                                + "]" + keyHint(result != null 
? result : bean, key));
         } else {
             // null safe so we can return null
             return null;
         }
     }
 
+    /**
+     * What to do when a key is read from a value that has no keys 
(CAMEL-25746): the message body as a stream or bytes
+     * (a request body before it is unmarshalled, or a variable set from it), 
JSON text, or a list.
+     */
+    static String keyHint(Object value, String key) {
+        if (value instanceof StreamCache || value instanceof InputStream || 
value instanceof byte[]) {
+            return " (the value is the message body as a stream, not a Map: 
unmarshal it first, for example with json,"
+                   + " then read the key; a variable keeps the value it had 
when it was set)";
+        }
+        if (value instanceof CharSequence text) {
+            String trimmed = text.toString().strip();
+            if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
+                return " (the value is JSON text, not a Map: unmarshal it 
first, for example with json, then read the key)";
+            }
+        }
+        if (value instanceof Collection || value instanceof Object[]) {
+            return " (a List has no keys: pick an element first, such as [0][" 
+ key + "], or split the list)";

Review Comment:
   💡 **Minor inconsistency:** `keyHint()` says "a List" for any `Collection` 
(including `Set`, `Queue`) and for `Object[]`, while `noResponseBody()` in the 
same PR correctly distinguishes `Map` / `List` / `Collection` with its type 
label.
   
   Suggestion for consistency:
   
   ```suggestion
               String label = value instanceof List ? "List" : value instanceof 
Object[] ? "array" : "Collection";
               return " (a " + label + " has no keys: pick an element first, 
such as [0][" + key + "], or split the list)";
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to