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

Croway 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 8ec805f1469b CAMEL-24610: camel-langchain4j-web-search - guard against 
a null maxResults when a custom WebSearchRequest omits it
8ec805f1469b is described below

commit 8ec805f1469b13c6c921a6ce86c6053e31bae35c
Author: Andrea Cosentino <[email protected]>
AuthorDate: Thu Sep 3 10:10:55 2026 +0200

    CAMEL-24610: camel-langchain4j-web-search - guard against a null maxResults 
when a custom WebSearchRequest omits it
    
    LangChain4jWebSearchProducer passes webSearchRequest.maxResults() into
    computeResponse(..., Integer maxResults), which did `if (maxResults == 1)` 
- an
    auto-unbox of a nullable Integer. In the advanced path, when the user 
supplies a
    custom WebSearchRequest that does not set maxResults, langchain4j's
    WebSearchRequest.maxResults() is null and the comparison threw
    NullPointerException. Guard the comparison with a null check so a null 
maxResults
    falls through to the multi-result branch.
    
    Co-Authored-By: Claude Opus 4.8 <[email protected]>
    Claude-Session: https://claude.ai/code/session_01Ka4dAcJMpxahMfk3kmG5Ls
    Signed-off-by: Andrea Cosentino <[email protected]>
---
 .../web/search/LangChain4jWebSearchProducer.java   |  4 +-
 .../LangChain4jWebSearchCustomRequestTest.java     | 67 ++++++++++++++++++++++
 2 files changed, 69 insertions(+), 2 deletions(-)

diff --git 
a/components/camel-ai/camel-langchain4j-web-search/src/main/java/org/apache/camel/component/langchain4j/web/search/LangChain4jWebSearchProducer.java
 
b/components/camel-ai/camel-langchain4j-web-search/src/main/java/org/apache/camel/component/langchain4j/web/search/LangChain4jWebSearchProducer.java
index d226ca247822..9e8c3faf3f29 100644
--- 
a/components/camel-ai/camel-langchain4j-web-search/src/main/java/org/apache/camel/component/langchain4j/web/search/LangChain4jWebSearchProducer.java
+++ 
b/components/camel-ai/camel-langchain4j-web-search/src/main/java/org/apache/camel/component/langchain4j/web/search/LangChain4jWebSearchProducer.java
@@ -91,8 +91,8 @@ public class LangChain4jWebSearchProducer extends 
DefaultProducer {
             return;
         }
 
-        // return a single object as a response
-        if (maxResults == 1) {
+        // return a single object as a response (maxResults may be null when a 
custom WebSearchRequest omits it)
+        if (maxResults != null && maxResults == 1) {
             switch (getEndpoint().getConfiguration().getResultType()) {
                 case LANGCHAIN4J_WEB_SEARCH_ORGANIC_RESULT -> 
exchange.getIn().setBody(webSearchOrganicResults.get(0));
                 case CONTENT -> 
exchange.getIn().setBody(webSearchOrganicResults.get(0).content());
diff --git 
a/components/camel-ai/camel-langchain4j-web-search/src/test/java/org/apache/camel/component/langchain4j/web/search/LangChain4jWebSearchCustomRequestTest.java
 
b/components/camel-ai/camel-langchain4j-web-search/src/test/java/org/apache/camel/component/langchain4j/web/search/LangChain4jWebSearchCustomRequestTest.java
new file mode 100644
index 000000000000..8edf76010f41
--- /dev/null
+++ 
b/components/camel-ai/camel-langchain4j-web-search/src/test/java/org/apache/camel/component/langchain4j/web/search/LangChain4jWebSearchCustomRequestTest.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.langchain4j.web.search;
+
+import java.net.URI;
+import java.util.List;
+
+import dev.langchain4j.web.search.WebSearchEngine;
+import dev.langchain4j.web.search.WebSearchInformationResult;
+import dev.langchain4j.web.search.WebSearchOrganicResult;
+import dev.langchain4j.web.search.WebSearchRequest;
+import dev.langchain4j.web.search.WebSearchResults;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+public class LangChain4jWebSearchCustomRequestTest extends CamelTestSupport {
+
+    private final WebSearchEngine engine = request -> WebSearchResults.from(
+            WebSearchInformationResult.from(1L),
+            List.of(WebSearchOrganicResult.from(
+                    "Title", URI.create("https://example.com";), "snippet", 
"content")));
+
+    // A custom (advanced) WebSearchRequest that deliberately does not set 
maxResults, so maxResults() is null.
+    private final WebSearchRequest customRequest = 
WebSearchRequest.builder().searchTerms("apache camel").build();
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        context.getRegistry().bind("engine", engine);
+        context.getRegistry().bind("customRequest", customRequest);
+
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:advanced")
+                        
.to("langchain4j-web-search:test?webSearchEngine=#engine&webSearchRequest=#customRequest");
+            }
+        };
+    }
+
+    @Test
+    void customRequestWithoutMaxResultsDoesNotThrow() {
+        Exchange result = template.request("direct:advanced", e -> 
e.getIn().setBody("apache camel"));
+
+        // Before the fix this NPE'd unboxing a null Integer at `maxResults == 
1`.
+        assertNull(result.getException());
+        assertNotNull(result.getMessage().getBody());
+    }
+}

Reply via email to