From d9883aa99a8aa70a8e4919ba2903bcacc2a5a6d2 Mon Sep 17 00:00:00 2001 From: Nikolay Nikolov Date: Thu, 26 Jun 2025 09:53:45 +0300 Subject: [PATCH] #3858 Compilation error if no mapper configuration was found to inherit from --- .../chapter-11-reusing-mapping-configurations.asciidoc | 2 +- .../ap/internal/processor/MapperCreationProcessor.java | 7 +++++++ .../main/java/org/mapstruct/ap/internal/util/Message.java | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/documentation/src/main/asciidoc/chapter-11-reusing-mapping-configurations.asciidoc b/documentation/src/main/asciidoc/chapter-11-reusing-mapping-configurations.asciidoc index efacaedbe5..9e2b4ec5f6 100644 --- a/documentation/src/main/asciidoc/chapter-11-reusing-mapping-configurations.asciidoc +++ b/documentation/src/main/asciidoc/chapter-11-reusing-mapping-configurations.asciidoc @@ -26,7 +26,7 @@ public interface CarMapper { The example above declares a mapping method `carDtoToCar()` with a configuration to define how the property `numberOfSeats` in the type `Car` shall be mapped. The update method that performs the mapping on an existing instance of `Car` needs the same configuration to successfully map all properties. Declaring `@InheritConfiguration` on the method lets MapStruct search for inheritance candidates to apply the annotations of the method that is inherited from. -One method *A* can inherit the configuration from another method *B* if all types of *A* (source types and result type) are assignable to the corresponding types of *B*. +One method *A* is considered *similar* and can inherit the configuration from another method *B* if all types of *A* (source types and result type) are assignable to the corresponding types of *B*. If methods are not resolved as *similar* a compilation error will be raised. Methods that are considered for inheritance need to be defined in the current mapper, a super class/interface, or in the shared configuration interface (as described in <>). diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java index 3b38a05b59..fae7a66e41 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java @@ -718,6 +718,13 @@ else if ( nameFilteredCandidates.size() > 1 ) { else { reportErrorWhenAmbiguousMapping( candidates, method, inheritConfiguration ); } + } else { + messager.printMessage( + method.getExecutable(), + Message.INHERITCONFIGURATION_PARAMETER_MISMATCH, + method.getName(), + resultMethod.getName() + ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/util/Message.java b/processor/src/main/java/org/mapstruct/ap/internal/util/Message.java index 883b3e3792..cc1a67384b 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/util/Message.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/util/Message.java @@ -199,6 +199,7 @@ public enum Message { INHERITCONFIGURATION_DUPLICATE_MATCHES( "Given name \"%s\" matches several candidate methods: %s." ), INHERITCONFIGURATION_NO_NAME_MATCH( "Given name \"%s\" does not match the only candidate. Did you mean: \"%s\"." ), INHERITCONFIGURATION_MULTIPLE_PROTOTYPE_METHODS_MATCH( "More than one configuration prototype method is applicable. Use @InheritConfiguration to select one of them explicitly: %s." ), + INHERITCONFIGURATION_PARAMETER_MISMATCH("Method \"%s\" cannot inherit configuration from \"%s\" because their parameters do not match."), INHERITINVERSECONFIGURATION_MULTIPLE_PROTOTYPE_METHODS_MATCH( "More than one configuration prototype method is applicable. Use @InheritInverseConfiguration to select one of them explicitly: %s." ), INHERITCONFIGURATION_CYCLE( "Cycle detected while evaluating inherited configurations. Inheritance path: %s" ),