Skip to content

Commit 975816d

Browse files
author
Claus
committed
UmbracoMapper shouldn't throw an exception when trying to map a source property being null.
1 parent 8831860 commit 975816d

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/Umbraco.Core/Mapping/UmbracoMapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public TTarget Map<TSource, TTarget>(TSource source, MapperContext context)
191191
private TTarget Map<TTarget>(object source, Type sourceType, MapperContext context)
192192
{
193193
if (source == null)
194-
throw new ArgumentNullException(nameof(source));
194+
return default;
195195

196196
var targetType = typeof(TTarget);
197197

src/Umbraco.Tests/Mapping/MappingTests.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,24 @@ public void EnumMap()
195195
Assert.AreEqual(Thing6Enum.Banana, thing6.Fruit2);
196196
Assert.AreEqual(Thing6Enum.Cherry, thing6.Fruit3);
197197
}
198-
198+
199+
[Test]
200+
public void NullPropertyMap()
201+
{
202+
var definitions = new MapDefinitionCollection(new IMapDefinition[]
203+
{
204+
new MapperDefinition5(),
205+
});
206+
var mapper = new UmbracoMapper(definitions);
207+
208+
var thing7 = new Thing7();
209+
210+
var thing8 = mapper.Map<Thing7, Thing8>(thing7);
211+
212+
Assert.IsNotNull(thing8);
213+
Assert.IsNull(thing8.Things);
214+
}
215+
199216

200217
private class Thing1
201218
{
@@ -241,6 +258,16 @@ private enum Thing6Enum
241258
Cherry = 2
242259
}
243260

261+
private class Thing7
262+
{
263+
public IEnumerable<Thing1> Things { get; set; }
264+
}
265+
266+
private class Thing8
267+
{
268+
public IEnumerable<Thing2> Things { get; set; }
269+
}
270+
244271
private class MapperDefinition1 : IMapDefinition
245272
{
246273
public void DefineMaps(UmbracoMapper mapper)
@@ -294,5 +321,24 @@ private void Map(Thing5 source, Thing6 target, MapperContext context)
294321
target.Fruit3 = context.Map<Thing6Enum>(source.Fruit3);
295322
}
296323
}
324+
325+
private class MapperDefinition5 : IMapDefinition
326+
{
327+
public void DefineMaps(UmbracoMapper mapper)
328+
{
329+
mapper.Define<Thing1, Thing2>((source, context) => new Thing2(), Map1);
330+
mapper.Define<Thing7, Thing8>((source, context) => new Thing8(), Map2);
331+
}
332+
333+
private void Map1(Thing1 source, Thing2 target, MapperContext context)
334+
{
335+
target.Value = source.Value;
336+
}
337+
338+
private void Map2(Thing7 source, Thing8 target, MapperContext context)
339+
{
340+
target.Things = context.Map<IEnumerable<Thing2>>(source.Things);
341+
}
342+
}
297343
}
298344
}

0 commit comments

Comments
 (0)