This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch CAMEL-27979-header-name-filter
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 9628f730d67b67be695361419ef974e48c1eaa4a
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Dec 1 21:28:51 2026 +0100

    CAMEL-27979: camel-jbang - optionsFilter=<header name> answers with the 
headers again when option descriptions name them
    
    camel_catalog_doc searches the message headers of a component only when
    optionsFilter matches no option. Since the option descriptions name
    headers (CAMEL-23832), keyName and deleteAfterRead of aws2-s3 mention
    CamelAwsS3Key, so optionsFilter=CamelAwsS3Key (and CamelAwsS3) returned
    those options instead of the headers, although headersHint says that
    optionsFilter=<header name> returns the header.
    
    An option that matches a single filter term only where its description
    names a message header that the term matches is no longer a match; the
    note names such options. A word such as key still matches the options
    whose descriptions use it.
    
    Fixes CatalogLookupsTest on main.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../dsl/jbang/core/commands/ai/CatalogDocs.java    | 61 ++++++++++++++++++++--
 .../jbang/core/commands/ai/CatalogLookupsTest.java | 11 ++--
 2 files changed, 64 insertions(+), 8 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogDocs.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogDocs.java
index b3922fdc6687..aacaa924f15e 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogDocs.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogDocs.java
@@ -2292,6 +2292,12 @@ public final class CatalogDocs {
         }
         Set<String> endpointListed = new HashSet<>();
         Set<String> endpointOmitted = new HashSet<>();
