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 48894fe86592 CAMEL-26160: camel-servicenow - remove dead Map branch in 
AbstractServiceNowProcessor.unwrap (#28935)
48894fe86592 is described below

commit 48894fe86592a61ad5c6e286f1439341e831d6fe
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Nov 2 20:49:33 2026 +0100

    CAMEL-26160: camel-servicenow - remove dead Map branch in 
AbstractServiceNowProcessor.unwrap (#28935)
    
    …viceNowProcessor.unwrap
    
    unwrap tested model.isInstance(Map.class), which asks whether the Class
    object Map.class is an instance of the model and is never true, so the
    branch reading an array as a plain List was dead. The other branch
    already reads a Map model as a List of Maps, and keeps the element type
    of a model that extends a Map, which Map.class.isAssignableFrom(model)
    would have lost. Remove the dead branch and add unit tests for unwrap.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../servicenow/AbstractServiceNowProcessor.java    |  11 +-
 .../component/servicenow/ServiceNowUnwrapTest.java | 117 +++++++++++++++++++++
 2 files changed, 120 insertions(+), 8 deletions(-)

diff --git 
a/components/camel-servicenow/camel-servicenow-component/src/main/java/org/apache/camel/component/servicenow/AbstractServiceNowProcessor.java
 
b/components/camel-servicenow/camel-servicenow-component/src/main/java/org/apache/camel/component/servicenow/AbstractServiceNowProcessor.java
index e792e58cac28..c1aee7230e0d 100644
--- 
a/components/camel-servicenow/camel-servicenow-component/src/main/java/org/apache/camel/component/servicenow/AbstractServiceNowProcessor.java
+++ 
b/components/camel-servicenow/camel-servicenow-component/src/main/java/org/apache/camel/component/servicenow/AbstractServiceNowProcessor.java
@@ -167,14 +167,9 @@ public abstract class AbstractServiceNowProcessor 
implements Processor {
         final Object result;
 
         if (node.isArray()) {
-            if (model.isInstance(Map.class)) {
-                // If the model is a Map, there's no need to use any
-                // specific JavaType to instruct Jackson about the
-                // expected element type
-                result = mapper.treeToValue(node, List.class);
-            } else {
-                result = mapper.readValue(node.traverse(), 
javaTypeCache.get(model));
-            }
+            // read as a List of the model type; this also covers a Map model,
+            // and keeps the element type of a model that extends a Map
+            result = mapper.readValue(node.traverse(), 
javaTypeCache.get(model));
         } else {
             result = mapper.treeToValue(node, model);
         }
diff --git 
a/components/camel-servicenow/camel-servicenow-component/src/test/java/org/apache/camel/component/servicenow/ServiceNowUnwrapTest.java
 
b/components/camel-servicenow/camel-servicenow-component/src/test/java/org/apache/camel/component/servicenow/ServiceNowUnwrapTest.java
new file mode 100644
index 000000000000..297628b6a580
--- /dev/null
+++ 
b/components/camel-servicenow/camel-servicenow-component/src/test/java/org/apache/camel/component/servicenow/ServiceNowUnwrapTest.java
@@ -0,0 +1,117 @@
+/*
+ * 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.servicenow;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.servicenow.model.Incident;
+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 static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+/**
+ * Tests how a ServiceNow "result" node is unwrapped into the message body for 
the different model types, without
+ * calling a ServiceNow instance.
+ */
+public class ServiceNowUnwrapTest {
+
+    private static final String ARRAY = 
"[{\"sys_id\":\"1\",\"number\":\"INC1\"},{\"sys_id\":\"2\",\"number\":\"INC2\"}]";
+
+    private CamelContext context;
+    private AbstractServiceNowProcessor processor;
+
+    @BeforeEach
+    public void setUp() {
+        context = new DefaultCamelContext();
+        context.start();
+
+        ServiceNowEndpoint endpoint
+                = 
context.getEndpoint("servicenow:myinstance?userName=user&password=pwd", 
ServiceNowEndpoint.class);
+        processor = new AbstractServiceNowProcessor(endpoint) {
+        };
+    }
+
+    @AfterEach
+    public void tearDown() {
+        context.stop();
+    }
+
+    @Test
+    public void testArrayWithMapModel() throws Exception {
+        Object result = processor.unwrap(readTree(ARRAY), Map.class);
+
+        List<?> list = assertInstanceOf(List.class, result);
+        assertEquals(2, list.size());
+        Map<?, ?> first = assertInstanceOf(Map.class, list.get(0));
+        assertEquals("INC1", first.get("number"));
+        Map<?, ?> second = assertInstanceOf(Map.class, list.get(1));
+        assertEquals("INC2", second.get("number"));
+    }
+
+    @Test
+    public void testArrayWithMapSubclassModel() throws Exception {
+        Object result = processor.unwrap(readTree(ARRAY), IncidentMap.class);
+
+        List<?> list = assertInstanceOf(List.class, result);
+        assertEquals(2, list.size());
+        IncidentMap first = assertInstanceOf(IncidentMap.class, list.get(0));
+        assertEquals("INC1", first.get("number"));
+        IncidentMap second = assertInstanceOf(IncidentMap.class, list.get(1));
+        assertEquals("INC2", second.get("number"));
+    }
+
+    @Test
+    public void testArrayWithPojoModel() throws Exception {
+        Object result = processor.unwrap(readTree(ARRAY), Incident.class);
+
+        List<?> list = assertInstanceOf(List.class, result);
+        assertEquals(2, list.size());
+        Incident first = assertInstanceOf(Incident.class, list.get(0));
+        assertEquals("1", first.getId());
+        assertEquals("INC1", first.getNumber());
+    }
+
+    @Test
+    public void testObjectWithMapModel() throws Exception {
+        Object result = 
processor.unwrap(readTree("{\"sys_id\":\"1\",\"number\":\"INC1\"}"), Map.class);
+
+        Map<?, ?> map = assertInstanceOf(Map.class, result);
+        assertEquals("INC1", map.get("number"));
+    }
+
+    @Test
+    public void testArrayWithStringModel() throws Exception {
+        Object result = processor.unwrap(readTree(ARRAY), String.class);
+
+        assertEquals(ARRAY, result);
+    }
+
+    private JsonNode readTree(String json) throws Exception {
+        return processor.mapper.readTree(json);
+    }
+
+    public static class IncidentMap extends HashMap<String, Object> {
+    }
+}

Reply via email to