Additional parameter in mapstruct mapper
Asked Answered
A

3

10

I have Car:

  • id
  • brand
  • model
  • owner

And CarDTO:

  • id
  • brand
  • model

In my service class I'm passing additional parameter "owner" and I need to convert the list.

Is it possible to add "owner" to Mapper?

If yes then I suppose it should be something similar to this (not working).

@Mapper
public interface CarMapper {

@Mapping(target = "owner", source = "owner")
List<Car> mapCars(List<CarDTO> cars, String owner);
}
Arianaariane answered 20/12, 2022 at 15:32 Comment(3)
You can try using an expression: @Mapping(target = "owner", expression = "java(owner)").Quintic
Hmm... This was the only solution that worked for me...Aminoplast
For Kotlin this way it works, @Mappings( Mapping(target = "stockItemId",expression = "java(stockItemToMap)"), ) fun convertFromResponseToModel( stockMappingPatchRequest: StockMappingPatchRequest, @Context stockItemToMap: StockItem, ): StockMappingUtgardloki
S
10

As described in the answer, you can use @Context.

Firstly, add a single object mapping method:

@Maping(target = "owner", source = "owner")
Car mapCar(CarDTO car, String owner);

Then define a method for mapping a list of objects with @Context:

List<Car> mapCars(List<CarDTO> cars, @Context String owner);

Since @Context parameters are not meant to be used as source parameters, a proxy method should be added to point MapStruct to the right single object mapping method to make it work.

In the end, add the proxy method:

default Car mapContext(CarDTO car, @Context String owner) {
    return mapCar(car, owner);
}
Staats answered 21/12, 2022 at 8:34 Comment(2)
I'm getting a build error java: No property named "owner" exists in source parameter(s). Did you mean "yyy"? mapstruct 1.5.3.Final Do you possibly know what is causing that?Cotidal
@Cotidal I don't know if you already solved this, but I had this problem when I accidentally put @Context on the single object mapping method too. (In the answer there wasn't.)Volin
R
1

I didnt even have to use context. I could simply pass the additional parameter and the mapper recognises it in the build.

    NotMORV2Entity toNotMorV2Entity(ReEntity reEntity, int glAccountTypeId);

Please also see this to understand more.

Rootless answered 18/1, 2024 at 15:53 Comment(0)
J
0

Yes it is possible. You could try the @Context annotation before "owner"

I would suggest something like this:

@Mapper
public interface CarMapper {

@Maping(target = "owner", source = "owner")
Car mapCar(CarDTO car, @Context String owner);

@Maping(target = "owner", source = "owner")
List<Car> mapAllCars(List<CarDTO> cars, @Context String owner);

@AfterMapping
default void afterMapping(@MappingTarget Car target, @Context String owner) {
    target.setOwner(owner);
}

there are multiple ways to approache your problem. You could also have a look at Passing additional parameters to MapStruct mapper

Jankey answered 21/12, 2022 at 8:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.