Skip to content

Commit 235036d

Browse files
oscerddkulp
authored andcommitted
CAMEL-10567: Camel-Jackson: Add an option to allow the UnmarshallType header use
1 parent 2f204b7 commit 235036d

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

Lines changed: 18 additions & 1 deletion
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}
@@ -159,7 +160,10 @@ public Object unmarshal(Exchange exchange, InputStream stream) throws Exception
159160

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

327344
public String getEnableFeatures() {
328345
return enableFeatures;
Lines changed: 54 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)