Skip to content

Commit ad1d3d2

Browse files
author
chaowlert
committed
FIX: do not validate explicit mapping for child property
1 parent 6efbcb3 commit ad1d3d2

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

src/Mapster.Tests/WhenExplicitMappingRequired.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ public void Mapped_Classes_Succeed()
5454
simpleDto.Name.ShouldEqual(simplePoco.Name);
5555
}
5656

57+
[Test]
58+
public void Mapped_Classes_Succeed_With_Child_Mapping()
59+
{
60+
TypeAdapterConfig.GlobalSettings.RequireExplicitMapping = true;
61+
62+
TypeAdapterConfig<CollectionPoco, CollectionDto>.NewConfig();
63+
64+
var collectionPoco = new CollectionPoco { Id = Guid.NewGuid(), Name = "TestName", Children = new List<ChildPoco>() };
65+
66+
var collectionDto = TypeAdapter.Adapt<CollectionPoco, CollectionDto>(collectionPoco);
67+
68+
collectionDto.Name.ShouldEqual(collectionPoco.Name);
69+
}
70+
5771

5872
#region TestClasses
5973

src/Mapster/MapContext.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,23 @@ public static MapContext Context
2727
get { return _context ?? (_context = new MapContext()); }
2828
}
2929

30+
public static bool HasContext
31+
{
32+
get { return _context != null; }
33+
}
34+
3035
private Dictionary<object, object> _references;
3136
public Dictionary<object, object> References
3237
{
3338
get { return _references ?? (_references = new Dictionary<object, object>(ReferenceComparer.Default)); }
3439
}
3540

41+
public static void EnsureContext()
42+
{
43+
if (_context == null)
44+
_context = new MapContext();
45+
}
46+
3647
public static void Clear()
3748
{
3849
_context = null;

src/Mapster/TypeAdapterConfig.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ internal LambdaExpression CreateInlineMapExpression(Type sourceType, Type destin
286286

287287
private LambdaExpression CreateInvokeExpression(Type sourceType, Type destinationType)
288288
{
289+
//ensure there is MapContext to prevent error on GetMergedSettings
290+
if (this.RequireExplicitMapping)
291+
MapContext.EnsureContext();
292+
289293
Expression invoker;
290294
if (this == GlobalSettings)
291295
{
@@ -306,7 +310,7 @@ private LambdaExpression CreateInvokeExpression(Type sourceType, Type destinatio
306310

307311
internal TypeAdapterSettings GetMergedSettings(Type sourceType, Type destinationType, MapType mapType)
308312
{
309-
if (this.RequireExplicitMapping && mapType != MapType.InlineMap)
313+
if (this.RequireExplicitMapping && mapType != MapType.InlineMap && !MapContext.HasContext)
310314
{
311315
if (!this.Dict.ContainsKey(new TypeTuple(sourceType, destinationType)))
312316
throw new InvalidOperationException(

src/Mapster/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "2.0.2-*",
2+
"version": "2.0.3-*",
33
"description": "A fast, fun and stimulating object to object mapper. Kind of like AutoMapper, just simpler and way, way faster.",
44
"summary": "A fast, fun and stimulating object to object mapper. Kind of like AutoMapper, but simpler and way faster.",
55
"iconUrl": "http://www.fancyicons.com/free-icons/103/pretty-office-5/png/128/order_128.png",

0 commit comments

Comments
 (0)