mapstruct / mapstruct/mapstruct

Non-Null check on nested collection mapping

Open
#1,679 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
7.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

I have a set of classes like the following

ClassRoot, ClassA, ClassB

ClassRootDto, ClassADto, ClassBDto

where

ClassRoot has a list of ClassA, and ClassA has a list of ClassB

ClassRootDto has a list of ClassADto, and ClassADto has a list of ClassBDto

I tried to map a ClassRoot instance to a ClassRootDto instance

The list of ClassADto instances is explicit set to the field of ClassRootDto instance without any problem.

However, when set the list of ClassBDto to the field of a ClassADto instance, the generated code does a null check on the target field.

Why are there different null-check behaviors for different level of mappings?

My current solution:
Initialize an empty ClassBDto list in ClassADto field

My expectation
I expect there should be no null-check on target field when doing collection mapping

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the nested ClassRoot, ClassA, and ClassB mapping described in the issue, then inspect the generated mapper where the ClassBDto collection is assigned. Compare that generated code with the ClassADto collection assignment and determine whether the target null check should differ. Done means the expected behavior is clarified and covered by a regression test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.