Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapstruct check null

Tags:

java

mapstruct

I want to 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?

like image 618
LeshaRB Avatar asked Aug 08 '26 01:08

LeshaRB


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

like image 89
Filip Avatar answered Aug 10 '26 14:08

Filip


You could annotade your mapping function with a conditionExpression

<i>@Mapping(source="bar", target="bar", conditionExpression = "java(fooDto.getBar()!=null)")</i>
like image 44
Blub Avatar answered Aug 10 '26 15:08

Blub