ProjectTo with value converters #3652
-
TL;DR: There might be a bug when AutoMapper tries to handle When building a REST application (not relevant) I ran into a potential bug in AutoMapper. When converting a Source/destination typesSource type (EFCore mapped model)public partial class Agent
{
....
public byte[] StagingKey { get; set; }
....
} Destination typepublic class AgentDto
{
....
public string StagingKey { get; set; }
....
} Mapping configurationpublic class AgentMapper : Profile
{
public AgentMapper()
{
CreateMap<Agent, AgentDto>()
.ForMember(dest => dest.StagingKey,
opt => opt.ConvertUsing<ByteToStringConverter, byte[]>())
.ReverseMap()
.ForMember(dest => dest.StagingKey,
opt => opt.ConvertUsing<StringToByteConverter, string>());
CreateMap<Byte, Char>();
CreateMap<Task, TaskDto>().ReverseMap();
}
}
class ByteToStringConverter : IValueConverter<byte[], string>
{
public string Convert(byte[] sourceMember, ResolutionContext context) =>
Encoding.Default.GetString(sourceMember);
}
class StringToByteConverter : IValueConverter<string, byte[]>
{
public byte[] Convert(string sourceMember, ResolutionContext context) =>
Encoding.Default.GetBytes(sourceMember);
} Version: x.y.z
Expected behavior
Actual behaviorAn exception is thrown ( For brevity I have only attached the relevant part of the stacktrace.
Steps to reproduce[HttpGet]
public async Task<ActionResult<IEnumerable<AgentDto>>> GetAgents()
{
return await _mapper.ProjectTo<AgentDto>(_context.Agents).ToListAsync();
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
https://docs.automapper.org/en/latest/Queryable-Extensions.html#supported-mapping-options |
Beta Was this translation helpful? Give feedback.
https://docs.automapper.org/en/latest/Queryable-Extensions.html#supported-mapping-options