You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Having map in a mapping target, I want to have option to keep it's existing elements, just call putAll() on addiing new ones (or replacing existing with the same key)
class Test {
Map<String,String> customFields;
}
@Mapper
TestMapper {
@Mapping(target = "customFields", source = "customFields)
Test update(Test source, @MappingTarget Test target);
}
Generated Code
if ( target.getCustomFields() != null ) {
Map<String, String> map = source.getCustomFields();
if ( map != null ) {
// the following line would not be present
// target.getCustomFields().clear();
target.getCustomFields().putAll( map );
}
else {
target.setCustomFields( new java.util.HashMap<>() );
}
}
Possible workarounds
custom mapping implementation needed
MapStruct Version
1.5.5.Final
The text was updated successfully, but these errors were encountered:
@typekpb have you perhaps tried using an adder and CollectionMappingStrategy#ADDER_PREFERRED? I am not sure if we are clearing the collection in this case.
@typekpb have you perhaps tried using an adder and CollectionMappingStrategy#ADDER_PREFERRED? I am not sure if we are clearing the collection in this case.
I also have a need for this feature... or at least, something similar. In my case, it's a bit simpler, where both the source and the target have a property that is a List, and I want to just add any values in the source list proprety to any pre-existing values in the target list property.
I don't own the target model so can't easily add an "add" method to it as you suggest. We just really need a way to modify the current generated code:
if ( target.getListProperty() != null ) {
target.getListProperty.clear();
List<String> list = source.getListProperty();
if ( list != null ) {
target.getListProperty().addAll( list );
}
}
so that, it doesn't do the clear first:
if ( target.getListProperty() != null ) {
List<String> list = source.getListProperty();
if ( list != null ) {
target.getListProperty().addAll( list );
}
}
AND also handles the case where the target list is null but the sourceList is not null:
else { //target.getListProperty() == null
List<String> list = source.getListProperty();
if ( list != null ) {
target.setListProperty(new ArrayList<>(list));
}
}
Use case
Having map in a mapping target, I want to have option to keep it's existing elements, just call putAll() on addiing new ones (or replacing existing with the same key)
Generated Code
Possible workarounds
custom mapping implementation needed
MapStruct Version
1.5.5.Final
The text was updated successfully, but these errors were encountered: