Skip to content

@BeanMapping(ignoreByDefault = true) does not work for constructor properties #3158

@alexlambrides

Description

@alexlambrides

Expected behavior

Utilizing the @BeanMapping(ignoreByDefault = true) on a mapping where the target is a Java 14 Record, all fields should be ignored except the ones that are explicitly defined through @Mapping.

Actual behavior

@BeanMapping(ignoreByDefault = true) does nothing, all fields are still mapped.

Steps to reproduce the problem

Add a very basic setup to mimic the carToCarDto example, making the CarDto a Java 14 record:

public record CarDto(int id, String make) {}
public class Car {
    private int id;
    private String make;
    // getters & setters...
}

And with a mapper like so:

@Mapper
public interface CarMapper {
    @BeanMapping(ignoreByDefault = true)
    @Mapping(source = "id", target = "id")
    CarDto carToCarDto(Car car);
}

The generated mapper does not ignore the make field as seen:

@Override
public CarDto carToCarDto(Car car) {
    if ( car == null ) {
        return null;
    }

    int id = 0;
    String make = null;

    id = car.getId();
    make = car.getMake();

    CarDto carDto = new CarDto( id, make );

    return carDto;
}

A workaround for now is to explicitly ignore all the fields you do not want mapped, which behaves correctly (eg. @Mapping(target = "make", ignore = true). However, with a large amount of fields this becomes very difficult to manage.

MapStruct Version

Mapstruct 1.5.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions