Mapstruct - Ambiguous mapping methods found for mapping property

ljs picture ljs · Feb 11, 2019 · Viewed 13.1k times · Source

I am using mapstruct to map from one DTO to another. I have multiple default methods , but 2 of them with a return value of String and that uses the same class as the input parameter gives me "Ambiguous mapping methods using java Mapstruct" error. I am adding the relevant parts of the code here:

@Mappings({
     @Mapping(source = "programInstance", target = "title", qualifiedByName = "title"),
     @Mapping(source = "programInstance", target = "seriesName", qualifiedByName = "seriesName"),
     @Mapping(source = "programInstance", target = "season", qualifiedByName = "season"),
     @Mapping(source = "programInstance", target = "epNumber", qualifiedByName = "epNumber"),
 })
 DTO1 mapDTOs (DTO2 dto2);

  @Named("title")
default String mapTitle(Program programInstance) {
    Optional<String> title = Utils.getObject(() -> programInstance.getTitle().getDescriptions().get(0).getValue());
    if (title.isPresent())
        return title.get();
    return null;
}
@Named("seriesName")
default String mapSeriesName(Program programInstance) {
    Optional<String> seriesName = Utils.getObject(() -> programInstance.get(0).getProgram().getTitle().getDescriptions().get(0).getValue());
    if (seriesName.isPresent())
        return seriesName.get();
    return null;
}
 @Named("season")
default Integer mapSeasonNumber(Program programInstance) {
    Optional<Integer> season = Utils.getObject(() -> programInstance.get(0).getSeasonOf().get(0).getOrderNo());
    if (season.isPresent())
        return season.get();
    return null;
}

@Named("epNumber")
default Integer mapEpNumber(Program programInstance) {
    Optional<Integer> epNumber = Utils.getObject(() -> programInstance.getEpOf().get(0).getOrderNo());
    if (epNumber.isPresent())
        return epNumber.get();
    return null;
}

The error is

Ambiguous mapping methods found for mapping property "Program programInstance" to java.lang.String: java.lang.String mapTitle(), java.lang.String mapSeriesName().

Answer

Sjaak picture Sjaak · Feb 12, 2019

I checked your example.. The problem is that the fields you try to target are of type String.

So:

public class IvpVodOfferStatusDTO {

    private String seasonNumber;
    private String episodeNumber;
} 

MapStruct tries to match this with the signature you provide:

 @Named("season")
default Integer mapSeasonNumber(Program programInstance) {
    Optional<Integer> season = Utils.getObject(() -> programInstance.get(0).getSeasonOf().get(0).getOrderNo());
    if (season.isPresent())
        return season.get();
    return null;
}

@Named("epNumber")
default Integer mapEpNumber(Program programInstance) {
    Optional<Integer> epNumber = Utils.getObject(() -> programInstance.getEpOf().get(0).getOrderNo());
    if (epNumber.isPresent())
        return epNumber.get();
    return null;
}

MapStruct has a predefined order of attempts:

  1. User provided Mapping method
  2. Direct (types source -target are the same)
  3. Mapping method (built-in)
  4. Type conversion

If this all fails MapStruct tries to do a number of 2 step approaches:

  1. mapping method - mapping method
  2. mapping method - type conversion
  3. type conversion - mapping method

At 6. it finds 2 qualifying methods (Program to String). It's probably an error in MapStruct that it selects methods that do not qualify (need to check whether this is intentional) by the @Named. Otherwise, I'll write an issue.

The most easy solution is: adapt the target:

public class IvpVodOfferStatusDTO {

    private Integer seasonNumber;
    private Integer episodeNumber;
}

What is probably what you intend (I guess).. Otherwise you could change the signature not to return an Integer but a String