1

I have the following methods in my @Mapper class:

@Mapping(source = "localCar.color", target = "color")
@Mapping(source = "blueBookCar.price", target = "price")
@Mapping(source = "localCar.features", target = "features", qualifiedByName = "mapFeatures") 
abstract CarDto from(LocalCar localCar, BlueBookCar blueBookCar);

@Named("mapFeatures")
Map<Long, Feature> mapFeatures(Map<Group, Feature> features, BlueBookCar blueBookCar) {
 // do stuff with both blueBookCar and features from localCar
}

However, I get a compile error stating no map struct method mapFeatures(Map) found.

From this post, I tried to use @Context blueBookCar for both methods but then I get an error that it can be used to map in the from method.

The only solution I can find is to pass in the blueBookCar twice once annotated with @Context and once without:

@Mapping(source = "localCar.color", target = "color")
@Mapping(source = "blueBookCar.price", target = "price")
@Mapping(source = "localCar.features", target = "features", qualifiedByName = "mapFeatures") 
abstract CarDto from(LocalCar localCar, BlueBookCar blueBookCar, @Context BlueBookCar blueBookCar2)

@Named("mapFeatures")
Map<Long, Feature> mapFeatures(Map<Group, Feature> features, @Context BlueBookCar blueBookCar2) {
 //do stuff with both blueBookCar and features from localCar
}

How can I use a parameter to map in both @Mapping and @Named methods?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
James
  • 2,876
  • 18
  • 72
  • 116

1 Answers1

1

As far as I know, mapping methods with multiple source properties is not yet supported using @Named methods.

However, you can use a Java expression workaround:

@Mapper
public abstract class CardMapper {

    @Mapping(target = "color", source = "localCar.color")
    @Mapping(target = "price", source = "blueBookCar.price")
    @Mapping(target = "features", expression = "java(mapFeatures(localCar, blueBookCar))")
    public abstract CarDto from(LocalCar localCar, BlueBookCar blueBookCar);

    protected Map<Long, Feature> mapFeatures(LocalCar localCar, BlueBookCar blueBookCar) {
        //do stuff with both blueBookCar and features from localCar
    }
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183