You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2023/07/26 14:40:39 UTC

[camel] branch main updated: CAMEL-19660: Improve support for MapStruct mappers defined as abstract classes

This is an automated email from the ASF dual-hosted git repository.

jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new ee61363049b CAMEL-19660: Improve support for MapStruct mappers defined as abstract classes
ee61363049b is described below

commit ee61363049b6b208bfdac6c192405f41ae88d213
Author: James Netherton <ja...@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 a289195d16a..f670b64b132 100644
--- a/components/camel-mapstruct/src/main/docs/mapstruct-component.adoc
+++ b/components/camel-mapstruct/src/main/docs/mapstruct-component.adoc
@@ -73,5 +73,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 713b65216e3..d84a0707486 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
@@ -58,7 +58,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;
@@ -69,9 +69,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);
+}