Mapstruct check null
Asked Answered
T

5

7

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?

Thoracoplasty answered 24/11, 2017 at 8:13 Comment(3)
May be you can use @Nullable/@NotNull annotations at the method declarations.Foret
MapStruct does not have support for those annotations yet see #1243Condense
The above method looks like a candidate for an @AfterMapping method in the mapper?Domingo
C
4

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

Condense answered 24/11, 2017 at 13:42 Comment(2)
how to prevent it for all ? No null check at all ?Annamariaannamarie
That is the default value. NullValueCheckStrategy.ON_IMPLICIT_CONVERSIONCondense
S
1

You could annotade your mapping function with a conditionExpression

<i>@Mapping(source="bar", target="bar", conditionExpression = "java(fooDto.getBar()!=null)")</i>
Surroundings answered 7/11, 2022 at 11:36 Comment(2)
But, that's what MapStruct will do by default ..Domingo
You are right, so the original question is now the standard behaviour?Surroundings
C
0

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????

Cleo answered 24/11, 2017 at 8:24 Comment(2)
I see this article (it's very old), but can't understand how use ignoreStrategyThoracoplasty
That part is not implemented yet, it is just a feature requestCondense
D
0

According to your example, you give fooDto.getBaz() precedence over fooDto.getBar(). You could realise this in the Mapper-Interface like:


/*
 * regular mapping FooDto.bar to Foo.bar
 */
Foo map(FooDto fooDto);

/*
 * Overwrite Foo.bar by FooDto.baz if present
 */
@AfterMapping
default updateFooFromNonNullAttributesOfDto(FooDto fooDto, Foo foo) {
  if (fooDto== null ) {
    return;
  }
  if(fooDto.getBaz() != null) {
   foo.setBar(fooDto.getBaz());
  }
}

Domingo answered 7/11, 2022 at 12:1 Comment(0)
A
-1

In MapStruct, if you want to apply a null value check strategy to only one method instead of all methods, you can use the @Mapping annotation with the nullValueCheckStrategy attribute directly on the method you want to customize. This way, you can specify the null value check strategy for that particular method without affecting other methods in the mapper.

Here's how you can achieve this:

Define Your Mapper:

Annotate your mapper interface with @Mapper without specifying the NullValueCheckStrategy. Use @Mapping on the specific method to apply NullValueCheckStrategy.ALWAYS. Example Implementation:

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueCheckStrategy;
import org.mapstruct.factory.Mappers;

@Mapper
public interface FooMapper {

    FooMapper INSTANCE = Mappers.getMapper(FooMapper.class);

    @Mapping(target = "bar", source = "bar", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
  
    void updateFooFromNonNullAttributesOfDto(FooDto fooDto, Foo foo);
}
Amman answered 15/6, 2024 at 7:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.