Skip to content

Commit 10f5526

Browse files
committed
CAMEL-10567: Camel-Jackson: Add an option to allow the UnmarshallType header use
1 parent a7aee7c commit 10f5526

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class JacksonDataFormat extends ServiceSupport implements DataFormat, Dat
6868
private String enableFeatures;
6969
private String disableFeatures;
7070
private boolean enableJacksonTypeConverter;
71+
private boolean allowJacksonUnmarshallType;
7172

7273
/**
7374
* Use the default Jackson {@link ObjectMapper} and {@link Map}
@@ -158,7 +159,10 @@ public Object unmarshal(Exchange exchange, InputStream stream) throws Exception
158159

159160
// is there a header with the unmarshal type?
160161
Class<?> clazz = unmarshalType;
161-
String type = exchange.getIn().getHeader(JacksonConstants.UNMARSHAL_TYPE, String.class);
162+
String type = null;
163+
if (allowJacksonUnmarshallType) {
164+
type = exchange.getIn().getHeader(JacksonConstants.UNMARSHAL_TYPE, String.class);
165+
}
162166
if (type == null && isAllowJmsType()) {
163167
type = exchange.getIn().getHeader("JMSType", String.class);
164168
}
@@ -326,6 +330,19 @@ public boolean isEnableJacksonTypeConverter() {
326330
public void setEnableJacksonTypeConverter(boolean enableJacksonTypeConverter) {
327331
this.enableJacksonTypeConverter = enableJacksonTypeConverter;
328332
}
333+
334+
public boolean isAllowJacksonUnmarshallType() {
335+
return allowJacksonUnmarshallType;
336+
}
337+
338+
/**
339+
* If enabled then Jackson is allowed to attempt to use the CamelJacksonUnmarshalType header during the unmarshalling.
340+
* <p/>
341+
* This should only be enabled when desired to be used.
342+
*/
343+
public void setAllowJacksonUnmarshallType(boolean allowJacksonUnmarshallType) {
344+
this.allowJacksonUnmarshallType = allowJacksonUnmarshallType;
345+
}
329346

330347
public String getEnableFeatures() {
331348
return enableFeatures;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.component.jackson;
18+
19+
import java.util.LinkedHashMap;
20+
21+
import org.apache.camel.builder.RouteBuilder;
22+
import org.apache.camel.component.mock.MockEndpoint;
23+
import org.apache.camel.test.junit4.CamelTestSupport;
24+
import org.junit.Test;
25+
26+
public class JacksonMarshalUnmarshalTypeHeaderNotAllowedTest extends CamelTestSupport {
27+
28+
@Test
29+
public void testUnmarshalPojo() throws Exception {
30+
MockEndpoint mock = getMockEndpoint("mock:reversePojo");
31+
mock.expectedMessageCount(1);
32+
mock.message(0).body().isInstanceOf(LinkedHashMap.class);
33+
34+
String json = "{\"name\":\"Camel\"}";
35+
template.sendBodyAndHeader("direct:backPojo", json, JacksonConstants.UNMARSHAL_TYPE, TestPojo.class.getName());
36+
37+
assertMockEndpointsSatisfied();
38+
}
39+
40+
@Override
41+
protected RouteBuilder createRouteBuilder() throws Exception {
42+
return new RouteBuilder() {
43+
44+
@Override
45+
public void configure() throws Exception {
46+
JacksonDataFormat format = new JacksonDataFormat();
47+
48+
from("direct:backPojo").unmarshal(format).to("mock:reversePojo");
49+
50+
}
51+
};
52+
}
53+
54+
}

components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonMarshalUnmarshalTypeHeaderTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ protected RouteBuilder createRouteBuilder() throws Exception {
4646
@Override
4747
public void configure() throws Exception {
4848
JacksonDataFormat format = new JacksonDataFormat();
49+
format.setAllowJacksonUnmarshallType(true);
4950

5051
from("direct:backPojo").unmarshal(format).to("mock:reversePojo");
5152

0 commit comments

Comments
 (0)