Mapstruct: Skip not null properties on target

Created on 21 Apr 2018  路  7Comments  路  Source: mapstruct/mapstruct

Hi folks!
Is there a way to fill properties on target only if it have null values?
Ex:

Source
prop1 = "val1"
prop2 = "val2"

Target
prop1 = null
prop2 = "val3"

Result
prop1 = "val1"
prop2 = "val3"

in other words:

if (target.prop1 == null) {
   target.setProp1(source.prop1);
}
if (target.prop2 == null) {
   target.setProp2(source.prop2);
}

feature

Most helpful comment

There is an example of my idea:

@Mapping(target = "foo",
         filterExpression = "java( target.getFoo() != null  )"
)

The result will be something like

if([filterExpression]) {
   target.setXXX(souce.getXXX);
}

In that whay, other situations can be acomplished, like override only if target(or source) has certain values or pass in some business validation made by an injected service.
Let me know what you think about it.

Best regards.

All 7 comments

Hi filiphr!

Thanks for the response.
I checked on the documentation but i need to perform a null check on the target before set the property, not on source.
I have sent the question on the gitter channel.

Again thanks for your time.
Best regards

@thiagogjt sorry it's my mistake. I understood it wrong. This is currently not possible with MapStruct. The null of a target is only checked when we have nested target mappings, but in this case only a new object is set.

If you don't mind me asking, what exactly is the use case for this?

My data came from different sources and this sources have a priority order.
In one of that cases the data came from a local sevice and have his properties enhanced by a remote one, but only the properties that are missing should be filled.
I have thought about and if we have the possibility to assign an expression in mapping to filter the assigngment we can achieve others use cases as well.

@thiagogjt there might be a way to achieve this if you update the properties that are incoming, that way the properties from the higher priority bean would override the ones in the incoming and those that are not present in the higher priority would stay in the incoming.

You could also do this by a small combination of mappings. You would first map the incoming into a result, and then update the result with the properties from the higher priority one. Although that might not work due to #879 (you'll have to try).

I have thought about and if we have the possibility to assign an expression in mapping to filter the assignment we can achieve others use cases as well.

What do you mean to have the possibility to assign an expression?

There is an example of my idea:

@Mapping(target = "foo",
         filterExpression = "java( target.getFoo() != null  )"
)

The result will be something like

if([filterExpression]) {
   target.setXXX(souce.getXXX);
}

In that whay, other situations can be acomplished, like override only if target(or source) has certain values or pass in some business validation made by an injected service.
Let me know what you think about it.

Best regards.

For me it would be also a nice option to have for all fields in the object.
We have streaming updates coming in but we need to update fields only if target fields is null

Was this page helpful?
0 / 5 - 0 ratings