Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,19 @@ private ObjectMapper resolveObjectMapper(CamelContext camelContext) throws Excep
lock.lock();
try {
if (defaultMapper == null) {
ObjectMapper mapper = new ObjectMapper();
JsonMapper.Builder builder = JsonMapper.builder();
if (moduleClassNames != null) {
for (Object o : ObjectHelper.createIterable(moduleClassNames)) {
Class<JacksonModule> type
= camelContext.getClassResolver().resolveMandatoryClass(o.toString(), JacksonModule.class);
JacksonModule module = camelContext.getInjector().newInstance(type);

LOG.debug("Registering module: {} -> {}", o, module);
mapper = JsonMapper.builder().addModule(module).build();
builder.addModule(module);
}
}

defaultMapper = mapper;
defaultMapper = builder.build();
}
} finally {
lock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.camel.spi.Transformer;
import org.apache.camel.util.ObjectHelper;
import tools.jackson.core.FormatSchema;
import tools.jackson.core.JacksonException;

/**
* Data type able to unmarshal Exchange body to Java object. Supports both Json schema types and uses Jackson object
Expand Down Expand Up @@ -71,7 +72,7 @@ public void transform(Message message, DataType fromType, DataType toType) {
}

message.setBody(getJavaObject(message, schemaType, schema, contentType));
} catch (InvalidPayloadException | IOException | ClassNotFoundException e) {
} catch (InvalidPayloadException | IOException | JacksonException | ClassNotFoundException e) {
throw new CamelExecutionException("Failed to apply Java object data type on exchange", message.getExchange(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.camel.spi.DataTypeTransformer;
import org.apache.camel.spi.MimeType;
import org.apache.camel.spi.Transformer;
import tools.jackson.core.JacksonException;
import tools.jackson.databind.JsonNode;

/**
Expand Down Expand Up @@ -61,7 +62,7 @@ public void transform(Message message, DataType fromType, DataType toType) {
message.setBody(unmarshalled);

message.setHeader(Exchange.CONTENT_TYPE, MimeType.STRUCT.type());
} catch (InvalidPayloadException | ClassNotFoundException e) {
} catch (InvalidPayloadException | JacksonException | ClassNotFoundException e) {
throw new CamelExecutionException("Failed to apply Json input data type on exchange", message.getExchange(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.jackson3.converter;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson3.JacksonConstants;
import org.apache.camel.test.junit6.CamelTestSupport;
import org.junit.jupiter.api.Test;
import tools.jackson.core.JsonGenerator;
import tools.jackson.databind.SerializationContext;
import tools.jackson.databind.ValueSerializer;
import tools.jackson.databind.module.SimpleModule;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JacksonConversionsMultipleModulesTest extends CamelTestSupport {

@Test
public void shouldRegisterAllConfiguredModules() {
context.getGlobalOptions().put(JacksonConstants.ENABLE_TYPE_CONVERTER, "true");
context.getGlobalOptions().put(JacksonConstants.TYPE_CONVERTER_TO_POJO, "true");
context.getGlobalOptions().put(JacksonConstants.TYPE_CONVERTER_MODULE_CLASS_NAMES,
FooModule.class.getName() + "," + BarModule.class.getName());

String foo = (String) template.requestBody("direct:test", new Foo());
String bar = (String) template.requestBody("direct:test", new Bar());

assertEquals("\"foo\"", foo);
assertEquals("\"bar\"", bar);
}

@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
from("direct:test").convertBodyTo(String.class);
}
};
}

public static class Foo {
}

public static class Bar {
}

public static class FooModule extends SimpleModule {
public FooModule() {
super("foo-module");
addSerializer(Foo.class, new ValueSerializer<Foo>() {
@Override
public void serialize(Foo value, JsonGenerator gen, SerializationContext ctxt) {
gen.writeString("foo");
}
});
}
}

public static class BarModule extends SimpleModule {
public BarModule() {
super("bar-module");
addSerializer(Bar.class, new ValueSerializer<Bar>() {
@Override
public void serialize(Bar value, JsonGenerator gen, SerializationContext ctxt) {
gen.writeString("bar");
}
});
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.camel.component.jackson3.transform;

import org.apache.camel.CamelExecutionException;
import org.apache.camel.Exchange;
import org.apache.camel.component.jackson3.SchemaHelper;
import org.apache.camel.impl.DefaultCamelContext;
Expand Down Expand Up @@ -112,6 +113,17 @@ void shouldHandleExplicitContentClass() throws Exception {
Assertions.assertEquals(19, exchange.getMessage().getBody(Person.class).age());
}

@Test
void shouldWrapMalformedJsonInCamelExecutionException() {
Exchange exchange = new DefaultExchange(camelContext);

exchange.setProperty(SchemaHelper.CONTENT_CLASS, Person.class.getName());
exchange.getMessage().setBody("{ this is no json }");

Assertions.assertThrows(CamelExecutionException.class,
() -> transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY));
}

@Test
public void shouldLookupDataTypeTransformer() throws Exception {
Transformer transformer = camelContext.getTransformerRegistry()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.camel.component.jackson3.transform;

import org.apache.camel.CamelExecutionException;
import org.apache.camel.Exchange;
import org.apache.camel.component.jackson3.SchemaHelper;
import org.apache.camel.impl.DefaultCamelContext;
Expand Down Expand Up @@ -93,6 +94,16 @@ void shouldHandleExplicitContentClass() throws Exception {
""", exchange.getMessage().getBody(String.class), true);
}

@Test
void shouldWrapMalformedJsonInCamelExecutionException() {
Exchange exchange = new DefaultExchange(camelContext);

exchange.getMessage().setBody("{ this is no json }");

Assertions.assertThrows(CamelExecutionException.class,
() -> transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY));
}

@Test
public void shouldLookupDataTypeTransformer() throws Exception {
Transformer transformer = camelContext.getTransformerRegistry()
Expand Down