Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException with V4.X when trying to map #381

Closed
Skyppid opened this issue Dec 15, 2023 · 12 comments
Closed

NullReferenceException with V4.X when trying to map #381

Skyppid opened this issue Dec 15, 2023 · 12 comments
Assignees
Labels
Milestone

Comments

@Skyppid
Copy link

Skyppid commented Dec 15, 2023

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!

@dadhi
Copy link
Owner

dadhi commented Dec 15, 2023

What is this Mapper library?

@Skyppid
Copy link
Author

Skyppid commented Dec 21, 2023

What is this Mapper library?

Mapster, specifically this: https://github.com/MapsterMapper/Mapster/wiki/FastExpressionCompiler

@dadhi
Copy link
Owner

dadhi commented Dec 21, 2023

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.

@Skyppid
Copy link
Author

Skyppid commented Dec 22, 2023

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.

@cblaze22
Copy link

I am experiencing same thing. If I update, site gets null reference errors unlike before.

@dadhi
Copy link
Owner

dadhi commented Jan 15, 2024

@cblaze22 Again, if you can produce the small reproducible case with the Test, it will help a lot.

@dadhi
Copy link
Owner

dadhi commented Jan 17, 2024

it may be related to #386, specifically to the caching of the nullable type members

@dadhi dadhi self-assigned this Jan 17, 2024
@dadhi dadhi added the bug label Jan 19, 2024
@dadhi dadhi added this to the v4.0.2 milestone Jan 19, 2024
@dadhi
Copy link
Owner

dadhi commented Jan 19, 2024

Let see if it's fixed

@dadhi dadhi closed this as completed Jan 19, 2024
@dadhi
Copy link
Owner

dadhi commented Jan 20, 2024

The v4.1.0 with the fix is out on NuGet

@cblaze22
Copy link

cblaze22 commented Jan 29, 2024

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)

@dadhi
Copy link
Owner

dadhi commented Jan 30, 2024

@cblaze22 Pity, so I still need a smaller reproducible example.

@cblaze22
Copy link

Unfortunately this project is too big and uses Entity Framework so to get a small project would take too much time to do .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants