question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement NullPropertyMappingStrategy to allow more flexibility when encountering null values

See original GitHub issue

Is your feature request related to a problem? Please describe. See https://github.com/riok/mapperly/discussions/96. Main use case is to merge two instances while only setting values which are non-null.

Describe the solution you’d like Implement different strategies on how to handle null values when mapping property values.

API

public sealed class MapperAttribute : Attribute
{
+    [Obsolete($"Use {nameof(NullPropertyMappingStrategy)} instead")]
      public bool ThrowOnPropertyMappingNullMismatch
      {
          get =>  NullPropertyMappingStrategy == NullPropertyMappingStrategy.SetOrThrowIfNull;
          set =>  NullPropertyMappingStrategy = NullPropertyMappingStrategy.SetOrThrowIfNull;
       }

+    /// <summary>
+    /// Specifies the behaviour how to handle property mappings when <c>null</c> values are involved.
+    /// </summary>
+    public NullPropertyMappingStrategy NullPropertyMappingStrategy { get; set; } = NullPropertyMappingStrategy.SetOrIgnoreIfNull;
}

+/// <summary>
+/// Strategies on how to handle <c>null</c> values when mapping property values.
+/// </summary>
+public enum NullPropertyMappingStrategy
+{
+    /// <summary>
+    /// Sets the value if the target is nullable or the source is not <c>null</c>.
+    /// Ignores the mapping if the source is <c>null</c> and the target is not nullable
+    /// </summary>
+    SetOrIgnoreIfNull,
+
+    /// <summary>
+    /// Sets the value if the target is nullable or the source is not <c>null</c>.
+    /// Throws if the source is <c>null</c> and the target is not nullable
+    /// </summary>
+    SetOrThrowIfNull,
+
+    /// <summary>
+    /// Sets the value if the target is nullable or the source is not <c>null</c>.
+    /// Sets a default value otherwise (<c>default</c> for value types, empty string for strings, <c>new()</c> for classes; throw if no parameterless ctor exists).
+    /// </summary>
+    SetOrDefaultIfNull,
+
+    /// <summary>
+    /// Ignores the property if the source is <c>null</c>.
+    /// </summary>
+    IgnoreIfNull,
+
+    /// <summary>
+    /// Sets the value if the source is not <c>null</c>.
+    /// Sets a default value otherwise.
+    /// Similar to <see cref="SetOrDefaultIfNull"/> but does never set <c>null</c> values.
+    /// </summary>
+    DefaultIfNull,
+}

Backward compatibility SetOrIgnoreIfNull is the same as ThrowOnPropertyMappingNullMismatch=false, SetOrThrowIfNull is the same as ThrowOnPropertyMappingNullMismatch=true. The existing ThrowOnPropertyMappingNullMismatch property is kept around but marked as obsolete. If ThrowOnPropertyMappingNullMismatch is false, the new NullPropertyMappingStrategy should be used. If ThrowOnPropertyMappingNullMismatch is true, always SetOrThrowIfNull should be used. The documentation should be updated accordingly.

Additional context See https://github.com/riok/mapperly/discussions/96 and https://github.com/riok/mapperly/discussions/452.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:8
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
markushaslingercommented, May 2, 2023

Awesome, thanks for checking and the update! I tested with 2.8.0-next.1 and can confirm it seems to be working as expected 👍

1reaction
latonzcommented, May 2, 2023

Ah got it, in the case you mentioned Mapperly is mapping to constructor parameters and not properties. The behaviour you expect should be the default behaviour, but there is a bug in 2.7.0 leading to the behaviour you observed. This bug should be resolved in 2.8.0-next.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to achieve NullValuePropertyMappingStrategy. ...
You can use the parameter defaultValue on the @Mapping annotation to map any null value attribute from the source to the specified default...
Read more >
NullValuePropertyMappingStrate...
Hi guys, I'm using MapStruct 1.3.1 final and facing a problem when using NullValuePropertyMappingStrategy.IGNORE on mapping methods. To give ...
Read more >
Enum NullValuePropertyMappingStrategy
Strategy for dealing with null or not present properties in the source bean. The NullValuePropertyMappingStrategy can be defined on MapperConfig , Mapper ...
Read more >
Ignoring Unmapped Properties with MapStruct
While mapping identical fields with identical field names is very straightforward, we often encounter mismatched beans. In this tutorial, we'll ...
Read more >
Java 8 as a baseline, null value property mappings ...
We have added a NullValuePropertyMappingStrategy that can be used to control how null or not present source properties are updated within a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found