Skip to content

False Positive Warnings for withers on records #3955

@RaniAgus

Description

@RaniAgus

Expected behavior

When mapping to Java records that have wither methods (either generated by Lombok's @With annotation or manually implemented), MapStruct incorrectly flags these methods as unmapped target properties.

MapStruct should compile successfully without warnings, as those methods are not properties and cannot be used as mapping targets. They should be ignored by the property detection mechanism.

Actual behavior

MapStruct emits the following warning:

[WARNING] Unmapped target properties: "withId, withName, withEmail".

This is a false positive because with*() methods are not JavaBean properties - they are utility methods for creating immutable copies.

Steps to reproduce the problem

Case 1: With Lombok @with

  1. Create a record with @With:
@With
public record User(
        String id,
        String name,
        String email
) {}

Case 2: Manual Implementation (No Lombok)

Records can also manually implement with*() methods:

public record UserDTO(
        String id,
        String name,
        String email
) {
    public UserDTO withId(String id) {
        return new UserDTO(id, this.name, this.email);
    }
    public UserDTO withName(String name) {
        return new UserDTO(this.id, name, this.email);
    }
    public UserDTO withEmail(String email) {
        return new UserDTO(this.id, this.name, email);
    }
}

In both cases, create a MapStruct mapper:

@Mapper(unmappedTargetPolicy = ReportingPolicy.WARN)
public interface UserMapper {
    UserDTO toDTO(User user);
}

Then compile the project with Maven:

mvn clean compile

Example pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>mapstruct-lombok-test</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mapstruct.version>1.5.5.Final</mapstruct.version>
        <lombok.version>1.18.30</lombok.version>
    </properties>

    <dependencies>
        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- MapStruct -->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.14.1</version>
                <configuration>
                    <source>21</source>
                    <target>21</target>
                    <annotationProcessorPaths>
                        <!-- Lombok processor -->
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <!-- MapStruct processor -->
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

MapStruct Version

MapStruct 1.5.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions