I often come across situations where objects have fields on different levels (e.g. target has flat fields, source has multiple nested objects and fields are on different levels)
Let's take the following example (getters/setters removed for clarity):
public class MapstructExample {
public static class FlatTarget {
public String targetA;
public String targetB;
}
public static class NestedOuter {
public String sourceA;
public NestedInner nestedInner;
}
public static class NestedInner {
public String sourceB;
}
@Mapper
public static interface MyMapper {
@Mapping(target = "targetA", source = "sourceA")
@Mapping(target = "targetB", source = "nestedInner.sourceB") // redundant mapping info
//@Mapping(target = ".", source = "nestedInner") // not working
FlatTarget convert1(NestedOuter in);
@Mapping(target = "targetB", source = "sourceB") // redundant mapping info
FlatTarget convert2(NestedInner in);
@Mapping(target = "sourceA", source = "targetA")
@Mapping(target = "nestedInner", source = ".") // no redundancy because "." works
NestedOuter convert3(FlatTarget in);
@Mapping(target = "sourceB", source = "targetB")
NestedInner convert4(FlatTarget in);
}
}
As you can see the last 2 mappings have no repetition, because I can declare source = "." and it passes the main object to the next mapping method.
Unfortunately target = "." doesn't seem to work (and applying @InheritInverseConfiguration(name="convert3") to the first method causes and error)
I have also tried to formulate all converter method using @MappingTarget but it doesn't change the behavior.
Is there a way to avoid repeating the mappings of all fields of the NestedInner object? @InheritConfiguration is also not an option because the mappings are declared on a different layer (every field is prefixed with nestedInner in the second method).
Typically I work around the issue by declaring @AfterMapping methods which call the other method to fill in the missing parts of the objects, but I hope there is a better way to declare these mappings (or perhaps there is a way to get target = "." to work which would allow automatic inversion of mappings using source = ".")
EDIT: I have reformatted the example in order to have a fully working example and named the convert methods from 1-4 for clarity
I often come across situations where objects have fields on different levels (e.g. target has flat fields, source has multiple nested objects and fields are on different levels)
Let's take the following example (getters/setters removed for clarity):
As you can see the last 2 mappings have no repetition, because I can declare
source = "."and it passes the main object to the next mapping method.Unfortunately
target = "."doesn't seem to work (and applying@InheritInverseConfiguration(name="convert3")to the first method causes and error)I have also tried to formulate all converter method using
@MappingTargetbut it doesn't change the behavior.Is there a way to avoid repeating the mappings of all fields of the NestedInner object?
@InheritConfigurationis also not an option because the mappings are declared on a different layer (every field is prefixed withnestedInnerin the second method).Typically I work around the issue by declaring
@AfterMappingmethods which call the other method to fill in the missing parts of the objects, but I hope there is a better way to declare these mappings (or perhaps there is a way to gettarget = "."to work which would allow automatic inversion of mappings usingsource = ".")EDIT: I have reformatted the example in order to have a fully working example and named the convert methods from 1-4 for clarity