diff --git a/src/Mapster.Tests/WhenUseDestinatonValueMappingRegression.cs b/src/Mapster.Tests/WhenUseDestinatonValueMappingRegression.cs new file mode 100644 index 0000000..4200ada --- /dev/null +++ b/src/Mapster.Tests/WhenUseDestinatonValueMappingRegression.cs @@ -0,0 +1,106 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Shouldly; +using System.Collections.Generic; +using System.Linq; + +namespace Mapster.Tests; + +[TestClass] +public class WhenUseDestinatonValueMappingRegression +{ + [TestClass] + public class WhenUseDestinatonMappingRegression + { + [TestMethod] + public void UseDestinatonValueUsingMapWithasParam() + { + TypeAdapterConfig> + .NewConfig() + .MapWith(src => MapThumbnailDetailsData(src).ToList()); + + var channelSrc = new ChannelSource + { + ChannelId = "123", + Thumbnails = new ThumbnailDetailsSource + { + Default = new ThumbnailSource + { + Url = "https://www.youtube.com/default.jpg" + }, + Medium = new ThumbnailSource + { + Url = "https://www.youtube.com/medium.jpg" + }, + High = new ThumbnailSource + { + Url = "https://www.youtube.com/high.jpg" + } + }, + + TempThumbnails = new List() { 1, 2, 3 } + }; + + var channelDest = channelSrc.Adapt(); + + channelDest.Thumbnails.Count.ShouldBe(3); + channelDest.TempThumbnails.Count.ShouldBe(3); + } + + + #region TestClasses + private static IEnumerable MapThumbnailDetailsData(ThumbnailDetailsSource thumbnailDetails) + { + yield return MapThumbnail(thumbnailDetails.Default, "Default"); + yield return MapThumbnail(thumbnailDetails.Medium, "Medium"); + yield return MapThumbnail(thumbnailDetails.High, "High"); + } + + private static ThumbnailDestination MapThumbnail( + ThumbnailSource thumbnail, + string thumbnailType) => + new() + { + Type = thumbnailType, + Url = thumbnail.Url.Trim(), + }; + + + public class ChannelDestination + { + public string ChannelId { get; set; } = default!; + + [UseDestinationValue] + public ICollection Thumbnails { get; } = new List(); + + [UseDestinationValue] + public ICollection TempThumbnails { get; } = new List(); + } + + public class ThumbnailDestination + { + public string Type { get; set; } = default!; + public string Url { get; set; } = default!; + } + + public class ChannelSource + { + public string ChannelId { get; set; } = default!; + public ThumbnailDetailsSource Thumbnails { get; set; } = default!; + public ICollection TempThumbnails { get; set; } = new List(); + } + + public class ThumbnailDetailsSource + { + public ThumbnailSource? Default { get; set; } + public ThumbnailSource? Medium { get; set; } + public ThumbnailSource? High { get; set; } + } + + public class ThumbnailSource + { + public string Url { get; set; } = default!; + } + + #endregion TestClasses + } +} diff --git a/src/Mapster/Adapters/BaseAdapter.cs b/src/Mapster/Adapters/BaseAdapter.cs index 366f1ee..0c3e4fb 100644 --- a/src/Mapster/Adapters/BaseAdapter.cs +++ b/src/Mapster/Adapters/BaseAdapter.cs @@ -495,6 +495,21 @@ internal Expression CreateAdaptExpression(Expression source, Type destinationTyp if (transform != null) exp = transform.TransformFunc(exp.Type).Apply(arg.MapType, exp); } + else + { + if (exp.NodeType != ExpressionType.Invoke) + { + var argExt = new CompileArgument + { + DestinationType = arg.DestinationType, + SourceType = arg.DestinationType, + MapType = MapType.MapToTarget, + Context = arg.Context, + }; + + return CreateAdaptExpressionCore(exp, destinationType, argExt, mapping, destination).To(destinationType); + } + } return exp.To(destinationType); }