You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/02/02 09:32:09 UTC

[camel] branch camel-3.7.x updated: CAMEL-16114: ClassResolver should be able to load array classes, which can be used by dataformats such as jackson. Thanks to Bob Paulin for the unit test.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-3.7.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-3.7.x by this push:
     new a71b455  CAMEL-16114: ClassResolver should be able to load array classes, which can be used by dataformats such as jackson. Thanks to Bob Paulin for the unit test.
a71b455 is described below

commit a71b4554ce94bf552738b4a9f0c99fa4a2250790
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Feb 2 10:29:21 2021 +0100

    CAMEL-16114: ClassResolver should be able to load array classes, which can be used by dataformats such as jackson. Thanks to Bob Paulin for the unit test.
---
 .../JacksonMarshalUnmarshalPojoArrayTest.java      | 72 ++++++++++++++++++++++
 .../org/apache/camel/builder/DataFormatClause.java | 11 ++++
 .../java/org/apache/camel/util/ObjectHelper.java   | 15 +++++
 .../test/java/org/apache/camel/util/MyPojo.java    | 30 +++++++++
 .../camel/util/ObjectHelperLoadClassTest.java      | 43 +++++++++++++
 5 files changed, 171 insertions(+)

diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonMarshalUnmarshalPojoArrayTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonMarshalUnmarshalPojoArrayTest.java
new file mode 100644
index 0000000..1c44da9
--- /dev/null
+++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonMarshalUnmarshalPojoArrayTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.jackson;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class JacksonMarshalUnmarshalPojoArrayTest extends CamelTestSupport {
+
+    @Test
+    public void testUnmarshalPojoArray() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:endArray");
+        mock.expectedMessageCount(1);
+        mock.message(0).body().isInstanceOf(Pojo[].class);
+
+        String json = "[{\"text\":\"Camel\"}, {\"text\":\"World\"}]";
+        template.sendBody("direct:beginArray", json);
+
+        assertMockEndpointsSatisfied();
+
+        Pojo[] array = mock.getReceivedExchanges().get(0).getIn().getBody(Pojo[].class);
+        assertNotNull(array);
+        assertEquals(2, array.length);
+
+        Pojo pojo = array[0];
+        assertEquals("Camel", pojo.getText());
+        pojo = array[1];
+        assertEquals("World", pojo.getText());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:beginArray").unmarshal().json(Pojo[].class).to("mock:endArray");
+            }
+        };
+    }
+
+    public static class Pojo {
+        private String text;
+
+        public String getText() {
+            return text;
+        }
+
+        public void setText(String text) {
+            this.text = text;
+        }
+    }
+
+}
diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/DataFormatClause.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/DataFormatClause.java
index aa836f0..5713481 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/builder/DataFormatClause.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/DataFormatClause.java
@@ -626,6 +626,17 @@ public class DataFormatClause<T extends ProcessorDefinition<?>> {
      * Uses the Jackson JSON data format
      *
      * @param unmarshalType unmarshal type for json jackson type
+     */
+    public T json(Class<?> unmarshalType) {
+        JsonDataFormat json = new JsonDataFormat(JsonLibrary.Jackson);
+        json.setUnmarshalType(unmarshalType);
+        return dataFormat(json);
+    }
+
+    /**
+     * Uses the Jackson JSON data format
+     *
+     * @param unmarshalType unmarshal type for json jackson type
      * @param jsonView      the view type for json jackson type
      */
     public T json(Class<?> unmarshalType, Class<?> jsonView) {
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
index 618dba5..724a9cb 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
@@ -372,9 +373,19 @@ public final class ObjectHelper {
             return null;
         }
 
+        boolean array = false;
+
         // Try simple type first
         Class<?> clazz = loadSimpleType(name);
         if (clazz == null) {
+            // special for array as we need to load the class and then after that instantiate an array class type
+            if (name.endsWith("[]")) {
+                name = name.substring(0, name.length() - 2);
+                array = true;
+            }
+        }
+
+        if (clazz == null) {
             // try context class loader
             clazz = doLoadClass(name, Thread.currentThread().getContextClassLoader());
         }
@@ -386,6 +397,10 @@ public final class ObjectHelper {
             // and fallback to the loader the loaded the ObjectHelper class
             clazz = doLoadClass(name, ObjectHelper.class.getClassLoader());
         }
+        if (clazz != null && array) {
+            Object arr = Array.newInstance(clazz, 0);
+            clazz = arr.getClass();
+        }
 
         if (clazz == null) {
             if (needToWarn) {
diff --git a/core/camel-util/src/test/java/org/apache/camel/util/MyPojo.java b/core/camel-util/src/test/java/org/apache/camel/util/MyPojo.java
new file mode 100644
index 0000000..5412609
--- /dev/null
+++ b/core/camel-util/src/test/java/org/apache/camel/util/MyPojo.java
@@ -0,0 +1,30 @@
+/*
+ * 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.util;
+
+public class MyPojo {
+
+    private String name;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}
diff --git a/core/camel-util/src/test/java/org/apache/camel/util/ObjectHelperLoadClassTest.java b/core/camel-util/src/test/java/org/apache/camel/util/ObjectHelperLoadClassTest.java
new file mode 100644
index 0000000..66175d2
--- /dev/null
+++ b/core/camel-util/src/test/java/org/apache/camel/util/ObjectHelperLoadClassTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.util;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class ObjectHelperLoadClassTest {
+
+    @Test
+    public void testLoadClass() throws Exception {
+        Class<?> clazz = ObjectHelper.loadClass("int");
+        Assertions.assertEquals(clazz.getName(), "int");
+
+        clazz = ObjectHelper.loadClass("java.lang.String");
+        Assertions.assertEquals(clazz.getName(), "java.lang.String");
+
+        clazz = ObjectHelper.loadClass("java.lang.String[]");
+        Assertions.assertTrue(clazz.isArray());
+
+        clazz = ObjectHelper.loadClass("org.apache.camel.util.MyPojo");
+        Assertions.assertEquals(clazz.getName(), "org.apache.camel.util.MyPojo");
+        Assertions.assertFalse(clazz.isArray());
+
+        clazz = ObjectHelper.loadClass("org.apache.camel.util.MyPojo[]");
+        Assertions.assertTrue(clazz.isArray());
+    }
+
+}