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 1cf4e1c149b7 CAMEL-26910: camel-kafka - KafkaKeyValueRepository skips 
a put or delete without a key, or a put it cannot read, in its topic (#29997)
1cf4e1c149b7 is described below

commit 1cf4e1c149b722d99fc5559a45257c84c26e1eb1
Author: allthingssecurity <[email protected]>
AuthorDate: Sun Nov 15 14:14:53 2026 +0530

    CAMEL-26910: camel-kafka - KafkaKeyValueRepository skips a put or delete 
without a key, or a put it cannot read, in its topic (#29997)
    
    A put or delete record without a key made the cache of 
KafkaKeyValueRepository throw a NullPointerException, and a put whose value 
could not be deserialized (a class not on the classpath, one the 
deserializationFilter rejects, or bytes that are not a serialized object) threw 
a RuntimeCamelException. At start the repository then failed for as long as the 
record was in the topic, and in the background poll the following records of 
that poll were not applied.
    
    addToCache now logs such a record at WARN (topic, partition, offset and 
cause) and skips it, and the cache keeps what it had for the key; a clear is 
applied as before, and a malformed deserializationFilter still fails. 
key-value-repository.adoc says so. New tests in KafkaKeyValueRepositoryTest.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../keyvalue/kafka/KafkaKeyValueRepository.java    | 22 +++++-
 .../kafka/KafkaKeyValueRepositoryTest.java         | 92 +++++++++++++++++++++-
 .../modules/ROOT/pages/key-value-repository.adoc   |  3 +
 3 files changed, 113 insertions(+), 4 deletions(-)

diff --git 
a/components/camel-kafka/src/main/java/org/apache/camel/processor/keyvalue/kafka/KafkaKeyValueRepository.java
 
b/components/camel-kafka/src/main/java/org/apache/camel/processor/keyvalue/kafka/KafkaKeyValueRepository.java
index 9e058b991c73..d0f76f27b259 100644
--- 
a/components/camel-kafka/src/main/java/org/apache/camel/processor/keyvalue/kafka/KafkaKeyValueRepository.java
+++ 
b/components/camel-kafka/src/main/java/org/apache/camel/processor/keyvalue/kafka/KafkaKeyValueRepository.java
@@ -349,6 +349,13 @@ public class KafkaKeyValueRepository extends 
ServiceSupport implements KeyValueR
         }
         byte action = data[0];
         String key = record.key();
+        if (key == null && (action == ACTION_PUT || action == ACTION_DELETE)) {
+            // the repository never writes such a record (the cache refuses a 
null key first); applied, it would fail
+            // the start, or the rest of the poll
+            LOG.warn("Put or delete record without a key on topic:{}, 
partition:{}, offset:{}. Ignoring.",
+                    record.topic(), record.partition(), record.offset());
+            return;
+        }
         if (action == ACTION_PUT) {
             if (data.length < 10) {
                 LOG.warn("Malformed put record on topic:{}, partition:{}, 
offset:{}. Ignoring.",
@@ -356,7 +363,20 @@ public class KafkaKeyValueRepository extends 
ServiceSupport implements KeyValueR
                 return;
             }
             long expiresAt = deserializeExpiresAt(data);
-            Object value = deserializeValue(data);
+            Object value;
+            try {
+                value = deserializeValue(data);
+            } catch (RuntimeCamelException e) {
+                // such as a class that is not on the classpath of this 
application, or that the deserialization filter
+                // does not allow. The cache keeps what it has for the key: 
the poller also reads back the puts of this
+                // repository, whose value must stay
+                Throwable cause = e.getCause() != null ? e.getCause() : e;
+                LOG.warn("Cannot read the value of the put record for key:{} 
on topic:{}, partition:{}, offset:{} due to: {}."
+                         + " Ignoring the record.",
+                        key, record.topic(), record.partition(), 
record.offset(), cause.toString());
+                LOG.debug("Cannot read the value of the put record for 
key:{}", key, e);
+                return;
+            }
             LOG.debug("Adding to cache key:{}", key);
             cache.put(key, new CacheEntry(value, expiresAt));
         } else if (action == ACTION_DELETE) {
diff --git 
a/components/camel-kafka/src/test/java/org/apache/camel/processor/keyvalue/kafka/KafkaKeyValueRepositoryTest.java
 
b/components/camel-kafka/src/test/java/org/apache/camel/processor/keyvalue/kafka/KafkaKeyValueRepositoryTest.java
index 4d29116e7fb8..c2f214b2d757 100644
--- 
a/components/camel-kafka/src/test/java/org/apache/camel/processor/keyvalue/kafka/KafkaKeyValueRepositoryTest.java
+++ 
b/components/camel-kafka/src/test/java/org/apache/camel/processor/keyvalue/kafka/KafkaKeyValueRepositoryTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.processor.keyvalue.kafka;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.util.Arrays;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
@@ -39,8 +40,9 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNull;
 
 /**
- * Unit tests for the expiry handling of {@link KafkaKeyValueRepository}, 
without a Kafka broker: the changelog sends to
- * a {@link MockProducer}, and the records of the topic are applied by calling 
the repository's poller callback.
+ * Unit tests of {@link KafkaKeyValueRepository} (expiry, and the records of 
the topic it cannot apply), without a Kafka
+ * broker: the changelog sends to a {@link MockProducer}, and the records of 
the topic are applied by calling the
+ * repository's poller callback.
  */
 public class KafkaKeyValueRepositoryTest {
 
@@ -147,14 +149,98 @@ public class KafkaKeyValueRepositoryTest {
         assertEquals(0, producer.history().size());
     }
 
+    @Test
+    void putWithoutKeyIsSkippedAndTheNextRecordsAreApplied() throws Exception {
+        applyPut("key1", "a", 0);
+
+        // the repository never writes such a record, another application or a 
tool can: before, the
+        // NullPointerException of the cache failed the start, or the rest of 
the poll
+        apply(null, putPayload("b", 0));
+        applyPut("key2", "c", 0);
+
+        assertEquals("a", repository.get("key1"));
+        assertEquals("c", repository.get("key2"));
+        assertEquals(Set.of("key1", "key2"), repository.keys());
+    }
+
+    @Test
+    void deleteWithoutKeyIsSkippedAndTheNextRecordsAreApplied() throws 
Exception {
+        applyPut("key1", "a", 0);
+
+        apply(null, new byte[] { 1 });
+        applyPut("key2", "c", 0);
+
+        assertEquals(Set.of("key1", "key2"), repository.keys());
+    }
+
+    @Test
+    void clearWithoutKeyIsApplied() throws Exception {
+        // control: the repository writes a clear without a key
+        applyPut("key1", "a", 0);
+
+        apply(null, new byte[] { 2 });
+
+        assertEquals(0, repository.size());
+    }
+
+    @Test
+    void putWhoseValueCannotBeReadIsSkipped() throws Exception {
+        applyPut("key1", "old", 0);
+
+        // a value that is not a serialized object: skipped, the cache keeps 
the value it has
+        byte[] payload = putPayload("ignored", 0);
+        apply("key1", Arrays.copyOf(payload, 12));
+        applyPut("key2", "b", 0);
+
+        assertEquals("old", repository.get("key1"));
+        assertEquals("b", repository.get("key2"));
+    }
+
+    @Test
+    void ownPutWhoseValueTheDeserializationFilterRejectsKeepsItsValue() throws 
Exception {
+        repository.setDeserializationFilter("java.lang.String;!*");
+
+        // the poller reads back the put of this repository: the value stays 
in the cache
+        repository.put("key1", 42, null);
+        apply("key1", producer.history().get(0).value());
+
+        assertEquals(42, repository.get("key1"));
+    }
+
+    @Test
+    void putWhoseValueTheDeserializationFilterRejectsIsSkipped() throws 
Exception {
+        repository.setDeserializationFilter("java.lang.String;!*");
+
+        // such as a class that the deserialization filter does not allow
+        applyPut("key1", 42, 0);
+        applyPut("key2", "b", 0);
+
+        assertNull(repository.get("key1"));
+        assertEquals("b", repository.get("key2"));
+    }
+
     /**
      * Applies a put record of the topic to the cache, as the poller does.
      */
     private void applyPut(String key, Object value, long expiresAt) {
+        apply(key, putPayload(value, expiresAt));
+    }
+
+    private byte[] putPayload(Object value, long expiresAt) {
         try {
             Method serialize = 
KafkaKeyValueRepository.class.getDeclaredMethod("serializePutAction", 
Object.class, long.class);
             serialize.setAccessible(true);
-            byte[] payload = (byte[]) serialize.invoke(repository, value, 
expiresAt);
+            return (byte[]) serialize.invoke(repository, value, expiresAt);
+        } catch (ReflectiveOperationException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
+    /**
+     * Applies a record of the topic to the cache, as the poller does.
+     */
+    private void apply(String key, byte[] payload) {
+        try {
             Method addToCache = 
KafkaKeyValueRepository.class.getDeclaredMethod("addToCache", 
ConsumerRecord.class);
             addToCache.setAccessible(true);
             addToCache.invoke(repository, new ConsumerRecord<>("test-topic", 
0, 0L, key, payload));
diff --git a/docs/user-manual/modules/ROOT/pages/key-value-repository.adoc 
b/docs/user-manual/modules/ROOT/pages/key-value-repository.adoc
index 4ecd6ae4a974..670324de81ae 100644
--- a/docs/user-manual/modules/ROOT/pages/key-value-repository.adoc
+++ b/docs/user-manual/modules/ROOT/pages/key-value-repository.adoc
@@ -182,6 +182,9 @@ repo.setTopic("camel-kvr");  // optional
 
 Entries are replayed from the topic on startup into an in-memory 
`ConcurrentHashMap` cache.
 TTL is stored as an epoch-millis timestamp in the value envelope.
+A put read from the topic whose value cannot be deserialized (for example a 
class that is not on the classpath, or
+that the `deserializationFilter` does not allow, see Serialization below) is 
skipped with a WARN, and the cache keeps
+what it has for that key.
 
 === Caffeine
 

Reply via email to