NullValuePropertyMappingStrategy.SET_TO_DEFAULT should set target Map/Collection to default when source and target are all null
Discussed in #3865
Originally posted by tangyang9464 May 23, 2025
For the following mapper:
public interface DestinationType { Map<String, String> getAttributes(); void setAttributes(Map<String, String> attributes); } public class SourceType implements DestinationType{ Map<String, String> attributes; public Map<String, String> getAttributes() { return attributes; } public void setAttributes(Map<String, String> attributes) { this.attributes = attributes; } } @Mapper(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT) public interface MyMapper{ MyMapper INSTANCE = Mappers.getMapper( MyMapper.class ); void update(@MappingTarget DestinationType destination, SourceType source); }
Should this test pass? Currently it is not possible, I don't know if it is by design or a bug?
public void shouldGenerateUpdateMethod() { DestinationType target = new SourceType(); SourceType source = new SourceType(); MyMapper.INSTANCE.update(target , source); assertThat(target.getAttributes()).isNotEqualTo(null); }
Generated code:
public void update(DestinationType destination, SourceType source) { if ( source == null ) { return; } if ( destination.getAttributes() != null ) { Map<String, String> map = source.getAttributes(); if ( map != null ) { destination.getAttributes().clear(); destination.getAttributes().putAll( map ); } else { destination.setAttributes( new LinkedHashMap<String, String>() ); } } else { Map<String, String> map = source.getAttributes(); if ( map != null ) { destination.setAttributes( new LinkedHashMap<String, String>( map ) ); } // Why is there no esle branch here? // else { // destination.setAttributes( new LinkedHashMap<String, String>() ) //}; } } ```</div>