Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link

Description

@alexlambrides
Issue body actions

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

Reactions are currently unavailable

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

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.