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 9fe60f293d46 CAMEL-27259: camel-mongodb - insert of a byte[] body 
fails with ClassCastException (#30454)
9fe60f293d46 is described below

commit 9fe60f293d46724714ee61444ed16f6d3606d3d2
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Nov 19 22:10:16 2026 +0100

    CAMEL-27259: camel-mongodb - insert of a byte[] body fails with 
ClassCastException (#30454)
    
    The insert operation took any array body as a batch and cast it to
    Object[], so a byte[] body, such as JSON text from a Kafka consumer,
    failed with a ClassCastException. Only an Object[] is now a batch; a
    byte[] is converted to a Document like JSON text.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/component/mongodb/MongoDbProducer.java   |  5 +-
 .../mongodb/MongoDbProducerInsertBodyTest.java     | 97 ++++++++++++++++++++++
 2 files changed, 99 insertions(+), 3 deletions(-)

diff --git 
a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
 
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
index 0cc7af5dc8ea..2d4bae6d0779 100644
--- 
a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
+++ 
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java
@@ -440,9 +440,8 @@ public class MongoDbProducer extends DefaultProducer {
             // is it batch or single insert
             boolean singleInsert = true;
             Object insert = exchange.getIn().getBody();
-            boolean array = insert != null && insert.getClass().isArray();
-            if (array) {
-                Object[] arr = (Object[]) insert;
+            // an array of objects is a batch; a byte[] is a payload, such as 
JSON text, converted to a Document below
+            if (insert instanceof Object[] arr) {
                 insert = Arrays.asList(arr);
             }
             if (insert instanceof Collection) {
diff --git 
a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbProducerInsertBodyTest.java
 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbProducerInsertBodyTest.java
new file mode 100644
index 000000000000..220c662671d0
--- /dev/null
+++ 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbProducerInsertBodyTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.mongodb;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+import com.mongodb.client.MongoClient;
+import com.mongodb.client.MongoCollection;
+import com.mongodb.client.MongoDatabase;
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.bson.Document;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * A byte[] body is an array, but a payload, not a batch: it is inserted as 
one document, like JSON text.
+ */
+class MongoDbProducerInsertBodyTest extends CamelTestSupport {
+
+    private final MongoCollection<Document> collection = 
mock(MongoCollection.class);
+
+    @Test
+    void testInsertByteArrayBodyAsOneDocument() {
+        template.requestBody("direct:insert", "{\"name\": \"order-1\", 
\"qty\": 2}".getBytes(StandardCharsets.UTF_8));
+
+        ArgumentCaptor<Document> inserted = 
ArgumentCaptor.forClass(Document.class);
+        verify(collection).insertOne(inserted.capture());
+        assertEquals("order-1", inserted.getValue().getString("name"));
+        assertEquals(2, inserted.getValue().getInteger("qty"));
+    }
+
+    @Test
+    @SuppressWarnings("unchecked")
+    void testInsertObjectArrayBodyAsBatch() {
+        Object[] batch = { new Document("name", "a"), new Document("name", 
"b") };
+        template.requestBody("direct:insert", batch);
+
+        ArgumentCaptor<List<Document>> inserted = 
ArgumentCaptor.forClass(List.class);
+        verify(collection).insertMany(inserted.capture());
+        assertEquals(2, inserted.getValue().size());
+        assertEquals("b", inserted.getValue().get(1).getString("name"));
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        final CamelContext context = super.createCamelContext();
+
+        when(collection.withWriteConcern(any())).thenReturn(collection);
+
+        final MongoDatabase database = mock(MongoDatabase.class);
+        when(database.getCollection(anyString(), 
eq(Document.class))).thenReturn(collection);
+
+        final MongoClient client = mock(MongoClient.class);
+        when(client.getDatabase(anyString())).thenReturn(database);
+        
when(client.listDatabaseNames()).thenReturn(mock(com.mongodb.client.MongoIterable.class));
+
+        context.getRegistry().bind("mongoClient", client);
+        return context;
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:insert")
+                        
.to("mongodb:mongoClient?database=test&collection=c&operation=insert&dynamicity=false");
+            }
+        };
+    }
+}

Reply via email to