Ambiguous mapping methods using java Mapstruct

Bruno Miguel :

I'm working with java Mapstruct to mapping Entities to DTOs

I want to use one mapper from other mapper and both implement the same method with the same signature and because of that I'm getting "Ambiguous mapping methods found for mapping property"

I have already tried to implement the shared method on an interface and then extend the interface on both mappers but the problem remains

I'm guessing I will need to use some kind of qualifier. I searched on google and in the official documentation but I can't figure it out how to apply this technic

// CHILD MAPPER ***
@Mapper(componentModel = "spring", uses = { })
public interface CustomerTagApiMapper {

CustomerTagAPI toCustomerTagApi(CustomerTag customerTag);

default OffsetDateTime fromInstant(Instant instant) {
    return instant == null ? null : instant.atOffset(ZoneOffset.UTC);
}
} 

// PARENT MAPPER ***
@Mapper(componentModel = "spring", uses = {  CustomerTagApiMapper.class })
public interface CustomerApiMapper {

CustomerAPI toCustomerApi(Customer customer);

default OffsetDateTime frmInstant(Instant instant) {
    return instant == null ? null : instant.atOffset(ZoneOffset.UTC);
}
}
Filip :

Using a qualifier is one way to solve this. However, in your case the problem is the fromInstant method which is actually an util method.

Why don't you extract that method to some static util class and tell both mapper to use that class as well?

public class MapperUtils {

    public static OffsetDateTime fromInstant(Instant instant) {
        return instant == null ? null : instant.atOffset(ZoneOffset.UTC);
    }
}

Then your mappers can look like:

@Mapper(componentModel = "spring", uses = { MapperUtils.class })
public interface CustomerTagApiMapper {

    CustomerTagAPI toCustomerTagApi(CustomerTag customerTag);

}

@Mapper(componentModel = "spring", uses = {  CustomerTagApiMapper.class, MapperUtils.class })
public interface CustomerApiMapper {

    CustomerAPI toCustomerApi(Customer customer);

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=101339&siteId=1