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 #406

Closed
KolyaNET opened this issue Apr 19, 2024 · 0 comments
Closed

NullReferenceException with V4.X #406

KolyaNET opened this issue Apr 19, 2024 · 0 comments
Assignees
Labels
Milestone

Comments

@KolyaNET
Copy link

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)]
public sealed class NullReferenceExceptionFastExpressionCompilerTests
{
    public sealed record AgentStatusDto
    {
        public int ErrorCode { get; init; }

        public int CompleteTaskType { get; set; }
    }

    public sealed record ResulAgent(ResponseAgent Common);

    public sealed record ResponseAgent(int ErrorType, int TaskType, int Code);

    internal class AgentStatusDtoAdaptRule : TypeAdapterRule
    {
        internal AgentStatusDtoAdaptRule()
        {
            TypeAdapterConfig<ResulAgent, AgentStatusDto>
                .NewConfig()
                .Map(dst => dst.ErrorCode, src => src.Common.Code)
                .AfterMapping(ToDtoMap);
        }

        private static void ToDtoMap(ResulAgent res, AgentStatusDto dto)
        {
            dto.CompleteTaskType = res.Common.TaskType switch
            {
                2 => 3,
                _ => default
            };
        }
    }

    [TestCase(CompilerFlags.Default)]
    [TestCase(CompilerFlags.NoInvocationLambdaInlining)]
    public void NullReferenceException(CompilerFlags compilerFlags)
    {
        // Arrange
        var result = new ResulAgent(new ResponseAgent(1, 2, 3));

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

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

        // Act
        if (compilerFlags == CompilerFlags.Default)
        {
            Assert.Throws<NullReferenceException>(() => result.Adapt<AgentStatusDto>());
        }
        else
        {
            var resultStatus = result.Adapt<AgentStatusDto>();

            // Assert
            Assert.NotNull(resultStatus, nameof(resultStatus));
        }
    }
}

If I revert to version 3.3.4 or use the NoInvocationLambdaInlining flag the NullReferenceException will no longer be thrown.

Similar problems:

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

2 participants