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 2a17afc161e5 CAMEL-27146: camel-jbang - the AI write check for a
returned resource: text reads only the script, not the other keys of the
language (#30318)
2a17afc161e5 is described below
commit 2a17afc161e51f185b08115af437d3c783179b36
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Nov 18 10:01:50 2026 +0100
CAMEL-27146: camel-jbang - the AI write check for a returned resource: text
reads only the script, not the other keys of the language (#30318)
The write tools refuse a script (groovy, jq, ...) that returns a text
starting with resource:
(CAMEL-24882). The check read every line under the language key, so a
resource: value under
another key looked like a script that returns it. A model that wrote the
wrong key
groovy: {resource: "resource:file:x.groovy"} got the schema message (the
text goes in
expression:) and also "the script returns the text ... add resolveResource:
true and return the
name without a scheme". It followed the second message, wrote the file name
as the script with
resolveResource: true, and groovy ran the name as code (No such property).
In the local-model
benchmark (r30 to r35), 31 of the 34 refusals with that message were for a
wrong key under groovy.
The check now reads only the script: the value of the language key, or of
its expression: key
(also a block scalar, or the entry of a flow mapping). The flow form that
the schema message
gives, groovy: {expression: "resource:file:x.groovy"}, gets no false
message either.
Claude-Session: https://claude.ai/code/session_016Y4dDi5iQNgtHrDdz2NpwF
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../dsl/jbang/core/commands/ai/BeanRefChecks.java | 43 ++++++++--
.../ai/SourceValidatorResourceLiteralTest.java | 95 +++++++++++++++++++++-
2 files changed, 131 insertions(+), 7 deletions(-)
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/BeanRefChecks.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/BeanRefChecks.java
index d69e2b33f4ee..68568e512672 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/BeanRefChecks.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/BeanRefChecks.java
@@ -618,12 +618,19 @@ final class BeanRefChecks {
/** A quoted resource:... literal inside a script: the script returns that
text. */
private static final Pattern QUOTED_RESOURCE_LITERAL
=
Pattern.compile("['\"](resource:(?:classpath:|file:|ref:|bean:)?[^'\"\\\\\\s]+)['\"]");
+ /** The expression: entry of a flow mapping, groovy: {expression: "...",
resolveResource: true}. */
+ private static final Pattern FLOW_EXPRESSION
+ =
Pattern.compile("[{,]\\s*expression\\s*:\\s*(\"(?:[^\"\\\\]|\\\\.)*\"|'(?:[^']|'')*'|[^,}]*)");
+ private static final Pattern FLOW_RESOLVE_RESOURCE =
Pattern.compile("[{,]\\s*resolveResource\\s*:\\s*true\\b");
/**
* A script (groovy, jq, ...) that returns 'resource:file:x' returns that
text: the resource: prefix is resolved on
* the expression text, never on a returned value, so the next step gets
24 characters instead of the file
* (CAMEL-24882). Says to add resolveResource: true (CAMEL-24884) and
return the name without a scheme, or to use
* constant/simple. With resolveResource: true, a returned name with a
scheme fails (CAMEL-25982): says to drop it.
+ * Only the script is read: the value of the language key, or of its
expression: key. A resource: value under
+ * another key, such as the wrong key resource:, is not a script that
returns it: the schema check names that key
+ * (CAMEL-27146).
*/
static List<String> validateReturnedResourceLiterals(String content) {
List<String> errors = new ArrayList<>();
@@ -645,23 +652,47 @@ final class BeanRefChecks {
continue;
}
int indent = YamlLines.countLeadingSpaces(lines[i]);
- // the expression block: the inline value, and the lines indented
under the language key
- StringBuilder block = new
StringBuilder(stripYamlQuotes(trimmed.substring(colon +
1).trim())).append('\n');
+ String inline = trimmed.substring(colon + 1).trim();
+ // the script: the inline value (a block scalar goes on in the
lines under the key), a flow mapping's
+ // expression: entry, or, when the key has no value, the
expression: option under it
+ StringBuilder block = new StringBuilder();
boolean resolves = false;
+ boolean options = inline.isEmpty() || inline.startsWith("#");
+ if (inline.startsWith("{")) {
+ Matcher e = FLOW_EXPRESSION.matcher(inline);
+ if (e.find()) {
+
block.append(stripYamlQuotes(e.group(1).trim())).append('\n');
+ }
+ resolves = FLOW_RESOLVE_RESOURCE.matcher(inline).find();
+ } else if (!options) {
+ block.append(stripYamlQuotes(inline)).append('\n');
+ }
+ boolean inScript = !options && !inline.startsWith("{");
+ int optionIndent = -1;
int end = i;
for (int j = i + 1; j < lines.length; j++) {
if (lines[j].isBlank()) {
continue;
}
- if (YamlLines.countLeadingSpaces(lines[j]) <= indent) {
+ int lineIndent = YamlLines.countLeadingSpaces(lines[j]);
+ if (lineIndent <= indent) {
break;
}
end = j;
String t = lines[j].trim();
- if (t.startsWith("resolveResource:") &&
t.substring(16).trim().startsWith("true")) {
- resolves = true;
+ if (options && (optionIndent < 0 || lineIndent <=
optionIndent)) {
+ // an option of the language: only expression: holds the
script, its block scalar goes on below
+ optionIndent = lineIndent;
+ if (t.startsWith("resolveResource:") &&
t.substring(16).trim().startsWith("true")) {
+ resolves = true;
+ }
+ inScript = t.startsWith("expression:");
+ if (inScript) {
+
block.append(stripYamlQuotes(t.substring(11).trim())).append('\n');
+ }
+ } else if (inScript) {
+ block.append(t).append('\n');
}
- block.append(t.startsWith("expression:") ?
stripYamlQuotes(t.substring(11).trim()) : t).append('\n');
}
Matcher m = QUOTED_RESOURCE_LITERAL.matcher(block);
if (resolves) {
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/SourceValidatorResourceLiteralTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/SourceValidatorResourceLiteralTest.java
index f3276cca57e6..288ace5b7520 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/SourceValidatorResourceLiteralTest.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/SourceValidatorResourceLiteralTest.java
@@ -18,13 +18,15 @@ package org.apache.camel.dsl.jbang.core.commands.ai;
import java.util.List;
+import org.apache.camel.catalog.DefaultCamelCatalog;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* CAMEL-24882: a script that returns 'resource:file:x' returns that text; the
validator says to add resolveResource:
- * true or to use constant/simple. CAMEL-25982: with resolveResource: true the
name has no scheme.
+ * true or to use constant/simple. CAMEL-25982: with resolveResource: true the
name has no scheme. CAMEL-27146: only the
+ * script is read, not the other keys under the language.
*/
class SourceValidatorResourceLiteralTest {
@@ -101,4 +103,95 @@ class SourceValidatorResourceLiteralTest {
+ " scheme, 'resource:order.json' instead of
'resource:file:order.json'");
}
+ @Test
+ void aResourceValueUnderAnotherKeyIsNotAScript() {
+ // r35 (local-model benchmark): a model wrote the file under the wrong
key resource: and was told that the
+ // script returns the text, so it wrote the file name as the script,
which groovy ran as code
+ String yaml = """
+ - route:
+ from:
+ uri: timer:orders
+ steps:
+ - setBody:
+ expression:
+ groovy:
+ resource:
"resource:file:shipment-mapping.groovy"
+ - setBody:
+ expression:
+ groovy:
+ resultType: java.util.Map
+ resourceUri:
"resource:file:shipment-mapping.groovy"
+ """;
+
assertThat(BeanRefChecks.validateReturnedResourceLiterals(yaml)).isEmpty();
+ }
+
+ @Test
+ void theScriptUnderExpressionIsReadAmongTheOtherOptions() {
+ String yaml = """
+ - route:
+ from:
+ uri: timer:orders
+ steps:
+ - setBody:
+ expression:
+ groovy:
+ resultType: java.lang.String
+ expression: |
+ def counter =
exchange.getProperty('CamelTimerCounter', Integer) ?: 0
+ counter == 0 ? 'resource:file:order.json' :
'resource:file:other.json'
+ trim: true
+ """;
+ List<String> errors =
BeanRefChecks.validateReturnedResourceLiterals(yaml);
+ assertThat(errors).hasSize(1);
+ assertThat(errors.get(0))
+ .startsWith("Line 7: groovy: the script returns the text
'resource:file:order.json', not the file");
+ }
+
+ @Test
+ void aFlowMappingIsReadByItsExpression() {
+ String yaml = """
+ - route:
+ from:
+ uri: timer:orders
+ steps:
+ - setBody:
+ expression:
+ groovy: {expression:
"resource:file:shipment-mapping.groovy"}
+ - setBody:
+ expression:
+ groovy: {resource:
"resource:file:shipment-mapping.groovy"}
+ - setBody:
+ expression:
+ groovy: {expression: "'resource:order.json'",
resolveResource: true}
+ - setBody:
+ expression:
+ groovy: {expression:
"'resource:file:order.json'"}
+ """;
+ List<String> errors =
BeanRefChecks.validateReturnedResourceLiterals(yaml);
+ assertThat(errors).hasSize(1);
+ assertThat(errors.get(0))
+ .startsWith("Line 16: groovy: the script returns the text
'resource:file:order.json', not the file");
+ }
+
+ @Test
+ void aWrongKeyGetsOnlyTheSchemaMessage() {
+ String yaml = """
+ - route:
+ id: data-mapping
+ from:
+ uri: timer:order?repeatCount=1
+ steps:
+ - setBody:
+ expression:
+ groovy:
+ resource:
"resource:file:shipment-mapping.groovy"
+ - log:
+ message: "Shipment: ${body}"
+ """;
+ List<String> errors =
SourceValidator.validate("data-mapping.camel.yaml", yaml, new
DefaultCamelCatalog(), null);
+ assertThat(errors).anyMatch(e -> e.contains("property 'resource' is
not defined")
+ && e.contains("write groovy: {expression:
\"resource:file:shipment-mapping.groovy\"}"));
+ assertThat(errors).noneMatch(e -> e.contains("the script returns the
text"));
+ }
+
}