Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 93 additions & 3 deletions src/Mapster.Tests/WhenAddingCustomMappings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using static Mapster.Tests.WhenMappingDerived;

namespace Mapster.Tests
{
Expand Down Expand Up @@ -44,8 +46,59 @@ public void Property_Is_Mapped_From_Null_Value_Successfully()
dto.AnotherName.ShouldBeNull();
}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/980
/// </summary>
[TestMethod]
public void ExtraSourceUsingCustomResolverSuccessfully()
{
TypeAdapterConfig config = new();
config.NewConfig<Entity980, Dto980>().Map(e => e, e => e.Props);
config.NewConfig<List<EntityProp980>, Dto980>()
.Map(e => e.Address, e => e.Get("Address"))
.Map(e => e.Description, e => e.Get("Description"))
.Map(e => e.Phone, e => e.Get("Phone"));

config
.NewConfig<Dto980, Entity980>().Map(e => e.Props, e => e);

config.NewConfig<Dto980, List<EntityProp980>>()
.MapWith(x=> x.ToMemerList());

Entity980 ent = new()
{
Name = "My entity",
Props = new(),
};

ent.Props.Add(new() { Key = "Phone", Value = "12345678" });
ent.Props.Add(new() { Key = "Address", Value = "Default street" });
ent.Props.Add(new() { Key = "Description", Value = "Sample text" });


var dto = ent.Adapt<Dto980>(config);
var entity = dto.Adapt<Entity980>(config);

entity
.ShouldSatisfyAllConditions(() =>
{
dto.Phone.ShouldBe("12345678");
dto.Address.ShouldBe("Default street");
dto.Description.ShouldBe("Sample text");

entity.Name.ShouldBe("My entity");
entity.Props.Count.ShouldBe(3);
entity.Props[0].Key.ShouldBe("Phone");
entity.Props[0].Value.ShouldBe("12345678");
});
}

#region TestClasses





public class SimplePoco
{
public Guid Id { get; set; }
Expand Down Expand Up @@ -98,4 +151,41 @@ public class CollectionDto

#endregion
}
class Entity980
{
public string Name { get; set; } = default!;
public List<EntityProp980> Props { get; set; } = default!;
}

public class EntityProp980
{
public string Key { get; set; } = default!;
public string Value { get; set; } = default!;
}

public class Dto980
{
public string Name { get; set; } = default!;
public string? Address { get; set; }
public string? Description { get; set; }
public string? Phone { get; set; }

public List<EntityProp980> ToMemerList()
{
var result = new List<EntityProp980>();

result.Add(new EntityProp980 { Key = "Phone", Value = Phone });
result.Add(new() { Key = "Address", Value = Address });
result.Add(new() { Key = "Description", Value = Description });

return result;
}
}

public static class ListExtensions980
{
// utility method as expression bodies are not allowed to have null propagating operator
public static string? Get(this List<EntityProp980> list, string key)
=> list.FirstOrDefault(e => e.Key == key)?.Value;
}
}
2 changes: 1 addition & 1 deletion src/Mapster/Settings/ValueAccessingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class ValueAccessingStrategy

private static Expression? CustomResolverFn(Expression source, IMemberModel destinationMember, CompileArgument arg)
{
var config = arg.Settings;
var config = source.Type == arg.SourceType ? arg.Settings : arg.Context.Config.GetMergedSettings(new TypeTuple(source.Type, arg.DestinationType),arg.MapType);
var resolvers = config.Resolvers;
if (resolvers.Count == 0)
return null;
Expand Down
Loading