mapstruct / mapstruct/mapstruct

Nesting on null target property

Open
#1,166 36 comments 16 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement for:team-discussion up-for-grabs
Dominant language
Java
Stars
7.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

When I use the following mapping configuration with a nested property:

@Mapping(target = "nestedId", source = "nested.id")
ObjectA map(ObjectB source);

@InheritInverseConfiguration
ObjectB map(ObjectA source);

Mapstruct generates the inverse mapping below:

ObjectB objectB = new ObjectB();

Nested nested = new Nested();
objectB.setNested(nested);

nested.setId(source.getNestedId());

But when nested is null I get an instance of ObjectB with a non-null instance as nested property containing a null id property.
I need to get an instance of ObjectB with null value on its nested property.

Can I configure MapStruct in this way?

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

The issue provides the mapping configuration and generated inverse code, but no repository paths or tests. Start by tracing how @InheritInverseConfiguration handles the nested.id mapping and verify the null-target case; done means a null nested source does not produce a non-null nested object, with regression coverage for the shown mappings.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.