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 2018/03/17 09:55:55 UTC

[camel] 02/02: CAMEL-12357: Allow to turn off using default object mapper in camel-jackson

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

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

commit bce0b0fd39307cec75af4f0350e829746947cdf7
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Mar 17 10:54:02 2018 +0100

    CAMEL-12357: Allow to turn off using default object mapper in camel-jackson
---
 .../camel/model/dataformat/JsonDataFormat.java     | 22 ++++--
 .../src/main/docs/json-jackson-dataformat.adoc     |  3 +-
 .../camel/component/jackson/JacksonDataFormat.java | 11 ++-
 .../JacksonNotUseDefaultObjectMapperTest.java      | 79 ++++++++++++++++++++++
 .../springboot/JacksonDataFormatConfiguration.java | 12 ++++
 5 files changed, 121 insertions(+), 6 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java
index cc92c90..997319a 100644
--- a/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java
+++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/JsonDataFormat.java
@@ -16,9 +16,6 @@
  */
 package org.apache.camel.model.dataformat;
 
-import java.text.DateFormat;
-import java.util.TimeZone;
-
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
@@ -46,6 +43,9 @@ public class JsonDataFormat extends DataFormatDefinition {
     @XmlAttribute
     private String objectMapper;
     @XmlAttribute
+    @Metadata(defaultValue = "true")
+    private Boolean useDefaultObjectMapper;
+    @XmlAttribute
     private Boolean prettyPrint;
     @XmlAttribute
     @Metadata(defaultValue = "XStream")
@@ -82,7 +82,7 @@ public class JsonDataFormat extends DataFormatDefinition {
     private Boolean allowUnmarshallType;
     @XmlAttribute
     private String timezone;
-    
+
     public JsonDataFormat() {
         super("json");
     }
@@ -103,6 +103,17 @@ public class JsonDataFormat extends DataFormatDefinition {
         this.objectMapper = objectMapper;
     }
 
+    public Boolean getUseDefaultObjectMapper() {
+        return useDefaultObjectMapper;
+    }
+
+    /**
+     * Whether to lookup and use default Jackson ObjectMapper from the registry.
+     */
+    public void setUseDefaultObjectMapper(Boolean useDefaultObjectMapper) {
+        this.useDefaultObjectMapper = useDefaultObjectMapper;
+    }
+
     public Boolean getPrettyPrint() {
         return prettyPrint;
     }
@@ -398,6 +409,9 @@ public class JsonDataFormat extends DataFormatDefinition {
             String ref = objectMapper.startsWith("#") ? objectMapper : "#" + objectMapper;
             setProperty(camelContext, dataFormat, "objectMapper", ref);
         }
+        if (useDefaultObjectMapper != null) {
+            setProperty(camelContext, dataFormat, "useDefaultObjectMapper", useDefaultObjectMapper);
+        }
         if (unmarshalType != null) {
             setProperty(camelContext, dataFormat, "unmarshalType", unmarshalType);
         }
diff --git a/components/camel-jackson/src/main/docs/json-jackson-dataformat.adoc b/components/camel-jackson/src/main/docs/json-jackson-dataformat.adoc
index fd17aa8..4b7cd27 100644
--- a/components/camel-jackson/src/main/docs/json-jackson-dataformat.adoc
+++ b/components/camel-jackson/src/main/docs/json-jackson-dataformat.adoc
@@ -18,7 +18,7 @@ from("activemq:My.Queue").
 
 
 // dataformat options: START
-The JSon Jackson dataformat supports 18 options which are listed below.
+The JSon Jackson dataformat supports 19 options which are listed below.
 
 
 
@@ -26,6 +26,7 @@ The JSon Jackson dataformat supports 18 options which are listed below.
 |===
 | Name | Default | Java Type | Description
 | objectMapper |  | String | Lookup and use the existing ObjectMapper with the given id when using Jackson.
+| useDefaultObjectMapper | true | Boolean | Whether to lookup and use default Jackson ObjectMapper from the registry.
 | prettyPrint | false | Boolean | To enable pretty printing output nicely formatted. Is by default false.
 | library | XStream | JsonLibrary | Which json library to use.
 | unmarshalTypeName |  | String | Class name of the java type to use when unarmshalling
diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
index dd86f70..c01418e 100644
--- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
+++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
@@ -58,6 +58,7 @@ public class JacksonDataFormat extends ServiceSupport implements DataFormat, Dat
 
     private CamelContext camelContext;
     private ObjectMapper objectMapper;
+    private boolean useDefaultObjectMapper = true;
     private Class<? extends Collection> collectionType;
     private List<Module> modules;
     private String moduleClassNames;
@@ -206,6 +207,14 @@ public class JacksonDataFormat extends ServiceSupport implements DataFormat, Dat
         this.objectMapper = objectMapper;
     }
 
+    public boolean isUseDefaultObjectMapper() {
+        return useDefaultObjectMapper;
+    }
+
+    public void setUseDefaultObjectMapper(boolean useDefaultObjectMapper) {
+        this.useDefaultObjectMapper = useDefaultObjectMapper;
+    }
+
     public Class<?> getUnmarshalType() {
         return this.unmarshalType;
     }
@@ -469,7 +478,7 @@ public class JacksonDataFormat extends ServiceSupport implements DataFormat, Dat
     protected void doStart() throws Exception {
         if (objectMapper == null) {
             // lookup if there is a single default mapper we can use
-            if (camelContext != null) {
+            if (useDefaultObjectMapper && camelContext != null) {
                 Set<ObjectMapper> set = camelContext.getRegistry().findByType(ObjectMapper.class);
                 if (set.size() == 1) {
                     objectMapper = set.iterator().next();
diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonNotUseDefaultObjectMapperTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonNotUseDefaultObjectMapperTest.java
new file mode 100644
index 0000000..9eee816
--- /dev/null
+++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonNotUseDefaultObjectMapperTest.java
@@ -0,0 +1,79 @@
+/**
+ * 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 java.util.HashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JacksonNotUseDefaultObjectMapperTest extends CamelTestSupport {
+
+    private JacksonDataFormat df = new JacksonDataFormat();
+    private ObjectMapper objectMapper = new ObjectMapper();
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("myMapper", objectMapper);
+        return jndi;
+    }
+
+    @Override
+    public void setUp() throws Exception {
+        df.setUseDefaultObjectMapper(false);
+        super.setUp();
+    }
+
+    @Test
+    public void testMarshalAndUnmarshalMap() throws Exception {
+        Map<String, Object> in = new HashMap<String, Object>();
+        in.put("name", "Camel");
+
+        MockEndpoint mock = getMockEndpoint("mock:reverse");
+        mock.expectedMessageCount(1);
+        mock.message(0).body().isInstanceOf(Map.class);
+        mock.message(0).body().isEqualTo(in);
+
+        Object marshalled = template.requestBody("direct:in", in);
+        String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled);
+        assertEquals("{\"name\":\"Camel\"}", marshalledAsString);
+
+        template.sendBody("direct:back", marshalled);
+
+        mock.assertIsSatisfied();
+
+        assertNotSame(objectMapper, df.getObjectMapper());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:in").marshal(df);
+                from("direct:back").unmarshal(df).to("mock:reverse");
+            }
+        };
+    }
+
+}
diff --git a/platforms/spring-boot/components-starter/camel-jackson-starter/src/main/java/org/apache/camel/component/jackson/springboot/JacksonDataFormatConfiguration.java b/platforms/spring-boot/components-starter/camel-jackson-starter/src/main/java/org/apache/camel/component/jackson/springboot/JacksonDataFormatConfiguration.java
index 089a57e..fab4911 100644
--- a/platforms/spring-boot/components-starter/camel-jackson-starter/src/main/java/org/apache/camel/component/jackson/springboot/JacksonDataFormatConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-jackson-starter/src/main/java/org/apache/camel/component/jackson/springboot/JacksonDataFormatConfiguration.java
@@ -40,6 +40,10 @@ public class JacksonDataFormatConfiguration
      */
     private String objectMapper;
     /**
+     * Whether to lookup and use default Jackson ObjectMapper from the registry.
+     */
+    private Boolean useDefaultObjectMapper = true;
+    /**
      * To enable pretty printing output nicely formatted. Is by default false.
      */
     private Boolean prettyPrint = false;
@@ -156,6 +160,14 @@ public class JacksonDataFormatConfiguration
         this.objectMapper = objectMapper;
     }
 
+    public Boolean getUseDefaultObjectMapper() {
+        return useDefaultObjectMapper;
+    }
+
+    public void setUseDefaultObjectMapper(Boolean useDefaultObjectMapper) {
+        this.useDefaultObjectMapper = useDefaultObjectMapper;
+    }
+
     public Boolean getPrettyPrint() {
         return prettyPrint;
     }

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.