Skip to content

Commit

Permalink
Fix issue mapping from nullable bool to bool value
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jul 26, 2023
1 parent 8c947ce commit 61343f3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ServiceStack.Text/src/ServiceStack.Text/AutoMappingUtils.cs
Expand Up @@ -1170,7 +1170,8 @@ public void Populate(object to, object from)
var fromValue = assignmentEntry.GetValueFn(from);

if (valuePredicate != null
&& fromType == toType) // don't short-circuit nullable <-> non-null values
&& (fromType == toType
|| Nullable.GetUnderlyingType(fromType) == toType)) // don't short-circuit nullable <-> non-null values
{
if (!valuePredicate(fromValue, fromMember.PropertyInfo.PropertyType))
continue;
Expand Down
Expand Up @@ -1758,6 +1758,7 @@ class NullableSource
public int? NullableIdNonMatchingUnderlyingType { get; set; }
public int? NullableIdMatchingUnderlyingType { get; set; }
public DateTime? DateTime { get; set; }
public bool NullableBool { get; set; }
}
class NullableTarget
{
Expand All @@ -1767,6 +1768,7 @@ class NullableTarget
public DateTime? NullableDateTime { get; set; }
public DateTime DateTime { get; set; }
public DateTime TargetOnlyDateTime { get; set; }
public bool? NullableBool { get; set; }
}

[Test]
Expand All @@ -1782,4 +1784,17 @@ public void Does_convert_nullables_of_different_types()
Assert.That(target.DateTime, Is.EqualTo(default(DateTime)));
Assert.That(target.TargetOnlyDateTime, Is.EqualTo(default(DateTime)));
}

[Test]
public void Does_not_populate_bool_with_nullable_bool()
{
var to = new NullableSource {
NullableBool = true,
};

var from = new NullableTarget();
to.PopulateWithNonDefaultValues(from);

Assert.That(to.NullableBool, Is.True);
}
}

0 comments on commit 61343f3

Please sign in to comment.