Mapstruct case insensitive mapping

Viewed 4574

is there a way in mapstruct to ignore the case of the fields when mapping. let say i want to map following two classes

public class Customer {

    private String ID;

    public String getID() {
        return ID;
    }

    public void setID(String iD) {
        this.ID = iD;
    }
}


public class CustomerDetails {

    private String id;

    public String getId() {
        return ID;
    }

    public void setId(String id) {
        this.id = id;
    }

}

MapStruct is not automatically mapping the fields because getter methods names that doesn't match. Is there a way to configure MapStruct to ignore the case of the fields and map them automatically

2 Answers

A custom AccessorNamingStrategy can be implemented that would lowercase the element name and thus making it case insensitive.

e.g.

public class CaseInsensitiveAccessorNamingStrategy extends DefaultAccessorNamingStrategy {

    @Override
    public String getPropertyName(ExecutableElement getterOrSetterMethod) {
        return super.getPropertyName( getterOrSetterMethod ).toLowerCase( Locale.ROOT );
    }

    @Override
    public String getElementName(ExecutableElement adderMethod) {
        return super.getElementName( adderMethod ).toLowerCase( Locale.ROOT );
    }
}

Not sure if you can configure mapstruct to map case insensitive but you always can define what should be mapped like this:

@Mapping(source = "ID", target = "id")
CustomerDetails toCustomerDetails(Customer customer);
Related