dadhi/FastExpressionCompiler

NullReferenceException with V4.X when using long?[]

KolyaNET opened this issue · 2 comments

Hello. I used FastExpressionCompiler version 3.3.4 and everything worked. After updating to version 4.1.0 I started getting errors. I also use Mapster 7.4.0.

I was able to find the problem using a NUnit test

using FastExpressionCompiler;
using Mapster;
using NUnit.Framework;

namespace Garda.Unit.Tests.Mapster;

[TestFixture]
[Parallelizable(ParallelScope.Default)]
internal sealed class NullItemArrayFastExpressionCompilerTests
{
    private sealed record ExportInfoDto(long[] Locations);

    private sealed record ExportInfoModel(long?[] Locations);

    private class ReportAdaptRule : TypeAdapterRule
    {
        internal ReportAdaptRule()
        {
            TypeAdapterConfig<ExportInfoModel, ExportInfoDto>
                .NewConfig();
        }
    }

    [TestCase(CompilerFlags.Default)]
    [TestCase(CompilerFlags.NoInvocationLambdaInlining)]
    [TestCase(CompilerFlags.EnableDelegateDebugInfo)]
    [TestCase(CompilerFlags.ThrowOnNotSupportedExpression)]
    public void NullItemArray(CompilerFlags compilerFlags)
    {
        // Arrange
        var report = new ExportInfoModel(new long?[] { 1 });

        TypeAdapterConfig.RulesTemplate.Add(new ReportAdaptRule());

        TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileFast(flags: compilerFlags);
        TypeAdapterConfig.GlobalSettings.Compile();

        // Act
        Assert.Throws<NullReferenceException>(() => report.Adapt<ExportInfoDto>());
    }
}

If I change long?[] to long[] or return version 3.3.4 then the NullReferenceException will no longer occur

Similar problems:

@KolyaNET Hi, thanks for the Test and the references to the similar issues.

As I understood, the problem is with the mapping long?[] to long[] or is it the other way around?

If it is from long?[] to long[] what happens with nulls, are they skipped?

@KolyaNET The test is passing in the latest code, so it is hopefully fixed.