Guido Glielmi
Guido Glielmi

Reputation: 73

How to ignore zero values with MapStruct

I am trying to update a postgres record based on what the http request is sending, which is unpredictable (can be any combination of attributes). Now, what i want to do is to get MapStruct to ignore "default" Java values (i.e. the ones not included in the request body) but it only ignores null values, so not default int type values (which is 0). I've accomplished it with expressions but it is not a very elegant solution. I've also tried using Integer type but still MapStruct treats it like a common int. My question is: ¿Is there a workaround ignoring properties that are equal to zero?

Upvotes: 0

Views: 697

Answers (1)

Ben Zegveld
Ben Zegveld

Reputation: 1516

With mapstruct 1.5.0 (currently at 1.5.0.Beta2) the @Condition annotation is introduced. With this you can define your own checks for when to map values.

for example to map all ints that are not 0 you could do the following:

@Condition
boolean shouldMap(int number) {
    return number != 0;
}

For more information see Conditional Mapping at the mapstruct reference guide.

Upvotes: 1

Related Questions