Skip to content

Commit

Permalink
Merge pull request #415 from foriequal0/fix/unmapped-destination-value
Browse files Browse the repository at this point in the history
Fix RequireDestinationMemberSource for immutable UseDestinationValue field
  • Loading branch information
andrerav authored Mar 25, 2022
2 parents b175c92 + 4d022ea commit 15380d0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/Mapster.Tests/WhenHandlingUnmappedMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ public void Error_Thrown_With_Explicit_Configuration_On_Unmapped_Child_Collectio
}
}

[TestMethod]
public void Error_Thrown_With_Explicit_Configuration_And_UseDestinationValue_On_Unmapped_Child_Collection()
{
try
{
TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true;
TypeAdapterConfig<ListPoco, ListDto>.NewConfig()
.UseDestinationValue(model => model.Type.IsConstructedGenericType
&& model.Type.GetGenericTypeDefinition() == typeof(List<>))
.Compile();

var source = new ListPoco();

TypeAdapter.Adapt<ListPoco, ListDto>(source);
Assert.Fail();
}
catch (InvalidOperationException ex)
{
ex.ToString().ShouldContain("UnmappedReadOnlyList");
}
}

[TestMethod]
public void NoErrorWhenMapped()
Expand Down Expand Up @@ -173,6 +194,26 @@ public class ParentDto
public List<ChildDto> UnmappedChildren { get; set; }
}

public class ListPoco
{
public ListPoco()
{
List = new List<int>();
}

public List<int> List { get; }
}

public class ListDto
{
public ListDto()
{
UnmappedReadOnlyList = new List<int>();
}

public List<int> UnmappedReadOnlyList { get; }
}

#endregion


Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Adapters/BaseClassAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ select fn(src, destinationMember, arg))
propertyModel.Getter = Expression.New(typeof(Never));
properties.Add(propertyModel);
}
else if (destinationMember.SetterModifier != AccessModifier.None)
else if (destinationMember.UseDestinationValue(arg) || destinationMember.SetterModifier != AccessModifier.None)
{
if (classModel.BreakOnUnmatched)
return null!;
Expand Down

0 comments on commit 15380d0

Please sign in to comment.