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 c140845b0379 CAMEL-27971: Fix camel-zendesk token propagation and 
shared client reuse (#31287)
c140845b0379 is described below

commit c140845b0379f896adc05a9070e4de580a737f63
Author: Guillaume Nodet <[email protected]>
AuthorDate: Thu Dec 3 08:42:20 2026 +0100

    CAMEL-27971: Fix camel-zendesk token propagation and shared client reuse 
(#31287)
    
    Hermes Agent (Claude Sonnet 4.6) on behalf of Guillaume Nodet
    
    Co-authored-by: Claude Sonnet 4.6 <[email protected]>
---
 .../apache/camel/catalog/components/zendesk.json   |   2 +-
 components/camel-zendesk/pom.xml                   |   6 +
 .../apache/camel/component/zendesk/zendesk.json    |   2 +-
 .../camel/component/zendesk/ZendeskComponent.java  |   9 +-
 .../camel/component/zendesk/ZendeskEndpoint.java   |  31 +++-
 .../component/zendesk/ZendeskComponentTest.java    | 169 +++++++++++++++++++++
 .../dsl/ZendeskComponentBuilderFactory.java        |   7 +-
 7 files changed, 215 insertions(+), 11 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/zendesk.json
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/zendesk.json
index 72075fb38e33..53e02e567024 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/zendesk.json
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/zendesk.json
@@ -30,7 +30,7 @@
     "lazyStartProducer": { "index": 2, "kind": "property", "displayName": 
"Lazy Start Producer", "group": "producer", "label": "producer", "required": 
false, "type": "boolean", "javaType": "boolean", "deprecated": false, 
"autowired": false, "secret": false, "defaultValue": false, "description": 
"Whether the producer should be started lazy (on the first message). By 
starting lazy you can use this to allow CamelContext and routes to startup in 
situations where a producer may otherwise fail [...]
     "autowiredEnabled": { "index": 3, "kind": "property", "displayName": 
"Autowired Enabled", "group": "advanced", "label": "advanced", "required": 
false, "type": "boolean", "javaType": "boolean", "deprecated": false, 
"autowired": false, "secret": false, "defaultValue": true, "description": 
"Whether autowiring is enabled. This is used for automatic autowiring options 
(the option must be marked as autowired) by looking up in the registry to find 
if there is a single instance of matching t [...]
     "configuration": { "index": 4, "kind": "property", "displayName": 
"Configuration", "group": "advanced", "label": "advanced", "required": false, 
"type": "object", "javaType": 
"org.apache.camel.component.zendesk.ZendeskConfiguration", "deprecated": false, 
"autowired": false, "secret": false, "description": "Component configuration" },
-    "zendesk": { "index": 5, "kind": "property", "displayName": "Zendesk", 
"group": "advanced", "label": "advanced", "required": false, "type": "object", 
"javaType": "org.zendesk.client.v2.Zendesk", "deprecated": false, "autowired": 
false, "secret": false, "description": "A Zendesk client to share. An endpoint 
uses it only when all its settings, the method included, are equal to those of 
the component configuration; otherwise the endpoint creates its own client." },
+    "zendesk": { "index": 5, "kind": "property", "displayName": "Zendesk", 
"group": "advanced", "label": "advanced", "required": false, "type": "object", 
"javaType": "org.zendesk.client.v2.Zendesk", "deprecated": false, "autowired": 
false, "secret": false, "description": "A Zendesk client to share. An endpoint 
uses it only when its connection settings (serverUrl, username, password, 
token, oauthToken) are equal to those of the component configuration; otherwise 
the endpoint creates its o [...]
     "oauthToken": { "index": 6, "kind": "property", "displayName": "Oauth 
Token", "group": "security", "label": "security", "required": false, "type": 
"string", "javaType": "java.lang.String", "deprecated": false, "autowired": 
false, "secret": true, "security": "secret", "description": "The OAuth access 
token, sent as a bearer token without username. Not used when password is set; 
takes precedence over token." },
     "password": { "index": 7, "kind": "property", "displayName": "Password", 
"group": "security", "label": "security", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": true, "security": "secret", "description": "The password of the 
Zendesk user, sent with username in basic authentication. Takes precedence over 
oauthToken and token." },
     "token": { "index": 8, "kind": "property", "displayName": "Token", 
"group": "security", "label": "security", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": true, "security": "secret", "description": "The Zendesk API token, 
sent with username as username\/token in basic authentication. Not used when 
password or oauthToken is set." },
diff --git a/components/camel-zendesk/pom.xml b/components/camel-zendesk/pom.xml
index bc2c5718948f..8244f0494c98 100644
--- a/components/camel-zendesk/pom.xml
+++ b/components/camel-zendesk/pom.xml
@@ -58,6 +58,12 @@
             <artifactId>camel-test-junit6</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>${mockito-version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git 
a/components/camel-zendesk/src/generated/resources/META-INF/org/apache/camel/component/zendesk/zendesk.json
 
b/components/camel-zendesk/src/generated/resources/META-INF/org/apache/camel/component/zendesk/zendesk.json
index 72075fb38e33..53e02e567024 100644
--- 
a/components/camel-zendesk/src/generated/resources/META-INF/org/apache/camel/component/zendesk/zendesk.json
+++ 
b/components/camel-zendesk/src/generated/resources/META-INF/org/apache/camel/component/zendesk/zendesk.json
@@ -30,7 +30,7 @@
     "lazyStartProducer": { "index": 2, "kind": "property", "displayName": 
"Lazy Start Producer", "group": "producer", "label": "producer", "required": 
false, "type": "boolean", "javaType": "boolean", "deprecated": false, 
"autowired": false, "secret": false, "defaultValue": false, "description": 
"Whether the producer should be started lazy (on the first message). By 
starting lazy you can use this to allow CamelContext and routes to startup in 
situations where a producer may otherwise fail [...]
     "autowiredEnabled": { "index": 3, "kind": "property", "displayName": 
"Autowired Enabled", "group": "advanced", "label": "advanced", "required": 
false, "type": "boolean", "javaType": "boolean", "deprecated": false, 
"autowired": false, "secret": false, "defaultValue": true, "description": 
"Whether autowiring is enabled. This is used for automatic autowiring options 
(the option must be marked as autowired) by looking up in the registry to find 
if there is a single instance of matching t [...]
     "configuration": { "index": 4, "kind": "property", "displayName": 
"Configuration", "group": "advanced", "label": "advanced", "required": false, 
"type": "object", "javaType": 
"org.apache.camel.component.zendesk.ZendeskConfiguration", "deprecated": false, 
"autowired": false, "secret": false, "description": "Component configuration" },
-    "zendesk": { "index": 5, "kind": "property", "displayName": "Zendesk", 
"group": "advanced", "label": "advanced", "required": false, "type": "object", 
"javaType": "org.zendesk.client.v2.Zendesk", "deprecated": false, "autowired": 
false, "secret": false, "description": "A Zendesk client to share. An endpoint 
uses it only when all its settings, the method included, are equal to those of 
the component configuration; otherwise the endpoint creates its own client." },
+    "zendesk": { "index": 5, "kind": "property", "displayName": "Zendesk", 
"group": "advanced", "label": "advanced", "required": false, "type": "object", 
"javaType": "org.zendesk.client.v2.Zendesk", "deprecated": false, "autowired": 
false, "secret": false, "description": "A Zendesk client to share. An endpoint 
uses it only when its connection settings (serverUrl, username, password, 
token, oauthToken) are equal to those of the component configuration; otherwise 
the endpoint creates its o [...]
     "oauthToken": { "index": 6, "kind": "property", "displayName": "Oauth 
Token", "group": "security", "label": "security", "required": false, "type": 
"string", "javaType": "java.lang.String", "deprecated": false, "autowired": 
false, "secret": true, "security": "secret", "description": "The OAuth access 
token, sent as a bearer token without username. Not used when password is set; 
takes precedence over token." },
     "password": { "index": 7, "kind": "property", "displayName": "Password", 
"group": "security", "label": "security", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": true, "security": "secret", "description": "The password of the 
Zendesk user, sent with username in basic authentication. Takes precedence over 
oauthToken and token." },
     "token": { "index": 8, "kind": "property", "displayName": "Token", 
"group": "security", "label": "security", "required": false, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "autowired": false, 
"secret": true, "security": "secret", "description": "The Zendesk API token, 
sent with username as username\/token in basic authentication. Not used when 
password or oauthToken is set." },
diff --git 
a/components/camel-zendesk/src/main/java/org/apache/camel/component/zendesk/ZendeskComponent.java
 
b/components/camel-zendesk/src/main/java/org/apache/camel/component/zendesk/ZendeskComponent.java
index 49162e6d7cf3..83fadf1b3071 100644
--- 
a/components/camel-zendesk/src/main/java/org/apache/camel/component/zendesk/ZendeskComponent.java
+++ 
b/components/camel-zendesk/src/main/java/org/apache/camel/component/zendesk/ZendeskComponent.java
@@ -46,9 +46,9 @@ public class ZendeskComponent extends 
AbstractApiComponent<ZendeskApiName, Zende
               description = "The password of the Zendesk user, sent with 
username in basic authentication. Takes precedence over oauthToken and token.")
     private String password;
     @Metadata(label = "advanced",
-              description = "A Zendesk client to share. An endpoint uses it 
only when all its settings, the method"
-                            + " included, are equal to those of the component 
configuration; otherwise the endpoint"
-                            + " creates its own client.")
+              description = "A Zendesk client to share. An endpoint uses it 
only when its connection settings"
+                            + " (serverUrl, username, password, token, 
oauthToken) are equal to those of the"
+                            + " component configuration; otherwise the 
endpoint creates its own client.")
     private Zendesk zendesk;
 
     public ZendeskComponent() {
@@ -151,6 +151,9 @@ public class ZendeskComponent extends 
AbstractApiComponent<ZendeskApiName, Zende
         if (endpointConfiguration.getOauthToken() == null) {
             endpointConfiguration.setOauthToken(oauthToken);
         }
+        if (endpointConfiguration.getToken() == null) {
+            endpointConfiguration.setToken(token);
+        }
 
         return new ZendeskEndpoint(uri, this, apiName, methodName, 
endpointConfiguration);
     }
diff --git 
a/components/camel-zendesk/src/main/java/org/apache/camel/component/zendesk/ZendeskEndpoint.java
 
b/components/camel-zendesk/src/main/java/org/apache/camel/component/zendesk/ZendeskEndpoint.java
index 72dd3b9f0fd5..0e7702bb2493 100644
--- 
a/components/camel-zendesk/src/main/java/org/apache/camel/component/zendesk/ZendeskEndpoint.java
+++ 
b/components/camel-zendesk/src/main/java/org/apache/camel/component/zendesk/ZendeskEndpoint.java
@@ -37,6 +37,7 @@ import org.apache.camel.support.component.AbstractApiEndpoint;
 import org.apache.camel.support.component.ApiMethod;
 import org.apache.camel.support.component.ApiMethodPropertiesHelper;
 import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ObjectHelper;
 import org.zendesk.client.v2.Zendesk;
 
 /**
@@ -74,6 +75,7 @@ public class ZendeskEndpoint extends 
AbstractApiEndpoint<ZendeskApiName, Zendesk
     private ZendeskConfiguration configuration;
 
     private Zendesk apiProxy;
+    private boolean ownedApiProxy = true;
 
     public ZendeskEndpoint(String uri, ZendeskComponent component, 
ZendeskApiName apiName, String methodName,
                            ZendeskConfiguration endpointConfiguration) {
@@ -131,7 +133,9 @@ public class ZendeskEndpoint extends 
AbstractApiEndpoint<ZendeskApiName, Zendesk
 
     @Override
     public void doStop() throws Exception {
-        IOHelper.close(apiProxy);
+        if (ownedApiProxy) {
+            IOHelper.close(apiProxy);
+        }
         super.doStop();
     }
 
@@ -156,13 +160,34 @@ public class ZendeskEndpoint extends 
AbstractApiEndpoint<ZendeskApiName, Zendesk
 
     private Zendesk getZendesk() {
         if (apiProxy == null) {
-            if (getConfiguration().equals(getComponent().getConfiguration())) {
-                apiProxy = getComponent().getZendesk();
+            Zendesk shared = getComponent().getZendesk();
+            if (shared != null && hasMatchingConnectionSettings()) {
+                apiProxy = shared;
+                ownedApiProxy = false;
             } else {
                 apiProxy = ZendeskHelper.create(getConfiguration());
+                ownedApiProxy = true;
             }
         }
         return apiProxy;
     }
 
+    /**
+     * Returns {@code true} when the endpoint's connection settings 
(serverUrl, username, password, token, oauthToken)
+     * match those of the component, so the component's shared Zendesk client 
can be reused instead of creating a new
+     * one per endpoint.
+     * <p>
+     * The comparison intentionally excludes {@code methodName} because that 
selects the API operation and does not
+     * affect client identity.
+     */
+    private boolean hasMatchingConnectionSettings() {
+        ZendeskComponent comp = getComponent();
+        ZendeskConfiguration cfg = getConfiguration();
+        return ObjectHelper.equal(cfg.getServerUrl(), comp.getServerUrl())
+                && ObjectHelper.equal(cfg.getUsername(), comp.getUsername())
+                && ObjectHelper.equal(cfg.getPassword(), comp.getPassword())
+                && ObjectHelper.equal(cfg.getToken(), comp.getToken())
+                && ObjectHelper.equal(cfg.getOauthToken(), 
comp.getOauthToken());
+    }
+
 }
diff --git 
a/components/camel-zendesk/src/test/java/org/apache/camel/component/zendesk/ZendeskComponentTest.java
 
b/components/camel-zendesk/src/test/java/org/apache/camel/component/zendesk/ZendeskComponentTest.java
new file mode 100644
index 000000000000..c75461f4b6ee
--- /dev/null
+++ 
b/components/camel-zendesk/src/test/java/org/apache/camel/component/zendesk/ZendeskComponentTest.java
@@ -0,0 +1,169 @@
+/*
+ * 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.zendesk;
+
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.zendesk.client.v2.Zendesk;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Unit tests for CAMEL-27971:
+ * <ol>
+ * <li>Token set on ZendeskComponent must be propagated to the endpoint 
configuration.</li>
+ * <li>Endpoints whose connection settings match those of the component must 
reuse the component's shared Zendesk
+ * client.</li>
+ * </ol>
+ */
+class ZendeskComponentTest {
+
+    private CamelContext context;
+    private ZendeskComponent component;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        context = new DefaultCamelContext();
+        component = new ZendeskComponent(context);
+        component.setServerUrl("https://test.zendesk.com";);
+        component.setUsername("[email protected]");
+        component.setToken("my-api-token");
+        context.addComponent("zendesk", component);
+        context.start();
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+        context.stop();
+    }
+
+    /**
+     * CAMEL-27971 bug 1: ZendeskComponent.createEndpoint() was missing the 
call to
+     * endpointConfiguration.setToken(token), so camel.component.zendesk.token 
had no effect on the endpoint.
+     */
+    @Test
+    void tokenSetOnComponentMustBePropagatedToEndpoint() throws Exception {
+        ZendeskEndpoint endpoint = (ZendeskEndpoint) 
context.getEndpoint("zendesk:getTicket");
+
+        assertNotNull(endpoint.getConfiguration().getToken(),
+                "token must be propagated from component to endpoint 
configuration");
+    }
+
+    /**
+     * When the component has no token, the endpoint configuration must also 
have no token.
+     */
+    @Test
+    void tokenIsNullWhenComponentHasNoToken() throws Exception {
+        ZendeskComponent comp2 = new ZendeskComponent(context);
+        comp2.setServerUrl("https://test.zendesk.com";);
+        comp2.setUsername("[email protected]");
+        // no token set on the component
+        context.addComponent("zendesk2", comp2);
+
+        ZendeskEndpoint endpoint = (ZendeskEndpoint) 
context.getEndpoint("zendesk2:getTicket");
+        assertNull(endpoint.getConfiguration().getToken(),
+                "token must be null when the component has no token");
+    }
+
+    /**
+     * CAMEL-27971 bug 2: ZendeskEndpoint.getZendesk() used to compare the 
full configurations (including methodName),
+     * which always differed, so the shared client was never reused. The 
endpoint must now reuse the component's shared
+     * client when the connection settings match.
+     */
+    @Test
+    void endpointWithMatchingSettingsMustReuseComponentClient() throws 
Exception {
+        // Inject a mock shared client into the component (bypassing 
ZendeskHelper.create())
+        Zendesk sharedClient = mock(Zendesk.class);
+        component.setZendesk(sharedClient);
+
+        ZendeskEndpoint endpoint = (ZendeskEndpoint) 
context.getEndpoint("zendesk:getTicket");
+
+        // getApiProxy() calls the private getZendesk(), which must return the 
shared client
+        Object proxy = endpoint.getApiProxy(null, Map.of());
+
+        assertSame(sharedClient, proxy,
+                "endpoint must reuse the component's shared Zendesk client 
when connection settings match");
+    }
+
+    /**
+     * When the endpoint overrides a connection setting (serverUrl), it must 
NOT reuse the component's client.
+     */
+    @Test
+    void endpointWithDifferentServerUrlMustNotReuseComponentClient() throws 
Exception {
+        Zendesk sharedClient = mock(Zendesk.class);
+        component.setZendesk(sharedClient);
+
+        // Override serverUrl in the URI — the endpoint gets its own client
+        ZendeskEndpoint endpoint
+                = (ZendeskEndpoint) 
context.getEndpoint("zendesk:getTicket?serverUrl=https://other.zendesk.com";);
+
+        Object proxy = endpoint.getApiProxy(null, Map.of());
+
+        // must NOT be the shared client — the connection settings differ
+        assertNotSame(sharedClient, proxy,
+                "endpoint with a different serverUrl must not reuse the 
component's shared client");
+    }
+
+    /**
+     * CAMEL-27971 null-client guard: when only component options are set (no 
Zendesk object injected), the endpoint
+     * must create its own client instead of returning null. Before the fix, 
hasMatchingConnectionSettings() was always
+     * true (options are copied to the endpoint in createEndpoint), so 
getZendesk() would return
+     * getComponent().getZendesk() == null, causing an NPE at invoke time.
+     */
+    @Test
+    void endpointMustCreateClientWhenComponentHasNoSharedClient() throws 
Exception {
+        // No setZendesk() call — component has only options, no shared 
Zendesk instance
+        ZendeskEndpoint endpoint = (ZendeskEndpoint) 
context.getEndpoint("zendesk:getTicket");
+
+        Object proxy = endpoint.getApiProxy(null, Map.of());
+
+        assertNotNull(proxy, "endpoint must create its own client when the 
component has no shared Zendesk instance");
+    }
+
+    /**
+     * CAMEL-27971 double-close guard: stopping one endpoint that shares the 
component's Zendesk client must NOT close
+     * that shared client. Only an endpoint that created its own client must 
close it on stop.
+     */
+    @Test
+    void stoppingSharedEndpointMustNotCloseComponentClient() throws Exception {
+        Zendesk sharedClient = mock(Zendesk.class);
+        component.setZendesk(sharedClient);
+
+        ZendeskEndpoint endpoint = (ZendeskEndpoint) 
context.getEndpoint("zendesk:getTicket");
+        endpoint.start();
+
+        // prime the shared proxy
+        assertSame(sharedClient, endpoint.getApiProxy(null, Map.of()));
+
+        // stopping the endpoint must not close the shared client
+        endpoint.stop();
+
+        // The component's shared Zendesk instance must still be alive (no 
close() call on mock)
+        verify(sharedClient, never()).close();
+    }
+}
diff --git 
a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/ZendeskComponentBuilderFactory.java
 
b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/ZendeskComponentBuilderFactory.java
index 875ca4b6903d..7ce64bb956eb 100644
--- 
a/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/ZendeskComponentBuilderFactory.java
+++ 
b/dsl/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/ZendeskComponentBuilderFactory.java
@@ -160,9 +160,10 @@ public interface ZendeskComponentBuilderFactory {
         }
     
         /**
-         * A Zendesk client to share. An endpoint uses it only when all its
-         * settings, the method included, are equal to those of the component
-         * configuration; otherwise the endpoint creates its own client.
+         * A Zendesk client to share. An endpoint uses it only when its
+         * connection settings (serverUrl, username, password, token,
+         * oauthToken) are equal to those of the component configuration;
+         * otherwise the endpoint creates its own client.
          * 
          * The option is a:
          * &lt;code&gt;org.zendesk.client.v2.Zendesk&lt;/code&gt; type.

Reply via email to