Skip to content

Commit

Permalink
FIX: do not validate explicit mapping for child property
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Jan 20, 2016
1 parent 6efbcb3 commit ad1d3d2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/Mapster.Tests/WhenExplicitMappingRequired.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ public void Mapped_Classes_Succeed()
simpleDto.Name.ShouldEqual(simplePoco.Name);
}

[Test]
public void Mapped_Classes_Succeed_With_Child_Mapping()
{
TypeAdapterConfig.GlobalSettings.RequireExplicitMapping = true;

TypeAdapterConfig<CollectionPoco, CollectionDto>.NewConfig();

var collectionPoco = new CollectionPoco { Id = Guid.NewGuid(), Name = "TestName", Children = new List<ChildPoco>() };

var collectionDto = TypeAdapter.Adapt<CollectionPoco, CollectionDto>(collectionPoco);

collectionDto.Name.ShouldEqual(collectionPoco.Name);
}


#region TestClasses

Expand Down
11 changes: 11 additions & 0 deletions src/Mapster/MapContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,23 @@ public static MapContext Context
get { return _context ?? (_context = new MapContext()); }
}

public static bool HasContext
{
get { return _context != null; }
}

private Dictionary<object, object> _references;
public Dictionary<object, object> References
{
get { return _references ?? (_references = new Dictionary<object, object>(ReferenceComparer.Default)); }
}

public static void EnsureContext()
{
if (_context == null)
_context = new MapContext();
}

public static void Clear()
{
_context = null;
Expand Down
6 changes: 5 additions & 1 deletion src/Mapster/TypeAdapterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ internal LambdaExpression CreateInlineMapExpression(Type sourceType, Type destin

private LambdaExpression CreateInvokeExpression(Type sourceType, Type destinationType)
{
//ensure there is MapContext to prevent error on GetMergedSettings
if (this.RequireExplicitMapping)
MapContext.EnsureContext();

Expression invoker;
if (this == GlobalSettings)
{
Expand All @@ -306,7 +310,7 @@ private LambdaExpression CreateInvokeExpression(Type sourceType, Type destinatio

internal TypeAdapterSettings GetMergedSettings(Type sourceType, Type destinationType, MapType mapType)
{
if (this.RequireExplicitMapping && mapType != MapType.InlineMap)
if (this.RequireExplicitMapping && mapType != MapType.InlineMap && !MapContext.HasContext)
{
if (!this.Dict.ContainsKey(new TypeTuple(sourceType, destinationType)))
throw new InvalidOperationException(
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.0.2-*",
"version": "2.0.3-*",
"description": "A fast, fun and stimulating object to object mapper. Kind of like AutoMapper, just simpler and way, way faster.",
"summary": "A fast, fun and stimulating object to object mapper. Kind of like AutoMapper, but simpler and way faster.",
"iconUrl": "http://www.fancyicons.com/free-icons/103/pretty-office-5/png/128/order_128.png",
Expand Down

0 comments on commit ad1d3d2

Please sign in to comment.