mapstruct / mapstruct/mapstruct

Inconsistent behaviour with @MappingTarget, source objects and null values

Open
#3,453 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
7.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

### Expected behavior

I'd expect the targetvalues for the source object to be updated even if there is no dto present

### Actual behavior

The targetvalue for the source object will only be updated if the dto or at least one of the source objects is not null.
| DTO | source object| expected | actual |
|--------|--------|--------|--------|
| set | set | update with dto-contents and source object | update with dto-contents and source object |
| set | null | update with dto-contents and source object to null | update with dto-contents and source object to null |
| null | set | no update with dto-contents but updated source object |no update with dto-contents but updated source object |
| null | null | no update with dto-contents but updated source object to null | no update at all |

I think the issue is the guard clause at the beginning of the generated implementation. This should be ommitted if there's a parameter with a @MappingTarget annotation

### Steps to reproduce the problem

https://github.com/nicktar/mapstruct-mcve
The failing unit test demostrated the issue

### MapStruct Version

MapStruct 1.5.5-Final

Contributor guide

Open the contributing guide

Research direction

Start with the failing unit test in the nicktar/mapstruct-mcve reproduction and inspect the generated implementation's guard clause. Verify the four DTO and source-object combinations, then confirm that a @MappingTarget is updated even when both the DTO and source object are null.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.