NullReferenceException with V4.X when trying to map
Skyppid opened this issue · 12 comments
We've first discovered this issue when upgrading to .NET 8 (as we also upgraded all packages) and thought it was a .NET issue. After hours and days of investigation we found out that FastExpressionCompiler was the culprit (badly enough I forgot we were using it, otherwise I would have found it out way faster probably, but well).
When upgrading from 3.4.0-preview01 to 4.0 we suddenly get the following exception when trying to map our projects:
System.NullReferenceException: Object reference not set to an instance of an object.
at ProjectOverviewDto (ArrayClosure, ProjectModel)
at TDestination Mapster.TypeAdapterBuilder.Map()
at TDestination Mapster.TypeAdapterBuilder.AdaptToType()
at async Task Mapster.TypeAdapterExtensions.AdaptToTypeAsync(IAdapterBuilder builder)
at async Task<PaginatedList> Common.Utilities.PaginatedListExtensions.Project<TSource, TProject>(PaginatedList source, IMapper mapper)
The mapping looks as follows:
public sealed class ProjectOverviewDto : IMapFrom<ProjectModel>
{
public ObjectId Id { get; set; }
public string Name { get; set; } = string.Empty;
public string ProjectNumber { get; set; } = string.Empty;
public ProjectStatusDto Status { get; set; } = null!;
public AuditStatus AuditStatus { get; set; }
public ProjectPriority EmployeePriority { get; set; }
public ProjectPriority CustomerPriority { get; set; }
public bool HasOpenDiscussions { get; set; }
public bool HasOpenTasks { get; set; }
public bool HasClosedTasks { get; set; }
public AuditInfoDto AuditInfo { get; set; } = null!;
public List<ProjectEditorDto> Editors { get; set; } = null!;
public void ConfigureMapping(TypeAdapterConfig config)
{
config.NewConfig<ProjectModel, ProjectOverviewDto>()
.Map(m => m.AuditStatus, m => m.PlantCertificate.AuditStatus.Status)
.Map(m => m.Editors, m => m.Editors)
.AfterMapping((source, dto) =>
{
var fields = source.IterateDataFieldEntities().SelectMany(entity => entity.DataFields).ToImmutableArray();
dto.HasOpenDiscussions = fields.Any(field => field.ChatThreads.Any(t => t.State == ChatThreadState.InDiscussion));
dto.HasOpenTasks = fields.Any(f => f.Tasks.Any(t => t.State == TaskState.Open));
dto.HasClosedTasks = fields.Any(f => f.Tasks.Any(t => t.State == TaskState.Done));
});
}
}
When removing the fast expression compiler it works. Any idea how that happens? Sadly we don't get any useful exception message, but something seems to be off between those two versions. Big regression.
Thanks!
What is this Mapper library?
What is this Mapper library?
Mapster, specifically this: https://github.com/MapsterMapper/Mapster/wiki/FastExpressionCompiler
Could you minimize your example so it can be pit into isolated test?
I should be able to reproduce it in FEC here. You may check other tests to give you an example in the IssueTests project.
I'll try as soon as I find time. I'll be on vacation until new year and have some more pressing things to do today. The main problem is that this issue only occured on our staging environment hosted in AKS while it worked locally. I tried to get some meaningful info but since all debugging tools don't work in our pods due to this issue, it's hard to debug.
I'll report back if I can reproduce it again and can deliver you something. Until then I guess you can ignore this.
I am experiencing same thing. If I update, site gets null reference errors unlike before.
@cblaze22 Again, if you can produce the small reproducible case with the Test, it will help a lot.
Let see if it's fixed
The v4.1.0 with the fix is out on NuGet
Unfortunately still not working for me
System.NullReferenceException: Object reference not set to an instance of an object.
at (ArrayClosure , Season )
at MapsterMapper.ServiceMapper.Map[TSource,TDestination](TSource source)
Unfortunately this project is too big and uses Entity Framework so to get a small project would take too much time to do .