+        // a filter that is (part of) the name of a message header asks for 
headers: an option that matches it only where
+        // its description names such a header (keyName and deleteAfterRead of 
aws2-s3 name CamelAwsS3Key) is no match,
+        // else the headers would never be returned, and the note names such 
options (CAMEL-27979)
+        List<String> headerNames
+                = filter != null && !several(filter) ? 
headerNamesContaining(model, filter) : List.of();
+        Set<String> mentionedBy = new LinkedHashSet<>();
         for (String optionScope : List.of("endpoint", "component")) {
             List<? extends BaseOptionModel> options
                     = "endpoint".equals(optionScope) ? 
model.getEndpointOptions() : model.getComponentOptions();
@@ -2302,6 +2308,10 @@ public final class CatalogDocs {
                 if (!matchesOptionFilter(opt, filter)) {
                     continue;
                 }
+                if (!headerNames.isEmpty() && onlyNamesHeaders(opt, filter, 
headerNames)) {
+                    mentionedBy.add(opt.getName());
+                    continue;
+                }
                 boolean endpoint = "endpoint".equals(optionScope);
                 if (!endpoint && endpointListed.contains(opt.getName())) {
                     // already listed with its endpoint twin
@@ -2364,7 +2374,7 @@ public final class CatalogDocs {
         if (filter != null && !several(filter) && ordered.isEmpty() && omitted 
== 0) {
             List<ComponentModel.EndpointHeaderModel> headers = 
headersMatching(model, filter);
             if (!headers.isEmpty()) {
-                putMatchedHeaders(result, headers, filter);
+                putMatchedHeaders(result, headers, filter, mentionedBy);
             } else {
                 // an empty list read as "not supported" (smtp + tls, xslt + 
param): say that nothing matched, and
                 // where else to look
@@ -2397,6 +2407,39 @@ public final class CatalogDocs {
     /** Up to this many message headers that a filter matches are listed in 
full; more are listed by name. */
     static final int MATCHED_HEADERS_LIMIT = 10;
 
+    /** The names of the message headers of a component that contain the 
filter, in lower case and the longest first. */
+    private static List<String> headerNamesContaining(ComponentModel model, 
String filter) {
+        List<String> answer = new ArrayList<>();
+        if (model.getEndpointHeaders() != null) {
+            for (ComponentModel.EndpointHeaderModel h : 
model.getEndpointHeaders()) {
+                String name = h.getName() != null ? 
h.getName().toLowerCase(Locale.ROOT) : "";
+                if (name.contains(filter)) {
+                    answer.add(name);
+                }
+            }
+        }
+        // CamelAwsS3KeysToDelete is taken out of a description before 
CamelAwsS3Key
+        answer.sort(Comparator.comparingInt(String::length).reversed());
+        return answer;
+    }
+
+    /** Whether an option matches the filter only where its description names 
one of these headers. */
+    private static boolean onlyNamesHeaders(BaseOptionModel opt, String 
filter, List<String> headerNames) {
+        if (opt.getDescription() == null || containsLower(opt.getName(), 
filter) || containsLower(opt.getGroup(), filter)
+                || containsLower(opt.getLabel(), filter)) {
+            return false;
+        }
+        String text = opt.getDescription().toLowerCase(Locale.ROOT);
+        for (String name : headerNames) {
+            text = text.replace(name, " ");
+        }
+        return !text.contains(filter);
+    }
+
+    private static boolean containsLower(String text, String filter) {
+        return text != null && text.toLowerCase(Locale.ROOT).contains(filter);
+    }
+
     /**
      * The message headers of a component that a filter matches, by name 
before by description. A header is not an
      * option, and the filter searched the options only: a model sent 
optionsFilter=CamelAwsS3Key on aws2-s3 17 times in
@@ -2430,7 +2473,8 @@ public final class CatalogDocs {
      * options.
      */
     private static void putMatchedHeaders(
-            JsonObject result, List<ComponentModel.EndpointHeaderModel> 
headers, String filter) {
+            JsonObject result, List<ComponentModel.EndpointHeaderModel> 
headers, String filter,
+            Set<String> mentionedBy) {
         boolean many = headers.size() > MATCHED_HEADERS_LIMIT;
         JsonArray full = new JsonArray();
         JsonArray names = new JsonArray();
@@ -2442,10 +2486,17 @@ public final class CatalogDocs {
             }
         }
         String see = full.isEmpty() ? "headerNames" : names.isEmpty() ? 
"headers" : "headers and headerNames";
-        result.put("note", "no option matches '" + filter + "', but " + 
(headers.size() == 1
+        String note = "no option matches '" + filter + "', but " + 
(headers.size() == 1
                 ? "a message header does" : headers.size() + " message headers 
do")
-                           + ": see " + see + ". A header is not set on the 
URI: set it on the message before a to:"
-                           + " (setHeader), or read it after a from: 
(${header." + headers.get(0).getName() + "})");
+                      + ": see " + see + ". A header is not set on the URI: 
set it on the message before a to:"
+                      + " (setHeader), or read it after a from: (${header." + 
headers.get(0).getName() + "})";
+        if (!mentionedBy.isEmpty()) {
+            note += ". " + (mentionedBy.size() == 1 ? "The option " : "The 
options ") + String.join(", ", mentionedBy)
+                    + (mentionedBy.size() == 1 ? " names " : " name ")
+                    + (headers.get(0).getName().equalsIgnoreCase(filter) ? 
headers.get(0).getName() : "such a header")
+                    + " in the description (optionsFilter=<option name> 
returns an option)";
+        }
+        result.put("note", note);
         if (!full.isEmpty()) {
             result.put("headers", full);
         }
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogLookupsTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogLookupsTest.java
index 19e5e1bae3ec..d6b6d34af55f 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogLookupsTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogLookupsTest.java
@@ -60,7 +60,7 @@ class CatalogLookupsTest {
         // a model sent optionsFilter=CamelAwsS3Key on aws2-s3 17 times in one 
step, and no option matches it
         JsonObject answer = catalogDoc(Map.of("name", "aws2-s3", 
"optionsFilter", "CamelAwsS3Key"));
         assertThat(answer.getInteger("matchedOptions")).isZero();
-        assertThat(answer.getInteger("matchedHeaders")).isEqualTo(2);
+        assertThat(answer.getInteger("matchedHeaders")).isEqualTo(3);
         // the header the filter names comes first, with what the catalog says 
of it
         JsonObject key = first(answer, "headers");
         assertThat(key.getString("name")).isEqualTo("CamelAwsS3Key");
@@ -68,13 +68,18 @@ class CatalogLookupsTest {
         assertThat(key.getString("javaType")).isEqualTo("String");
         assertThat(key.getString("group")).isEqualTo("common");
         assertThat(key.getString("description")).contains("key");
-        assertThat(answer.getString("note")).contains("2 message headers do", 
"${header.CamelAwsS3Key}");
+        assertThat(answer.getString("note")).contains("3 message headers do", 
"${header.CamelAwsS3Key}");
         assertThat(answer.get("closestOptions")).isNull();
+        // CAMEL-27979: keyName and deleteAfterRead name the header in their 
description, which does not make them a
+        // match of a filter that is the name of a header, else the header 
would never be returned; the note says so
+        assertThat(names(answer, "options")).isEmpty();
+        assertThat(answer.getString("note")).contains("keyName", 
"deleteAfterRead", "name CamelAwsS3Key");
 
         // with includeHeaders the headers the filter matches, not all of them 
(54, cut at the end of the answer)
         JsonObject included
                 = catalogDoc(Map.of("name", "aws2-s3", "optionsFilter", 
"CamelAwsS3Key", "includeHeaders", "true"));
-        assertThat(names(included, 
"headers")).containsExactly("CamelAwsS3Key", "CamelAwsS3KeysToDelete");
+        assertThat(names(included, 
"headers")).containsExactly("CamelAwsS3Key", "CamelAwsS3KeysToDelete",
+                "CamelAwsS3ProducedKey");
 
         // a filter that matches many headers lists their names, so the answer 
stays short
         JsonObject many = catalogDoc(Map.of("name", "aws2-s3", 
"optionsFilter", "CamelAwsS3"));

Reply via email to