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-kamelets.git
commit 00ccfe7e4fb16dfe8aa692d9579bbec2e72ca310 Author: Christoph Deppisch <cdeppi...@redhat.com> AuthorDate: Wed Nov 30 15:56:53 2022 +0100 Simplify Json model data type - Remove JacksonDataFormat in favor of using simple ObjectMapper instance - Reuse ObjectMapper instance for all exchanges processed by the data type --- .../converter/standard/JsonModelDataType.java | 22 +++++++++++++++++----- .../converter/standard/JsonModelDataTypeTest.java | 14 +++++++++++++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/format/converter/standard/JsonModelDataType.java b/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/format/converter/standard/JsonModelDataType.java index 0a80ee32..183f1112 100644 --- a/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/format/converter/standard/JsonModelDataType.java +++ b/library/camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/format/converter/standard/JsonModelDataType.java @@ -26,7 +26,6 @@ import org.apache.camel.CamelContextAware; import org.apache.camel.CamelExecutionException; import org.apache.camel.Exchange; import org.apache.camel.InvalidPayloadException; -import org.apache.camel.component.jackson.JacksonDataFormat; import org.apache.camel.kamelets.utils.format.spi.DataTypeConverter; import org.apache.camel.kamelets.utils.format.spi.annotations.DataType; import org.apache.camel.util.ObjectHelper; @@ -41,21 +40,30 @@ public class JsonModelDataType implements DataTypeConverter, CamelContextAware { public static final String DATA_TYPE_MODEL_PROPERTY = "CamelDataTypeModel"; + private String model; + private CamelContext camelContext; private static final ObjectMapper mapper = new ObjectMapper(); @Override public void convert(Exchange exchange) { - if (!exchange.hasProperties() || !exchange.getProperties().containsKey(DATA_TYPE_MODEL_PROPERTY)) { + String type; + if (exchange.hasProperties() && exchange.getProperties().containsKey(DATA_TYPE_MODEL_PROPERTY)) { + type = exchange.getProperty(DATA_TYPE_MODEL_PROPERTY, String.class); + } else { + type = model; + } + + if (type == null) { + // neither model property nor exchange property defines proper type - do nothing return; } ObjectHelper.notNull(camelContext, "camelContext"); - String type = exchange.getProperty(DATA_TYPE_MODEL_PROPERTY, String.class); - try (JacksonDataFormat dataFormat = new JacksonDataFormat(mapper, camelContext.getClassResolver().resolveMandatoryClass(type))) { - Object unmarshalled = dataFormat.unmarshal(exchange, getBodyAsStream(exchange)); + try { + Object unmarshalled = mapper.reader().forType(camelContext.getClassResolver().resolveMandatoryClass(type)).readValue(getBodyAsStream(exchange)); exchange.getMessage().setBody(unmarshalled); } catch (Exception e) { throw new CamelExecutionException( @@ -78,6 +86,10 @@ public class JsonModelDataType implements DataTypeConverter, CamelContextAware { return camelContext; } + public void setModel(String model) { + this.model = model; + } + @Override public void setCamelContext(CamelContext camelContext) { this.camelContext = camelContext; diff --git a/library/camel-kamelets-utils/src/test/java/org/apache/camel/kamelets/utils/format/converter/standard/JsonModelDataTypeTest.java b/library/camel-kamelets-utils/src/test/java/org/apache/camel/kamelets/utils/format/converter/standard/JsonModelDataTypeTest.java index cb253a16..7785017c 100644 --- a/library/camel-kamelets-utils/src/test/java/org/apache/camel/kamelets/utils/format/converter/standard/JsonModelDataTypeTest.java +++ b/library/camel-kamelets-utils/src/test/java/org/apache/camel/kamelets/utils/format/converter/standard/JsonModelDataTypeTest.java @@ -44,7 +44,19 @@ public class JsonModelDataTypeTest { } @Test - void shouldMapFromStringToJsonModel() throws Exception { + void shouldMapStringToJsonModelWithModelProperty() throws Exception { + Exchange exchange = new DefaultExchange(camelContext); + + exchange.getMessage().setBody("{ \"name\": \"Rajesh\", \"age\": 28}"); + dataType.setModel(Person.class.getName()); + dataType.convert(exchange); + + assertEquals(Person.class, exchange.getMessage().getBody().getClass()); + assertEquals("Rajesh", exchange.getMessage().getBody(Person.class).getName()); + } + + @Test + void shouldMapStringToJsonModelWithExchangeProperty() throws Exception { Exchange exchange = new DefaultExchange(camelContext); exchange.setProperty(JsonModelDataType.DATA_TYPE_MODEL_PROPERTY, Person.class.getName());