Skip to content

Ignore mappings by default #1392

Description

@mowcixo

As Augusto Breno described at Google Groups (https://groups.google.com/forum/?fromgroups#!searchin/mapstruct-users/ignore%7Csort:date/mapstruct-users/BDaK45b3rAg/V1amaHESBwAJ) it would be great to have the possibility of ignore mappings by default.

And yes, as @agudian said at the same topic, it can result a little odd based on the main purpose of MapStruct, but it can be very useful in cases like I'll try to describe:

In some cases you have a class that has copied values from another for historical purposes, for example, an invoice and a customer. And at the same time, both classes have some fields that have the same name.

class Invoice {

    private Long id;

    private String code;

    // Customer historical fields

    private String customerNoun;

    private String customerSurname;

    // getters and setters ....

}

class Customer {

   private Long id;

   private String code;

   private String noun;

   private String surname;

    // getters and setters ....

}

Then, you may have following mapping definition to copy values from Customer to Invoice:

// ...
    @Mappings({
            @Mapping(source = "noun", target = "customerNoun"),
            @Mapping(source = "surname", target = "customerSurname"),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "code", ignore = true)
    })
    public abstract void fillInvoice(Customer customer, @MappingTarget Invoice invoice);
// ...

If you have just two properties to ignore, it's not a big deal, but in more complex structs, it can be a large number of properties that match it's name, so it would be great to have something as Augusto described in the @Mapper annotation, or even in the @Mappings annotation such as:

// ...
    @Mapper(ignoreByDefault=true)
// ...

or...

// ...
    @Mappings(ignoreByDefault=true, {
            @Mapping(source = "noun", target = "customerNoun"),
// ...

Regards!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions