This is an automated email from the ASF dual-hosted git repository.
aldettinger pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new e48fcd6 CAMEL-16226: Fixed camel-cbor promoting non cbor mapper as
default
e48fcd6 is described below
commit e48fcd602622bc2dc59ce17faf7900bf3340a1c6
Author: aldettinger <[email protected]>
AuthorDate: Wed Feb 17 11:19:50 2021 +0100
CAMEL-16226: Fixed camel-cbor promoting non cbor mapper as default
---
.../camel/component/cbor/CBORDataFormat.java | 12 +++--
...faultMapperWithNonCBORMapperInRegistryTest.java | 57 ++++++++++++++++++++++
2 files changed, 66 insertions(+), 3 deletions(-)
diff --git
a/components/camel-cbor/src/main/java/org/apache/camel/component/cbor/CBORDataFormat.java
b/components/camel-cbor/src/main/java/org/apache/camel/component/cbor/CBORDataFormat.java
index d6d081c..f400f38 100644
---
a/components/camel-cbor/src/main/java/org/apache/camel/component/cbor/CBORDataFormat.java
+++
b/components/camel-cbor/src/main/java/org/apache/camel/component/cbor/CBORDataFormat.java
@@ -25,6 +25,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
@@ -323,14 +324,19 @@ public class CBORDataFormat extends ServiceSupport
implements DataFormat, DataFo
// lookup if there is a single default mapper we can use
if (useDefaultObjectMapper && camelContext != null) {
Set<ObjectMapper> set =
camelContext.getRegistry().findByType(ObjectMapper.class);
+ set = set.stream().filter(om -> om.getFactory() instanceof
CBORFactory).collect(Collectors.toSet());
if (set.size() == 1) {
objectMapper = set.iterator().next();
- LOG.info("Found single ObjectMapper in Registry to use:
{}", objectMapper);
- } else if (set.size() > 1) {
- LOG.debug("Found {} ObjectMapper in Registry cannot use as
default as there are more than one instance.",
+ LOG.info(
+ "Found a single ObjectMapper with a CBORFactory in
the registry, so promoting it as the default ObjectMapper: {}",
+ objectMapper);
+ } else {
+ LOG.debug(
+ "Found {} ObjectMapper with a CBORFactory in the
registry, so cannot promote any as the default ObjectMapper.",
set.size());
}
}
+ // use a fallback object mapper in last resort
if (objectMapper == null) {
CBORFactory factory = new CBORFactory();
objectMapper = new ObjectMapper(factory);
diff --git
a/components/camel-cbor/src/test/java/org/apache/camel/component/cbor/CBORUseDefaultMapperWithNonCBORMapperInRegistryTest.java
b/components/camel-cbor/src/test/java/org/apache/camel/component/cbor/CBORUseDefaultMapperWithNonCBORMapperInRegistryTest.java
new file mode 100644
index 0000000..8d6a988
--- /dev/null
+++
b/components/camel-cbor/src/test/java/org/apache/camel/component/cbor/CBORUseDefaultMapperWithNonCBORMapperInRegistryTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.cbor;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class CBORUseDefaultMapperWithNonCBORMapperInRegistryTest extends
CamelTestSupport {
+
+ // The bytes obtained when marshaling an Author with the fallback object
mapper created in CBORDataFormat.doInit()
+ private static final byte[] AUTHOR_CBOR_BYTES = new byte[] {
+ -65, 100, 110, 97, 109, 101, 99, 68, 111, 110, 103, 115, 117, 114,
110, 97, 109, 101, 103, 87, 105, 110, 115, 108,
+ 111, 119, -1 };
+
+ @Test
+ void unmarshalShouldIgnoreTheNonCBORMapperFromRegistry() {
+ Author authReturned = template.requestBody("direct:unmarshal-author",
AUTHOR_CBOR_BYTES, Author.class);
+ assertEquals("Don", authReturned.getName());
+ assertEquals("Winslow", authReturned.getSurname());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+
+ @Override
+ public void configure() {
+
+ // Bind a non CBOR mapper in the registry, it should be
ignored as it can't unmarshal a CBOR payload
+ context.getRegistry().bind("non-cbor-object-mapper", new
ObjectMapper());
+
+ CBORDataFormat useDefaultObjectMapperDataFormat = new
CBORDataFormat();
+
useDefaultObjectMapperDataFormat.setUnmarshalType(Author.class);
+
from("direct:unmarshal-author").unmarshal(useDefaultObjectMapperDataFormat);
+ }
+ };
+ }
+
+}