Skip to content

Commit 70301da

Browse files
committed
CAMEL-24620: Add support to enable/disable features from DatatypeFeature enums
1 parent d8b8bfb commit 70301da

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/AbstractJacksonDataFormat.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
import tools.jackson.databind.PropertyNamingStrategies;
5656
import tools.jackson.databind.PropertyNamingStrategy;
5757
import tools.jackson.databind.SerializationFeature;
58+
import tools.jackson.databind.cfg.DatatypeFeature;
59+
import tools.jackson.databind.cfg.DateTimeFeature;
60+
import tools.jackson.databind.cfg.EnumFeature;
61+
import tools.jackson.databind.cfg.JsonNodeFeature;
5862
import tools.jackson.databind.type.CollectionType;
5963

6064
/**
@@ -551,6 +555,14 @@ public void enableFeature(MapperFeature feature) {
551555
}
552556
}
553557

558+
public void enableFeature(Enum<? extends DatatypeFeature> feature) {
559+
if (enableFeatures == null) {
560+
enableFeatures = feature.name();
561+
} else {
562+
enableFeatures += "," + feature.name();
563+
}
564+
}
565+
554566
public void disableFeature(SerializationFeature feature) {
555567
if (disableFeatures == null) {
556568
disableFeatures = feature.name();
@@ -575,6 +587,14 @@ public void disableFeature(MapperFeature feature) {
575587
}
576588
}
577589

590+
public void disableFeature(Enum<? extends DatatypeFeature> feature) {
591+
if (disableFeatures == null) {
592+
disableFeatures = feature.name();
593+
} else {
594+
disableFeatures += "," + feature.name();
595+
}
596+
}
597+
578598
@Override
579599
protected void doInit() throws Exception {
580600
if (unmarshalTypeName != null && (unmarshalType == null || unmarshalType == Object.class)) {
@@ -702,9 +722,27 @@ private void doEnableFeaures() {
702722
setObjectMapper(om);
703723
continue;
704724
}
725+
DateTimeFeature dtf = getCamelContext().getTypeConverter().tryConvertTo(DateTimeFeature.class, enable);
726+
if (dtf != null) {
727+
ObjectMapper om = objectMapper.rebuild().enable(dtf).build();
728+
setObjectMapper(om);
729+
continue;
730+
}
731+
EnumFeature ef = getCamelContext().getTypeConverter().tryConvertTo(EnumFeature.class, enable);
732+
if (ef != null) {
733+
ObjectMapper om = objectMapper.rebuild().enable(ef).build();
734+
setObjectMapper(om);
735+
continue;
736+
}
737+
JsonNodeFeature jnf = getCamelContext().getTypeConverter().tryConvertTo(JsonNodeFeature.class, enable);
738+
if (jnf != null) {
739+
ObjectMapper om = objectMapper.rebuild().enable(jnf).build();
740+
setObjectMapper(om);
741+
continue;
742+
}
705743
throw new IllegalArgumentException(
706744
"Enable feature: " + enable
707-
+ " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature]");
745+
+ " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature,DateTimeFeature,EnumFeature,JsonNodeFeature]");
708746
}
709747
}
710748

@@ -803,9 +841,27 @@ private void doDisableFeatures() {
803841
setObjectMapper(om);
804842
continue;
805843
}
844+
DateTimeFeature dtf = getCamelContext().getTypeConverter().tryConvertTo(DateTimeFeature.class, disable);
845+
if (dtf != null) {
846+
ObjectMapper om = objectMapper.rebuild().disable(dtf).build();
847+
setObjectMapper(om);
848+
continue;
849+
}
850+
EnumFeature ef = getCamelContext().getTypeConverter().tryConvertTo(EnumFeature.class, disable);
851+
if (ef != null) {
852+
ObjectMapper om = objectMapper.rebuild().disable(ef).build();
853+
setObjectMapper(om);
854+
continue;
855+
}
856+
JsonNodeFeature jnf = getCamelContext().getTypeConverter().tryConvertTo(JsonNodeFeature.class, disable);
857+
if (jnf != null) {
858+
ObjectMapper om = objectMapper.rebuild().disable(jnf).build();
859+
setObjectMapper(om);
860+
continue;
861+
}
806862
throw new IllegalArgumentException(
807863
"Disable feature: " + disable
808-
+ " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature]");
864+
+ " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature,DateTimeFeature,EnumFeature,JsonNodeFeature]");
809865
}
810866
}
811867

components/camel-jackson3/src/test/java/org/apache/camel/component/jackson3/JacksonFeaturesTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
*/
1717
package org.apache.camel.component.jackson3;
1818

19+
import java.util.Date;
20+
1921
import org.apache.camel.builder.RouteBuilder;
2022
import org.apache.camel.component.mock.MockEndpoint;
2123
import org.apache.camel.test.junit6.CamelTestSupport;
2224
import org.junit.jupiter.api.Test;
2325
import tools.jackson.databind.DeserializationFeature;
2426
import tools.jackson.databind.MapperFeature;
2527
import tools.jackson.databind.SerializationFeature;
28+
import tools.jackson.databind.cfg.DateTimeFeature;
2629

2730
public class JacksonFeaturesTest extends CamelTestSupport {
2831

@@ -49,6 +52,17 @@ public void testEnableMapperFeature() throws Exception {
4952
mock.assertIsSatisfied();
5053
}
5154

55+
@Test
56+
public void testEnableDatatypeMapperFeature() throws Exception {
57+
MockEndpoint mock = getMockEndpoint("mock:result");
58+
mock.message(0).body().isEqualTo("123");
59+
60+
template.send("direct:unformat", exchange -> exchange.getIn().setBody(new Date(123)));
61+
62+
mock.expectedMessageCount(1);
63+
mock.assertIsSatisfied();
64+
}
65+
5266
@Override
5367
protected RouteBuilder createRouteBuilder() {
5468
return new RouteBuilder() {
@@ -61,8 +75,10 @@ public void configure() {
6175
format.disableFeature(SerializationFeature.INDENT_OUTPUT);
6276
format.disableFeature(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
6377
format.disableFeature(MapperFeature.APPLY_DEFAULT_VALUES);
78+
format.enableFeature(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS);
6479

6580
from("direct:format").unmarshal(format).to("mock:result");
81+
from("direct:unformat").marshal(format).to("mock:result");
6682
}
6783
};
6884
}

0 commit comments

Comments
 (0)