Using mapstruct, what I need is a mapping method with several sources and that these several sources are passed around to other mapping methods so I can have all my several sources for all mapping methods where I need these additional sources.
Currently there are two features that could be made to work together maybe :
- only the @Context can be passed around to other mappings methods but it can't be used as a source.
- a secondary parameter (non @Context) can be used as a source but it is not passed around to other mapping methods
So the feature need would be either to allow secondary source parameters to be passed around to other mapping methods or to make the @Context parameter able to be referenced by a @Mapping(target="something", source="ctx.somethingElse")
or @Mapping(target="ctx.something", source="somethingElse)
Example :
// source classes : `Instant timestamp` is a field I obtain separately
Instant timestamp;
class WrapperSource
List<NestedSource> nested;
class NestedSource
String name;
// target classes : I want to map the nested and name field but also to insert the timestamp in both the WrapperTarget and every NestedTarget in the nested list
class WrapperTarget
Instant timestamp;
List<NestedTarget> nested;
class NestedTarget
String name;
Instant timestamp;
Ideally, the mapping would be something like :
// Currently this doesn't work because we can't reference the @Context in the source attribute
@Mapping(target = "nested", source="source.nested")
@Mapping(target = "timestamp", source="timestamp")
WrapperTarget map(WrapperSource source, @Context Instant timestamp);
@Mapping(target = "name", source="source.name")
@Mapping(target = "timestamp", source="timestamp")
NestedTarget map(NestedSource source, @Context Instant timestamp);
Or :
// Currently this doesn't work because the second method with 2 sources in not called by the first generated method
@Mapping(target = "nested", source="source.nested")
@Mapping(target = "timestamp", source="timestamp")
WrapperTarget map(WrapperSource source, Instant timestamp);
@Mapping(target = "name", source="source.name")
@Mapping(target = "timestamp", source="timestamp")
NestedTarget map(NestedSource source, Instant timestamp);
The only (verbose) workaround that works for me is :
// @Context is passed around and I can manually use it as a source in an @AfterMapping but it requires additional code
WrapperTarget map(WrapperSource source, @Context Instant timestamp);
@AfterMapping
void map(WrapperSource source, @MappingTarget WrapperTarget target, @Context Instant timestamp) {
target.setTimestamp(timestamp);
}
NestedTarget map(NestedSource source, @Context Instant timestamp);
@AfterMapping
void map(NestedSource source, @MappingTarget NestedTarget target, @Context Instant timestamp) {
target.setTimestamp(timestamp);
}
This works alright but it required additional manual code, so a better alternative would be to be able to reference a @Context
in a @Mapping's attributes
. This way I could use the first "ideal" mapping example.
Is there a better workaround for this issue ?