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 aa31e39acf4b CAMEL-27191: camel-google-bigquery - convert the
useAsInsertId field to a String instead of casting it (#30634)
aa31e39acf4b is described below
commit aa31e39acf4b0c630eb7eb3194cb861682b1d15a
Author: Urmila Unni <[email protected]>
AuthorDate: Mon Nov 23 14:02:32 2026 +0530
CAMEL-27191: camel-google-bigquery - convert the useAsInsertId field to a
String instead of casting it (#30634)
A numeric insert id field, such as an Integer or Long order id, failed the
exchange with a ClassCastException. The
useAsInsertId field is now converted to a String with the type converter of
the CamelContext of the endpoint, which
works without an exchange, as for the rows of a List body. It matches the
insert id header, and turns a byte[] field
into its text.
Co-authored-by: Claude <[email protected]>
---
.../google/bigquery/GoogleBigQueryProducer.java | 4 +-
.../google/bigquery/unit/BaseBigQueryTest.java | 1 +
.../bigquery/unit/GoogleBigQueryProducerTest.java | 44 ++++++++++++++++++++++
3 files changed, 48 insertions(+), 1 deletion(-)
diff --git
a/components/camel-google/camel-google-bigquery/src/main/java/org/apache/camel/component/google/bigquery/GoogleBigQueryProducer.java
b/components/camel-google/camel-google-bigquery/src/main/java/org/apache/camel/component/google/bigquery/GoogleBigQueryProducer.java
index 8fa4f49432b0..b1cdc6d6a92f 100644
---
a/components/camel-google/camel-google-bigquery/src/main/java/org/apache/camel/component/google/bigquery/GoogleBigQueryProducer.java
+++
b/components/camel-google/camel-google-bigquery/src/main/java/org/apache/camel/component/google/bigquery/GoogleBigQueryProducer.java
@@ -240,7 +240,9 @@ public class GoogleBigQueryProducer extends DefaultProducer
{
private InsertAllRequest.RowToInsert createRowRequest(Exchange exchange,
Map<String, Object> object) {
String insertId = null;
if (configuration.getUseAsInsertId() != null) {
- insertId = (String) (object.get(configuration.getUseAsInsertId()));
+ // convert, as the field can be a number such as an order or
sequence id, or bytes
+ insertId =
getEndpoint().getCamelContext().getTypeConverter().convertTo(String.class,
+ object.get(configuration.getUseAsInsertId()));
} else {
if (exchange != null) {
insertId =
exchange.getIn().getHeader(GoogleBigQueryConstants.INSERT_ID, String.class);
diff --git
a/components/camel-google/camel-google-bigquery/src/test/java/org/apache/camel/component/google/bigquery/unit/BaseBigQueryTest.java
b/components/camel-google/camel-google-bigquery/src/test/java/org/apache/camel/component/google/bigquery/unit/BaseBigQueryTest.java
index fd5e93e53e71..b5f56ec35f3d 100644
---
a/components/camel-google/camel-google-bigquery/src/test/java/org/apache/camel/component/google/bigquery/unit/BaseBigQueryTest.java
+++
b/components/camel-google/camel-google-bigquery/src/test/java/org/apache/camel/component/google/bigquery/unit/BaseBigQueryTest.java
@@ -42,6 +42,7 @@ public abstract class BaseBigQueryTest extends
CamelTestSupport {
@BeforeEach
public void init() throws Exception {
setupBigqueryMock();
+ when(endpoint.getCamelContext()).thenReturn(context);
producer = createProducer();
}
diff --git
a/components/camel-google/camel-google-bigquery/src/test/java/org/apache/camel/component/google/bigquery/unit/GoogleBigQueryProducerTest.java
b/components/camel-google/camel-google-bigquery/src/test/java/org/apache/camel/component/google/bigquery/unit/GoogleBigQueryProducerTest.java
index 265825794dd5..d4368f8f80a7 100644
---
a/components/camel-google/camel-google-bigquery/src/test/java/org/apache/camel/component/google/bigquery/unit/GoogleBigQueryProducerTest.java
+++
b/components/camel-google/camel-google-bigquery/src/test/java/org/apache/camel/component/google/bigquery/unit/GoogleBigQueryProducerTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.google.bigquery.unit;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -71,6 +72,49 @@ public class GoogleBigQueryProducerTest extends
BaseBigQueryTest {
assertEquals("value1", requests.get(0).getRows().get(0).getId());
}
+ @Test
+ public void useAsInsertIdWithANumericField() throws Exception {
+ configuration.setUseAsInsertId("orderId");
+ Map<String, Object> object = new HashMap<>();
+ object.put("orderId", 42);
+ producer.process(createExchangeWithBody(object));
+ ArgumentCaptor<InsertAllRequest> dataCaptor =
ArgumentCaptor.forClass(InsertAllRequest.class);
+ verify(bigquery).insertAll(dataCaptor.capture());
+ List<InsertAllRequest> requests = dataCaptor.getAllValues();
+ assertEquals(1, requests.size());
+ assertEquals(1, requests.get(0).getRows().size());
+ assertEquals("42", requests.get(0).getRows().get(0).getId());
+ }
+
+ @Test
+ public void useAsInsertIdWithNumericFieldsInAList() throws Exception {
+ configuration.setUseAsInsertId("orderId");
+ List<Map<String, Object>> messages = new ArrayList<>();
+ messages.add(Map.of("orderId", 1L));
+ messages.add(Map.of("orderId", 2L));
+ producer.process(createExchangeWithBody(messages));
+ ArgumentCaptor<InsertAllRequest> dataCaptor =
ArgumentCaptor.forClass(InsertAllRequest.class);
+ verify(bigquery).insertAll(dataCaptor.capture());
+ List<InsertAllRequest> requests = dataCaptor.getAllValues();
+ assertEquals(1, requests.size());
+ assertEquals(2, requests.get(0).getRows().size());
+ assertEquals("1", requests.get(0).getRows().get(0).getId());
+ assertEquals("2", requests.get(0).getRows().get(1).getId());
+ }
+
+ @Test
+ public void useAsInsertIdWithABytesField() throws Exception {
+ configuration.setUseAsInsertId("orderId");
+ Map<String, Object> object = new HashMap<>();
+ object.put("orderId", "order-1".getBytes(StandardCharsets.UTF_8));
+ producer.process(createExchangeWithBody(object));
+ ArgumentCaptor<InsertAllRequest> dataCaptor =
ArgumentCaptor.forClass(InsertAllRequest.class);
+ verify(bigquery).insertAll(dataCaptor.capture());
+ List<InsertAllRequest> requests = dataCaptor.getAllValues();
+ assertEquals(1, requests.size());
+ assertEquals("order-1", requests.get(0).getRows().get(0).getId());
+ }
+
@Test
public void listOfMessages() throws Exception {
List<Map<String, String>> messages = new ArrayList<>();