Mapstruct check null

Viewed 18140

I wand check check null in method

@Override
 public void updateFooFromNonNullAttributesOfDto(FooDto fooDto, Foo foo) {
        if ( fooDto== null ) {
            return;
        }
        if ( fooDto.getBar() != null ) {
            site.setBar( fooDto.getBar() );
        }
         if ( fooDto.getBaz() != null ) {
            site.setBar( fooDto.getBaz() );
        }
}

When I use

@Mapper( NullValueCheckStrategy.ALWAYS)

It's check in all methods, but I want check only in one... How to solve this problem?

2 Answers

This is not yet possible with MapStruct, you can either do for all or for none. There is already a feature request for the exact same thing. See 1243

You can use some custom Ignore startegy.

Source of discussion :[link][1]

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());

[1]: https://github.com/mapstruct/mapstruct/issues/369 ???

Bug? I can modify the other's answer????

Related