davsclaus commented on code in PR #26757:
URL: https://github.com/apache/camel/pull/26757#discussion_r4079655261
##########
components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java:
##########
@@ -535,6 +542,40 @@ exchange, describeBean(holder, beanName, exchange),
return newResult;
}
+ /** Says that the bean is not a Map with that key, as null is a value a
key can hold. */
+ private static final Object NO_SUCH_KEY = new Object();
+
+ /**
+ * The value of the key on a Map bean when the method of that name does
not exist, so that ${body.sku} reads the sku
+ * of a map the way ${body[sku]} does - what a map means in jq, JavaScript
and Groovy too (CAMEL-24916).
+ * <p/>
+ * A method still wins: ${body.size} on a Map calls size() as before. A
name that is not a key still fails, so a
+ * misspelled field is still reported.
+ *
+ * @return the value of the key, or {@link #NO_SUCH_KEY} when this is not
that case
Review Comment:
Added the four @param tags.
##########
core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleSyntaxHintsTest.java:
##########
@@ -144,16 +144,42 @@ public void testOgnlRuntimeMessages() {
}
@Test
- public void testOgnlDotOnAMapSaysToUseAKey() {
+ public void testOgnlDotOnAMapReadsTheKey() {
+ // CAMEL-24916: a map has no method type, so the key is what the dot
can mean
exchange.getIn().setBody(new
java.util.LinkedHashMap<>(java.util.Map.of("type", "order")));
- Exception e = assertThrows(Exception.class,
- () ->
context.resolveLanguage("simple").createExpression("${body.type}").evaluate(exchange,
- String.class));
- assertThat(e.getMessage()).contains("the value is a Map: a key is read
with [type], as in ${body[type]}");
+ assertEquals("order",
context.resolveLanguage("simple").createExpression("${body.type}").evaluate(exchange,
+ String.class));
assertEquals("order",
context.resolveLanguage("simple").createExpression("${body[type]}").evaluate(exchange,
String.class));
}
+ @Test
+ public void testOgnlDotOnAMapWithoutThatKeySaysToUseAKey() {
+ exchange.getIn().setBody(new
java.util.LinkedHashMap<>(java.util.Map.of("type", "order")));
+ Exception e = assertThrows(Exception.class,
+ () ->
context.resolveLanguage("simple").createExpression("${body.typo}").evaluate(exchange,
+ String.class));
+ assertThat(e.getMessage()).contains("the value is a Map: a key is read
with [typo], as in ${body[typo]}");
+ }
+
+ @Test
+ public void testAMethodOfAMapStillWins() {
+ exchange.getIn().setBody(new
java.util.LinkedHashMap<>(java.util.Map.of("size", "not the size")));
+ assertEquals("1",
context.resolveLanguage("simple").createExpression("${body.size}").evaluate(exchange,
+ String.class), "size() is a method of Map, so it still answers
before the key");
+ }
+
+ @Test
+ public void testOgnlDotOnANestedMapReadsTheKey() {
+ java.util.Map<String, Object> item = new java.util.LinkedHashMap<>();
+ item.put("sku", "CAMEL-MUG");
+ java.util.Map<String, Object> body = new java.util.LinkedHashMap<>();
+ body.put("item", item);
+ exchange.getIn().setBody(body);
+ assertEquals("CAMEL-MUG",
context.resolveLanguage("simple").createExpression("${body.item.sku}")
+ .evaluate(exchange, String.class));
+ }
Review Comment:
Good catch - that is the case the sentinel exists for, and it had no test.
Added `testOgnlDotOnAMapWithANullValueAnswersNull`: a map with `sku` mapped to
null answers null instead of throwing, which is what tells "the key is there
and holds null" apart from "there is no such key".
--
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]