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

[camel] 01/10: CAMEL-16912 - camel-jackson - Make it easy to configure property naming strategy

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

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

commit d64c90615ffec0bddd1c002e3325825537df4eb0
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Thu Nov 18 11:51:30 2021 +0100

    CAMEL-16912 - camel-jackson - Make it easy to configure property naming strategy
---
 .../jackson/AbstractJacksonDataFormat.java         | 18 +++++--
 .../jackson/JacksonMarshalNamingStrategyTest.java  | 60 ++++++++++++++++++++++
 .../component/jackson/PojoNamingStrategy.java      | 24 +++++++++
 3 files changed, 98 insertions(+), 4 deletions(-)

diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/AbstractJacksonDataFormat.java b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/AbstractJacksonDataFormat.java
index d5c62ce..7b83b0d 100644
--- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/AbstractJacksonDataFormat.java
+++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/AbstractJacksonDataFormat.java
@@ -29,11 +29,8 @@ import java.util.TimeZone;
 
 import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.core.FormatSchema;
-import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.MapperFeature;
+import com.fasterxml.jackson.databind.*;
 import com.fasterxml.jackson.databind.Module;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.SerializationFeature;
 import com.fasterxml.jackson.databind.type.CollectionType;
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
@@ -80,6 +77,7 @@ public abstract class AbstractJacksonDataFormat extends ServiceSupport
     private boolean autoDiscoverObjectMapper;
     private SchemaResolver schemaResolver;
     private boolean autoDiscoverSchemaResolver = true;
+    private PropertyNamingStrategy namingStrategy;
 
     /**
      * Use the default Jackson {@link ObjectMapper} and {@link Object}
@@ -285,6 +283,14 @@ public abstract class AbstractJacksonDataFormat extends ServiceSupport
         return modules;
     }
 
+    public PropertyNamingStrategy getNamingStrategy() {
+        return namingStrategy;
+    }
+
+    public void setNamingStrategy(PropertyNamingStrategy namingStrategy) {
+        this.namingStrategy = namingStrategy;
+    }
+
     /**
      * To use custom Jackson {@link Module}s
      */
@@ -647,6 +653,10 @@ public abstract class AbstractJacksonDataFormat extends ServiceSupport
                 LOG.debug("Setting timezone to Object Mapper: {}", timezone);
                 objectMapper.setTimeZone(timezone);
             }
+
+            if (org.apache.camel.util.ObjectHelper.isNotEmpty(namingStrategy)) {
+                objectMapper.setPropertyNamingStrategy(namingStrategy);
+            }
         } else {
             LOG.debug("The objectMapper was already found in the registry, no customizations will be applied");
         }
diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonMarshalNamingStrategyTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonMarshalNamingStrategyTest.java
new file mode 100644
index 0000000..c6de91e
--- /dev/null
+++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonMarshalNamingStrategyTest.java
@@ -0,0 +1,60 @@
+/*
+ * 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 com.fasterxml.jackson.databind.PropertyNamingStrategies;
+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 java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class JacksonMarshalNamingStrategyTest extends CamelTestSupport {
+
+    @Test
+    public void testMarshalAndUnmarshalMap() throws Exception {
+        PojoNamingStrategy pojoNamingStrategy = new PojoNamingStrategy();
+        pojoNamingStrategy.setFieldOne("test");
+        pojoNamingStrategy.setFieldTwo("supertest");
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        Object marshalled = template.requestBody("direct:in", pojoNamingStrategy);
+        String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled);
+        assertEquals("{\"field.one\":\"test\",\"field.two\":\"supertest\"}", marshalledAsString);
+
+        mock.assertIsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+
+            @Override
+            public void configure() throws Exception {
+                JacksonDataFormat format = new JacksonDataFormat();
+                format.setNamingStrategy(PropertyNamingStrategies.LOWER_DOT_CASE);
+                from("direct:in").marshal(format).to("mock:result");
+            }
+        };
+    }
+
+}
diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/PojoNamingStrategy.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/PojoNamingStrategy.java
new file mode 100644
index 0000000..b7bd28f
--- /dev/null
+++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/PojoNamingStrategy.java
@@ -0,0 +1,24 @@
+package org.apache.camel.component.jackson;
+
+public class PojoNamingStrategy {
+
+        private String fieldOne;
+
+        private String fieldTwo;
+
+        public String getFieldOne() {
+            return fieldOne;
+        }
+
+        public void setFieldOne(String fieldOne) {
+            this.fieldOne = fieldOne;
+        }
+
+        public String getFieldTwo() {
+            return fieldTwo;
+        }
+
+        public void setFieldTwo(String kFieldTwo) {
+            this.fieldTwo = kFieldTwo;
+        }
+}