Replace @Mapping(ignore=...) with @Mapping(ignoreStrategy=...)
Possible values for ignoreStrategy:
- ALWAYS (as true)
- NEVER (as false)
- NULL_SOURCE (ignore if source value is null) - often required in many projects
if (source.getValue() != null)
target.setValue (source.getValue());
- NULL_TARGET (ignore if target value is null)
if (target.getValue() != null)
target.setValue(source.getValue());
- other values ...
Interesting idea, I also think the use-case is a pretty common one.
I'd rather leave ignore as it is, but add some other way to more explicitly specify the cases 3 and 4. E.g. overwriteWithNull or overwriteNonNullTarget - well, not that great names, but I'm confident we can find something more intuitive.
This is exactly what I'm looking for.
In my case the Pojo I'm mapping has several properties and null ones I must ignore.
Is there a way to do it with current version, without having to write the code myself?
Thanks.
What about ignoreSourceNullValues and ignoreTargetNullValues?
I'm going to create a PR for this.
Hi @agudian @sjaakd @gunnarmorling , I am trying to to use Mapstruct. I have a requirement where in source value should not be mapped to target if it is null. But i am not able to find a way out for this. From the above discussion i think this is not implemented yet. Is there any way we can do this? If not, then are you taking this into consideration for your next release. When is the expected Release with this feature.
I'm pretty sure most of this issue is now covered by the new @Mapper#nullValueMappingStrategy, where you can define @Mapper(nullValueMappingStragety = ALWAYS) to perform null-checks around every setter invocation. It's on the current master now and we'll do a release some time next week with it.
What's not yet implemented is a more fine grained control on a per-mapping method level or per-property level (right, @sjaakd?). Therefore we should keep this issue open.
@0x3333 are you still interested in helping out?
Sure I'm. I just don't know where to start.
Some guidance would be great. I'm not used to MapStruct codebase, but I have some time to spend on this.
per-mapping method level (@BeanMapping): yes. per-property-level: I'm not sure what would be the use case to have such fine grained control. For frameworks like protobuf I reckon its more than sufficient to have it on @Mapper or even @MapperConfig level. Right?
@sjaakd for example for jpa to dto mapping I would like to exclude Hibernate proxies that are not yet initialized (as it would lead to n+1 loading/performance issues).
It should be possible to have:
if (!ignoreStrategy.isIgnored(source, "value")l) target.setValue (source.getValue());
where the application provides its own mapping strategy, as in this case the check whether a given property is either not a proxy or an initialized proxy.
@sjaakd We used dozer to map properties of an incoming entity to an existing entity(source and target class is same). The non-null fields in the incoming entity are mapped to the existing entity(null fields in incoming entity are ignored and these fields will remain unmodified in the existing entity). Finally, the updated/merged entity is persisted. How do we achieve this functionality in mapstruct?
@thiyanesh .. Have you checked out: @Mapper(nullValueMappingStragety = ALWAYS). See our ref guide for more info
@remmeier .. , @0x3333 is PR still required or is it covered by our current functionality. ?
@sjaakd Thank you. "NullValueCheckStrategy.ALWAYS" worked for the use-case.
A long as I can tell, fixed.
Hi @sjaakd , I am using mapstruct for mapping hierarchical data (both source and target have multi level objects).
The problem I have is , the target objects are getting created even if the source element is null at leaf node level.
In the example below even if the house number is not present in Customer.Address , UserDTO AddressDTO objects are getting created. The generated code , should check if the element is null till the leaf node level and if present then i should create required the target object
Another problem I facing is the generated code it is setting UserDTO.homeDTO to null if the customer.address is null. I will lose other values that were set in UserDTO.homeDTO.
Is there any way to achive this
Here is mapping that I am using
@Mapper( nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS )
public abstract class Customer2UserMapper {
Customer2UserMapper MAPPER = Mappers.getMapper(Customer2UserMapper.class);
@Mapping( source="address.houseNumber", target="homeDTO.addressDTO.houseNo")
abstract void mapKeyFinFigOverVw(Customer customer, @MappingTarget UserDTO userDTO);
}
The generated code is below
@Generated(
value = "org.mapstruct.ap.MappingProcessor"
)
public class Customer2UserMapperImpl extends Customer2UserMapper {
@Override
void mapKeyFinFigOverVw(Customer customer, UserDTO userDTO) {
if ( customer == null ) {
return;
}
if ( customer.getAddress() != null ) {
if ( userDTO.getHomeDTO() == null ) {
userDTO.setHomeDTO( new HomeDTO() );
}
addressToHomeDTO( customer.getAddress(), userDTO.getHomeDTO() );
}
else {
userDTO.setHomeDTO( null ); _//should not setting to NULL. HOMEDTO contains other elements that will removed._
}
}
protected void addressToAddressDTO(Address address, AddressDTO mappingTarget) {
if ( address == null ) { //this check is done twice . _If there is multiple hierarchical level then this check is e done multiple times_
return;
}
if ( address.getHouseNumber() != null ) {
mappingTarget.setHouseNo( address.getHouseNumber() );
}
}
protected void addressToHomeDTO(Address address, HomeDTO mappingTarget) {
if ( address == null ) { //this check is done twice
return;
}
if ( mappingTarget.getAddressDTO() == null ) {
mappingTarget.setAddressDTO( new AddressDTO() );
}
addressToAddressDTO( address, mappingTarget.getAddressDTO() );
}
}
I would like to be generated code to be similar to
[expected code.txt](https://github.com/mapstruct/mapstruct/files/1330798/expected.code.txt)
@sjaakd Here is my use case:
@Mappings({
@Mapping(target = "t0", source = "s0"),
@Mapping(target = "t1", source = "s1", qualifiedBy = Q1.class),
@Mapping(target = "t2", source = "s2", qualifiedBy = Q2.class)
})
TargetType map(SourceType s);
Now my problem is that at the beginning of every single custom mapping method that are triggered by Q1, Q2 etc. I have to check whether the parameter (s1, s2...) is null. This is a boilerplate code. I do think that it would be nice to tell declaratively that "hey, don't call Q1's mapping method when s1 is null, skip setting t1 or set t1 to null".
NullValueCheckStrategy.ALWAYS doesn't solve this problem when I still want t0 to be set when s0 is null. So it's too general.
Does that make sense to you?
@msundari : the problem you are facing is due to fact you are using nested mappings on both source and target site. My advice in the current setup would be to use individual mapping methods.
So: mapping address.houseNumber", target="homeDTO.addressDTO.houseNo")
- customer -> userDto
- address -> homeDTO
- address -> addressDto (you can pass the target directly as well)
- houseNumber -> houseNo
instead of doing it all from the top level.
But I can see some merit your case. Since the issue is not directly related to the current one (this one is particular to nested mappings), you can copy the text in a new issue. I can make it a feature request. Are you willing to help out?
See #1377
@malex211 : do I understand you correctly you want the fine grained control that @agudian proposed? If so: Can you open a new issue for that? I'll proceed to close this particular one since I think it has been solved.