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 671c1aee61f2 CAMEL-27148: camel-jbang - a write note when the steps of 
an idempotentConsumer that skips duplicates check CamelDuplicateMessage (#30320)
671c1aee61f2 is described below

commit 671c1aee61f28680eb2818f0cd8293ca0fd6487c
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Nov 18 10:01:45 2026 +0100

    CAMEL-27148: camel-jbang - a write note when the steps of an 
idempotentConsumer that skips duplicates check CamelDuplicateMessage (#30320)
    
    An idempotentConsumer skips a duplicate by default: the duplicate never 
reaches the steps under
    it (it goes on to the steps after it, with CamelDuplicateMessage set to 
true), and a new message
    has no such property. So a check of CamelDuplicateMessage under it is never 
true. In the
    local-model benchmark (r30 to r35), route-enrich-and-dedupe step 1 asks to 
log accepted orders and
    skipped duplicates: the 8 step runs that checked the property without 
skipDuplicate: false all
    failed (2 of them with == false, which logged every new order as a 
duplicate), and the 27 that set
    it all passed.
    
    The write tools now give a note for such an idempotentConsumer: add 
skipDuplicate: false, and
    check == true for a duplicate (not == false). The write is not refused. A 
skipDuplicate that is
    not plainly true, such as a placeholder, and a check after the 
idempotentConsumer get no note.
    
    Claude-Session: https://claude.ai/code/session_016Y4dDi5iQNgtHrDdz2NpwF
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../dsl/jbang/core/commands/ai/AuthoringTools.java | 13 +++-
 .../dsl/jbang/core/commands/ai/WriteNotes.java     | 79 +++++++++++++++++++++-
 .../core/commands/ai/AuthoringToolsEditTest.java   | 58 ++++++++++++++++
 .../dsl/jbang/core/commands/ai/WriteNotesTest.java | 77 +++++++++++++++++++++
 4 files changed, 224 insertions(+), 3 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringTools.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringTools.java
index 8a350590ee95..017b38fe272c 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringTools.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringTools.java
@@ -1566,7 +1566,7 @@ public final class AuthoringTools {
      * template does not use (CAMEL-25403), a {expression} in the text of an 
XSLT element, a JMS reply header set by
      * hand in a route that sends nothing InOut, a to: that copies each file 
its file consumer then moves to .camel, a
      * request-reply over JMS whose reply replaces the message that was sent, 
a to: that appends to a file with nothing
-     * between the messages.
+     * between the messages, an idempotentConsumer that skips duplicates whose 
steps check CamelDuplicateMessage.
      */
     private static void putFileNotes(JsonObject result, String file, String 
content) {
         String name = file != null ? file.toLowerCase(Locale.ROOT) : "";
@@ -1583,11 +1583,13 @@ public final class AuthoringTools {
             }
         } else if ((name.endsWith(".yaml") || name.endsWith(".yml")) && 
!SourceValidator.isCitrusTest(name)) {
             // a JMS reply header set by hand, with nothing sent InOut; a to: 
that copies the file a consumer moves; a
-            // JMS reply that replaces the message that was sent; a file 
appended to with nothing between the messages
+            // JMS reply that replaces the message that was sent; a file 
appended to with nothing between the messages;
+            // a check of CamelDuplicateMessage that a duplicate never reaches
             List<String> notes = new 
ArrayList<>(HeaderChecks.replyHeaderNotes(content));
             notes.addAll(WriteNotes.fileCopyNotes(content));
             notes.addAll(WriteNotes.requestReplyNotes(content));
             notes.addAll(WriteNotes.fileAppendNotes(content));
+            notes.addAll(WriteNotes.duplicateMessageNotes(content));
             if (!notes.isEmpty()) {
                 result.put("notes", new JsonArray(notes));
             }
@@ -1889,6 +1891,7 @@ public final class AuthoringTools {
         boolean copies = false;
         boolean replyReplaces = false;
         boolean appends = false;
+        boolean duplicateNeverSeen = false;
         List<WriteNotes.UnusedProperty> unusedProperties = List.of();
         if (validate && !citrusTest) {
             putFileNotes(result, file, content);
@@ -1899,6 +1902,7 @@ public final class AuthoringTools {
             copies = yaml && !kamelet && 
!WriteNotes.fileCopyNotes(content).isEmpty();
             replyReplaces = yaml && !kamelet && 
!WriteNotes.requestReplyNotes(content).isEmpty();
             appends = yaml && !kamelet && 
!WriteNotes.fileAppendNotes(content).isEmpty();
+            duplicateNeverSeen = yaml && !kamelet && 
!WriteNotes.duplicateMessageNotes(content).isEmpty();
             // a literal written over a {{key}} that leaves the key of 
application.properties unused
             unusedProperties = WriteNotes.unusedProperties(dir, file, before, 
content);
             if (!unusedProperties.isEmpty()) {
@@ -1975,6 +1979,11 @@ public final class AuthoringTools {
                                   + " with nothing between them; appendChars 
on the to: adds a line break after each"
                                   + " (see notes).");
         }
+        if (duplicateNeverSeen) {
+            result.put("message", result.getString("message") + " Note: an 
idempotentConsumer skips duplicates, so a"
+                                  + " check of CamelDuplicateMessage in its 
steps is never true; skipDuplicate: false"
+                                  + " lets a duplicate reach them (see 
notes).");
+        }
         if (!repaired.isEmpty()) {
             result.put("repaired", new JsonArray(repaired));
             result.put("message", "Written with these repairs of the YAML (the 
file has them now): "
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/WriteNotes.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/WriteNotes.java
index 17297d402fbf..0c5572ec38b2 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/WriteNotes.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/WriteNotes.java
@@ -41,7 +41,8 @@ import org.yaml.snakeyaml.constructor.SafeConstructor;
  * What a written route does that works but is probably not what was asked, 
said as a note of the write: the route can
  * be right, so the write is not refused. A to: that copies each file its file 
consumer then moves to .camel, a to: that
  * appends to a file with nothing between the messages, a request-reply over 
JMS whose reply replaces the message that
- * was sent, and a literal written over a {{key}} placeholder that leaves the 
key in application.properties unused.
+ * was sent, an idempotentConsumer that skips duplicates whose steps check 
CamelDuplicateMessage, and a literal written
+ * over a {{key}} placeholder that leaves the key in application.properties 
unused.
  */
 final class WriteNotes {
 
@@ -95,6 +96,9 @@ final class WriteNotes {
     /** The appendChars option in a uri, with its value up to the next option. 
*/
     private static final Pattern APPEND_CHARS = 
Pattern.compile("(?i)([?&]appendChars=)[^&]*");
 
+    /** A check of CamelDuplicateMessage with == false, which a new message, 
with no such property, never matches. */
+    private static final Pattern DUPLICATE_EQUALS_FALSE = 
Pattern.compile("CamelDuplicateMessage}?\\s*==\\s*false");
+
     private WriteNotes() {
     }
 
@@ -438,6 +442,79 @@ final class WriteNotes {
         return null;
     }
 
+    /**
+     * An idempotentConsumer that skips a duplicate (skipDuplicate is true by 
default) whose steps check the exchange
+     * property CamelDuplicateMessage: a duplicate never reaches its steps (it 
goes on to the steps after it, with the
+     * property set), and a new message has no such property, so the check 
there is never true. r30 to r35 of the
+     * local-model benchmark, route-enrich-and-dedupe step 1 ("log ... 
accepted for a new order id, and ... is a
+     * duplicate, skipped for one seen before"): the 8 step runs that checked 
the property without skipDuplicate: false
+     * all failed (2 of them checked == false, so every new order was logged 
as a duplicate), and the 27 that set it all
+     * passed. A skipDuplicate that is not plainly true, such as a 
placeholder, gets no note. Each note is "Line N: ..."
+     * for the line of the idempotentConsumer.
+     */
+    static List<String> duplicateMessageNotes(String content) {
+        List<String> notes = new ArrayList<>();
+        if (content == null || !content.contains("CamelDuplicateMessage")) {
+            return notes;
+        }
+        Object doc;
+        try {
+            doc = new Yaml(new SafeConstructor(new 
LoaderOptions())).load(content);
+        } catch (RuntimeException e) {
+            return notes;
+        }
+        try {
+            // the pointer of each such idempotentConsumer, and whether its 
steps check the property with == false
+            Map<String, Boolean> skipping = new LinkedHashMap<>();
+            idempotentConsumers(doc, "", skipping);
+            Node root = skipping.isEmpty() ? null : 
YamlPointerLines.root(content);
+            for (Map.Entry<String, Boolean> e : skipping.entrySet()) {
+                int line = YamlPointerLines.line(root, e.getKey(), null);
+                notes.add((line > 0 ? "Line " + line + ": " : "") + "this 
idempotentConsumer skips a duplicate"
+                          + " (skipDuplicate is true by default): a duplicate 
never reaches the steps under it, so"
+                          + " the check of the exchange property 
CamelDuplicateMessage there is never true. Add"
+                          + " skipDuplicate: false, and a duplicate goes on to 
these steps with CamelDuplicateMessage"
+                          + " set to true."
+                          + (e.getValue()
+                                  ? " Check == true for a duplicate (and 
otherwise: for a new message), not == false:"
+                                    + " a new message has no such property."
+                                  : ""));
+            }
+        } catch (RuntimeException e) {
+            // a shape the walk does not know: a note must never fail the write
+        }
+        return notes;
+    }
+
+    /**
+     * Adds the pointer of each idempotentConsumer step under the node that 
skips duplicates and whose steps check
+     * CamelDuplicateMessage, with whether they check it with == false. Any 
list or map is walked: an idempotentConsumer
+     * can be the step of a route, or of a split or a when: of a choice.
+     */
+    private static void idempotentConsumers(Object node, String at, 
Map<String, Boolean> skipping) {
+        if (node instanceof List<?> list) {
+            for (int i = 0; i < list.size(); i++) {
+                idempotentConsumers(list.get(i), at + "/" + i, skipping);
+            }
+        } else if (node instanceof Map<?, ?> map) {
+            if (map.size() == 1 && map.get("idempotentConsumer") instanceof 
Map<?, ?> eip && skipsDuplicates(eip)) {
+                String steps = String.valueOf(eip.get("steps"));
+                if (steps.contains("CamelDuplicateMessage")) {
+                    skipping.put(at, 
DUPLICATE_EQUALS_FALSE.matcher(steps).find());
+                }
+            }
+            for (Map.Entry<?, ?> e : map.entrySet()) {
+                idempotentConsumers(e.getValue(), at + "/" + e.getKey(), 
skipping);
+            }
+        }
+    }
+
+    /** Whether an idempotentConsumer skips duplicates: skipDuplicate left 
out, or plainly true. */
+    private static boolean skipsDuplicates(Map<?, ?> eip) {
+        Object skip = eip.get("skipDuplicate");
+        return skip == null || Boolean.TRUE.equals(skip) || 
"true".equalsIgnoreCase(String.valueOf(skip).trim());
+    }
+
     /**
      * A request-reply over JMS whose reply replaces the body: a to: on a JMS 
endpoint with InOut (exchangePattern=InOut
      * on the endpoint, pattern: InOut on the to:, or a setExchangePattern 
before it) and no variableReceive. After it
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringToolsEditTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringToolsEditTest.java
index 8f8111463fc5..91ad88d3ecc9 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringToolsEditTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringToolsEditTest.java
@@ -429,6 +429,64 @@ class AuthoringToolsEditTest {
         assertThat(kept.getString("message")).doesNotContain("Note:");
     }
 
+    /**
+     * route-enrich-and-dedupe step 1 of the local-model benchmark (r30 to 
r35): asked to log the duplicates, the model
+     * checked CamelDuplicateMessage under an idempotentConsumer that skips 
them, so no duplicate was ever logged
+     * (CAMEL-27148). The write goes through, with a note that says to add 
skipDuplicate: false.
+     */
+    @Test
+    void aDuplicateCheckThatADuplicateNeverReachesIsANoteOfTheWrite(@TempDir 
Path dir) throws IOException {
+        String route = """
+                - beans:
+                    - name: orderRepo
+                      type: 
org.apache.camel.support.processor.idempotent.MemoryIdempotentRepository
+                - route:
+                    id: incoming-orders
+                    from:
+                      uri: file
+                      parameters:
+                        directoryName: orders
+                        noop: true
+                      steps:
+                        - unmarshal:
+                            json: {}
+                        - idempotentConsumer:
+                            expression:
+                              simple: "${body[orderId]}"
+                            idempotentRepository: orderRepo
+                            steps:
+                              - choice:
+                                  when:
+                                    - expression:
+                                        simple: 
"${exchangeProperty.CamelDuplicateMessage} == true"
+                                      steps:
+                                        - log:
+                                            message: "Order ${body[orderId]} 
is a duplicate, skipped"
+                                  otherwise:
+                                    steps:
+                                      - log:
+                                          message: "Order ${body[orderId]} 
accepted"
+                """;
+
+        JsonObject result = AuthoringTools.writeFile(new ToolContext(), dir, 
"enrich-and-dedupe.camel.yaml", route, true);
+
+        
assertThat(result.getString("status")).as(result.toJson()).isEqualTo("created");
+        assertThat(((JsonArray) 
result.get("notes"))).singleElement().asString()
+                .startsWith("Line 14: this idempotentConsumer skips a 
duplicate");
+        assertThat(result.getString("message"))
+                .endsWith("Note: an idempotentConsumer skips duplicates, so a 
check of CamelDuplicateMessage in its steps"
+                          + " is never true; skipDuplicate: false lets a 
duplicate reach them (see notes).");
+
+        // skipDuplicate: false as the note gives it: the write is valid, with 
no note
+        JsonObject fixed = AuthoringTools.writeFile(new ToolContext(), dir, 
"enrich-and-dedupe.camel.yaml",
+                route.replace("idempotentRepository: orderRepo",
+                        "idempotentRepository: orderRepo\n" + " ".repeat(12) + 
"skipDuplicate: false"),
+                true);
+        
assertThat(fixed.getString("status")).as(fixed.toJson()).isEqualTo("overwritten");
+        assertThat(fixed.get("notes")).as(fixed.toJson()).isNull();
+        assertThat(fixed.getString("message")).doesNotContain("Note:");
+    }
+
     /**
      * connect-file-processing step 1 of the local-model benchmark (r24, r25): 
asked to move the processed files to
      * done, the model added a to: done, which copies each file while the 
consumer still moves it to .camel. The edit
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/WriteNotesTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/WriteNotesTest.java
index 47430562c3e7..7ce646b48c84 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/WriteNotesTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/WriteNotesTest.java
@@ -698,4 +698,81 @@ class WriteNotesTest {
         // a properties file is not a route
         assertThat(WriteNotes.unusedPropertyNotes(dir, "other.properties", 
"a={{report.schedule}}\n", "a=1\n")).isEmpty();
     }
+
+    /** route-enrich-and-dedupe step 1 of the local-model benchmark, as r35 
wrote it: the duplicate is never logged. */
+    private static final String DEDUPE = """
+            - route:
+                id: incoming-orders
+                from:
+                  uri: file
+                  parameters:
+                    directoryName: orders
+                    noop: true
+                  steps:
+                    - unmarshal:
+                        json: {}
+                    - idempotentConsumer:
+                        expression:
+                          simple: "${body[orderId]}"
+                        idempotentRepository: orderRepo
+                        steps:
+                          - choice:
+                              when:
+                                - expression:
+                                    simple: 
"${exchangeProperty.CamelDuplicateMessage} == true"
+                                  steps:
+                                    - log:
+                                        message: "Order ${body[orderId]} is a 
duplicate, skipped"
+                              otherwise:
+                                steps:
+                                  - log:
+                                      message: "Order ${body[orderId]} 
accepted"
+            """;
+
+    @Test
+    void 
aCheckOfTheDuplicatePropertyUnderAnIdempotentConsumerThatSkipsDuplicates() {
+        
assertThat(WriteNotes.duplicateMessageNotes(DEDUPE)).singleElement().isEqualTo(
+                "Line 11: this idempotentConsumer skips a duplicate 
(skipDuplicate is true by default): a duplicate"
+                                                                               
        + " never reaches the steps under it, so the check of the exchange 
property"
+                                                                               
        + " CamelDuplicateMessage there is never true. Add skipDuplicate: 
false, and a"
+                                                                               
        + " duplicate goes on to these steps with CamelDuplicateMessage set to 
true.");
+    }
+
+    @Test
+    void aCheckWithEqualsFalseIsNeverTrueForANewMessage() {
+        String yaml = DEDUPE.replace("CamelDuplicateMessage} == true", 
"CamelDuplicateMessage} == false");
+        
assertThat(WriteNotes.duplicateMessageNotes(yaml)).singleElement().asString()
+                .startsWith("Line 11: this idempotentConsumer skips a 
duplicate")
+                .endsWith(" Check == true for a duplicate (and otherwise: for 
a new message), not == false: a new"
+                          + " message has no such property.");
+    }
+
+    @Test
+    void skipDuplicateFalseOrACheckAfterTheIdempotentConsumerHasNoNote() {
+        // skipDuplicate: false routes a duplicate on to the steps, with the 
property set
+        
assertThat(WriteNotes.duplicateMessageNotes(DEDUPE.replace("idempotentRepository:
 orderRepo",
+                "idempotentRepository: orderRepo\n" + " ".repeat(12) + 
"skipDuplicate: false"))).isEmpty();
+        // a placeholder may be false
+        
assertThat(WriteNotes.duplicateMessageNotes(DEDUPE.replace("idempotentRepository:
 orderRepo",
+                "idempotentRepository: orderRepo\n" + " ".repeat(12) + 
"skipDuplicate: \"{{skip}}\""))).isEmpty();
+        // after the idempotentConsumer a duplicate goes on with the property 
set: the check works there
+        String after = """
+                - route:
+                    from:
+                      uri: direct:orders
+                      steps:
+                        - idempotentConsumer:
+                            expression:
+                              simple: "${body[orderId]}"
+                            idempotentRepository: orderRepo
+                            steps:
+                              - log: "Order ${body[orderId]} accepted"
+                        - filter:
+                            expression:
+                              simple: 
"${exchangeProperty.CamelDuplicateMessage} == true"
+                            steps:
+                              - log: "Order ${body[orderId]} is a duplicate, 
skipped"
+                """;
+        assertThat(WriteNotes.duplicateMessageNotes(after)).isEmpty();
+    }
 }

Reply via email to