NullValuePropertyMappingStrategy.SET_TO_DEFAULT should set target Map/Collection to default when source and target are all null #3865
-
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>() )
//};
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
filiphr
May 25, 2025
Replies: 1 comment 1 reply
-
That looks like a bug @tangyang9464. Looking at the generated code for I can't say with 100% certainty why it isn't there. We can see if we have some test failures if we fix it. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
tangyang9464
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That looks like a bug @tangyang9464. Looking at the generated code for
UserMapper
andDomainDtoWithNvmsDefaultMapper
.I can't say with 100% certainty why it isn't there. We can see if we have some test failures if we fix it.