Skip to content

Commit 58b6db5

Browse files
authored
Merge pull request #2640 from lbargaoanu/dynamic_mapping_null
Dynamic mapping : Map null to the default for value types
2 parents c9bdff2 + 63355e8 commit 58b6db5

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

src/AutoMapper/Internal/ReflectionHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static object MapMember(ResolutionContext context, MemberInfo member, obj
3232
{
3333
var memberType = GetMemberType(member);
3434
var destValue = destination == null ? null : GetMemberValue(member, destination);
35-
return context.Map(value, destValue, value?.GetType() ?? memberType, memberType, PropertyMap.Default);
35+
return context.Map(value, destValue, value?.GetType() ?? typeof(object), memberType, PropertyMap.Default);
3636
}
3737

3838
public static bool IsDynamic(object obj) => obj is IDynamicMetaObjectProvider;

src/UnitTests/Mappers/DynamicMapperTests.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Destination
1212
public string Bar { get; set; }
1313
internal string Jack { get; set; }
1414
public int[] Data { get; set; }
15+
public int Baz { get; set; }
1516
}
1617

1718
public class DynamicDictionary : DynamicObject
@@ -63,10 +64,11 @@ public void Should_map_source_properties()
6364
{
6465
var config = new MapperConfiguration(cfg => { });
6566
var data = new[] { 1, 2, 3 };
66-
_destination = config.CreateMapper().Map<DynamicDictionary>(new Destination { Foo = "Foo", Bar = "Bar", Data = data });
67-
((int)_destination.Count).ShouldBe(3);
67+
_destination = config.CreateMapper().Map<DynamicDictionary>(new Destination { Foo = "Foo", Bar = "Bar", Data = data, Baz = 12 });
68+
((int)_destination.Count).ShouldBe(4);
6869
Assert.Equal("Foo", _destination.Foo);
6970
Assert.Equal("Bar", _destination.Bar);
71+
Assert.Equal(12, _destination.Baz);
7072
((int[])_destination.Data).SequenceEqual(data).ShouldBeTrue();
7173
}
7274
}
@@ -130,6 +132,24 @@ public void Should_map_existing_properties()
130132
_destination.Foo.ShouldBe("Foo");
131133
_destination.Bar.ShouldBeNull();
132134
}
135+
}
136+
137+
public class When_mapping_from_dynamic_null_to_int
138+
{
139+
Destination _destination;
140+
141+
[Fact]
142+
public void Should_map_to_zero()
143+
{
144+
dynamic source = new DynamicDictionary();
145+
source.Foo = "Foo";
146+
source.Baz = null;
147+
var config = new MapperConfiguration(cfg => { });
148+
_destination = config.CreateMapper().Map<Destination>(source);
149+
_destination.Foo.ShouldBe("Foo");
150+
_destination.Bar.ShouldBeNull();
151+
_destination.Baz.ShouldBe(0);
152+
}
133153
}
134154

135155
public class When_mapping_from_dynamic_to_dynamic

src/UnitTests/Mappers/StringDictionaryMapperTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Destination
99
{
1010
public string Foo { get; set; }
1111
public string Bar { get; set; }
12+
public int Baz { get; set; }
1213
}
1314

1415
public class When_mapping_to_StringDictionary : NonValidatingSpecBase
@@ -47,6 +48,7 @@ public void Should_map_destination_properties()
4748
{
4849
_destination.Foo.ShouldBe("Foo");
4950
_destination.Bar.ShouldBe("Bar");
51+
_destination.Baz.ShouldBe(0);
5052
}
5153
}
5254

@@ -93,6 +95,28 @@ public void Should_map_existing_properties()
9395
{
9496
_destination.Foo.ShouldBe("Foo");
9597
_destination.Bar.ShouldBeNull();
98+
_destination.Baz.ShouldBe(0);
99+
}
100+
}
101+
102+
public class When_mapping_from_StringDictionary_null_to_int : NonValidatingSpecBase
103+
{
104+
Destination _destination;
105+
106+
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg => { });
107+
108+
protected override void Because_of()
109+
{
110+
var source = new StringDictionary() { { "Foo", "Foo" }, { "Baz", null } };
111+
_destination = Mapper.Map<Destination>(source);
112+
}
113+
114+
[Fact]
115+
public void Should_map_to_zero()
116+
{
117+
_destination.Foo.ShouldBe("Foo");
118+
_destination.Bar.ShouldBeNull();
119+
_destination.Baz.ShouldBe(0);
96120
}
97121
}
98122

0 commit comments

Comments
 (0)