Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 34a5096

Browse filesBrowse files
committed
#3387 Document nullValueIterablePropertyMappingStrategy and nullValueMapPropertyMappingStrategy
Add a dedicated section to chapter-10-advanced-mapping-options.asciidoc, mirroring the existing section for nullValueIterableMappingStrategy / nullValueMapMappingStrategy, and note the new attributes in NEXT_RELEASE_CHANGELOG.md.
1 parent e989338 commit 34a5096
Copy full SHA for 34a5096

2 files changed

+44Lines changed: 44 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎NEXT_RELEASE_CHANGELOG.md‎

Copy file name to clipboardExpand all lines: NEXT_RELEASE_CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
### Features
22

3+
- [#3387](https://github.com/mapstruct/mapstruct/issues/3387) Add `nullValueIterablePropertyMappingStrategy` and `nullValueMapPropertyMappingStrategy` on `@Mapping`, `@BeanMapping`, `@Mapper` and `@MapperConfig`, allowing Iterable- and Map-typed properties to use a different null-handling strategy than the rest of the bean.
4+
35
### Enhancements
46

57
### Bugs
Collapse file

‎documentation/src/main/asciidoc/chapter-10-advanced-mapping-options.asciidoc‎

Copy file name to clipboardExpand all lines: documentation/src/main/asciidoc/chapter-10-advanced-mapping-options.asciidoc
+42Lines changed: 42 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,48 @@ Some types of mappings (collections, maps), in which MapStruct is instructed to
265265
null check, regardless of the value of the `NullValuePropertyMappingStrategy`, to avoid addition of `null` to the target collection or map. Since the target is assumed to be initialised this strategy will not be applied.
266266
====
267267

268+
[[mapping-result-for-null-iterable-or-map-properties]]
269+
=== Controlling mapping result for 'null' Iterable or Map properties
270+
271+
With <<mapping-result-for-null-properties>> it is possible to control how a target property is set when the corresponding source property is `null` or not present. That strategy is applied uniformly, regardless of the type of the target property.
272+
273+
However, MapStruct also offers a more dedicated way to control this for target properties of Iterable (including array) or Map type, e.g. reset collections / maps to their *default* (empty) value, while setting plain bean properties to `null`.
274+
275+
For target properties of `Collection`, `Iterable` or array type this can be controlled through:
276+
277+
* `MapperConfig#nullValueIterablePropertyMappingStrategy`
278+
* `Mapper#nullValueIterablePropertyMappingStrategy`
279+
* `BeanMapping#nullValueIterablePropertyMappingStrategy`
280+
* `Mapping#nullValueIterablePropertyMappingStrategy`
281+
282+
For target properties of `Map` type this can be controlled through:
283+
284+
* `MapperConfig#nullValueMapPropertyMappingStrategy`
285+
* `Mapper#nullValueMapPropertyMappingStrategy`
286+
* `BeanMapping#nullValueMapPropertyMappingStrategy`
287+
* `Mapping#nullValueMapPropertyMappingStrategy`
288+
289+
If left unconfigured on a given annotation, the more specific (Iterable / Map) strategy falls back to `nullValuePropertyMappingStrategy` configured on that same annotation, before falling back to the next, less specific, annotation level. How the resulting `NullValuePropertyMappingStrategy` value is applied is the same as in <<mapping-result-for-null-properties>>.
290+
291+
.Mapper with dedicated null strategies for Iterable / Map properties
292+
====
293+
[source, java, linenums]
294+
[subs="verbatim,attributes"]
295+
----
296+
@Mapper(
297+
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_NULL,
298+
nullValueIterablePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT,
299+
nullValueMapPropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT
300+
)
301+
public interface OrderMapper {
302+
303+
void update(OrderDto dto, @MappingTarget Order target);
304+
}
305+
----
306+
====
307+
308+
Given the mapper above, a `null` `tags` (`List`) or `meta` (`Map`) source property resets the corresponding target property to an empty collection / map, while a `null` `address` (a plain bean property) sets the target property to `null`, without having to repeat `nullValuePropertyMappingStrategy = SET_TO_DEFAULT` on every Iterable / Map `@Mapping` individually.
309+
268310
[TIP]
269311
====
270312
`NullValuePropertyMappingStrategy` also applies when the presence checker returns `not present`.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.