user3817206
user3817206

Reputation: 344

Mapstruct case insensitive mapping

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

Upvotes: 5

Views: 10325

Answers (2)

Filip
Filip

Reputation: 21403

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 );
    }
}

Upvotes: 6

name not found
name not found

Reputation: 622

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);

Upvotes: 1

Related Questions