This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch camel-3.21.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.21.x by this push: new 9b1ffc6d667 CAMEL-19660: Improve support for MapStruct mappers defined as abstract classes 9b1ffc6d667 is described below commit 9b1ffc6d667290717d8fd68a6d1fc885bdb66e8b Author: James Netherton <jamesnether...@gmail.com> AuthorDate: Wed Jul 26 14:52:36 2023 +0100 CAMEL-19660: Improve support for MapStruct mappers defined as abstract classes --- .../src/main/docs/mapstruct-component.adoc | 4 ++ .../mapstruct/DefaultMapStructFinder.java | 6 +- .../component/mapstruct/CarToVehicleTest.java | 81 ++++++++++++++++++++++ .../component/mapstruct/mapper/VehicleMapper.java | 31 +++++++++ 4 files changed, 119 insertions(+), 3 deletions(-) diff --git a/components/camel-mapstruct/src/main/docs/mapstruct-component.adoc b/components/camel-mapstruct/src/main/docs/mapstruct-component.adoc index 69d2972ec3f..aac240c4db9 100644 --- a/components/camel-mapstruct/src/main/docs/mapstruct-component.adoc +++ b/components/camel-mapstruct/src/main/docs/mapstruct-component.adoc @@ -72,5 +72,9 @@ from("direct:foo") Where `MyFooDto` is a POJO that MapStruct is able to convert to/from. +NOTE: Camel does not support mapper methods defined with a `void` return type such as those used with `@MappingTarget`. + +WARNING: If you define multiple mapping methods for the same from / to types, then the implementation chosen by +Camel to do its type conversion is potentially non-deterministic. include::spring-boot:partial$starter.adoc[] diff --git a/components/camel-mapstruct/src/main/java/org/apache/camel/component/mapstruct/DefaultMapStructFinder.java b/components/camel-mapstruct/src/main/java/org/apache/camel/component/mapstruct/DefaultMapStructFinder.java index 7e1f93bcde6..f68993ed877 100644 --- a/components/camel-mapstruct/src/main/java/org/apache/camel/component/mapstruct/DefaultMapStructFinder.java +++ b/components/camel-mapstruct/src/main/java/org/apache/camel/component/mapstruct/DefaultMapStructFinder.java @@ -57,7 +57,7 @@ public class DefaultMapStructFinder extends ServiceSupport implements MapStructM // is there a generated mapper final Object mapper = Mappers.getMapper(clazz); if (mapper != null) { - ReflectionHelper.doWithMethods(clazz, mc -> { + ReflectionHelper.doWithMethods(mapper.getClass(), mc -> { // must not be a default method if (mc.isDefault()) { return; @@ -68,9 +68,9 @@ public class DefaultMapStructFinder extends ServiceSupport implements MapStructM return; } Class<?> from = mc.getParameterTypes()[0]; - // must return a value + // must return a non-primitive value Class<?> to = mc.getReturnType(); - if (to.equals(Void.class)) { + if (to.isPrimitive()) { return; } // okay register this method as a Camel type converter diff --git a/components/camel-mapstruct/src/test/java/org/apache/camel/component/mapstruct/CarToVehicleTest.java b/components/camel-mapstruct/src/test/java/org/apache/camel/component/mapstruct/CarToVehicleTest.java new file mode 100644 index 00000000000..f059bbd5e1a --- /dev/null +++ b/components/camel-mapstruct/src/test/java/org/apache/camel/component/mapstruct/CarToVehicleTest.java @@ -0,0 +1,81 @@ +/* + * 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.mapstruct; + +import org.apache.camel.CamelContext; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mapstruct.dto.CarDto; +import org.apache.camel.component.mapstruct.dto.VehicleDto; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class CarToVehicleTest extends CamelTestSupport { + + @ParameterizedTest() + @ValueSource(strings = { "component", "converter" }) + void testCarToVehicleMapping(String endpoint) throws Exception { + getMockEndpoint("mock:result-" + endpoint).expectedMessageCount(1); + + CarDto c = new CarDto(); + c.setBrand("Volvo"); + c.setModel("XC40"); + c.setYear(2021); + c.setElectric(true); + + VehicleDto vehicle = template.requestBody("direct:" + endpoint, c, VehicleDto.class); + assertNotNull(vehicle); + + assertEquals("Volvo", vehicle.getCompany()); + assertEquals("XC40", vehicle.getName()); + assertEquals(2021, vehicle.getYear()); + assertEquals("true", vehicle.getPower()); + + MockEndpoint.assertIsSatisfied(context); + } + + @Override + protected CamelContext createCamelContext() throws Exception { + MapStructMapperFinder converter = new DefaultMapStructFinder(); + converter.setMapperPackageName("org.apache.camel.component.mapstruct.mapper"); + + CamelContext context = super.createCamelContext(); + context.addService(converter); + return context; + } + + @Override + protected RoutesBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:component") + .toF("mapstruct:%s", VehicleDto.class.getName()) + .to("mock:result-component"); + + from("direct:converter") + .convertBodyTo(VehicleDto.class) + .to("mock:result-converter"); + } + }; + } +} diff --git a/components/camel-mapstruct/src/test/java/org/apache/camel/component/mapstruct/mapper/VehicleMapper.java b/components/camel-mapstruct/src/test/java/org/apache/camel/component/mapstruct/mapper/VehicleMapper.java new file mode 100644 index 00000000000..33101df0783 --- /dev/null +++ b/components/camel-mapstruct/src/test/java/org/apache/camel/component/mapstruct/mapper/VehicleMapper.java @@ -0,0 +1,31 @@ +/* + * 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.mapstruct.mapper; + +import org.apache.camel.component.mapstruct.dto.CarDto; +import org.apache.camel.component.mapstruct.dto.VehicleDto; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +@Mapper +public abstract class VehicleMapper { + + @Mapping(source = "brand", target = "company") + @Mapping(source = "model", target = "name") + @Mapping(source = "electric", target = "power") + public abstract VehicleDto toVehicle(CarDto car); +}