Skip to content

ProjectToType: Error while compiling when using extension methods. #875

@fretje

Description

@fretje

When I update to the latest version that just came out, we get an "Error while compiling" when using ProjectToType:

Mapster.CompileException: Error while compiling
source=CloudConnector.Api.Domain.CommandRuns.CommandRun
destination=CloudConnector.Api.Application.CommandRuns.CommandRunDto
type=Projection
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'key')
   at System.Collections.Generic.Dictionary`2.FindValue(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at Mapster.Adapters.BaseClassAdapter.CreateClassConverter(Expression source, ClassModel classModel, CompileArgument arg, Expression destination, Boolean ctorMapping, ClassModel recordRestorMemberModel)
   at Mapster.Adapters.ClassAdapter.CreateInlineExpression(Expression source, CompileArgument arg, Boolean IsRequiredOnly)
   at Mapster.Adapters.BaseAdapter.CreateInlineExpressionBody(Expression source, CompileArgument arg)
   at Mapster.Adapters.BaseAdapter.CreateExpressionBody(Expression source, Expression destination, CompileArgument arg)
   at Mapster.Adapters.BaseAdapter.CreateAdaptFunc(CompileArgument arg)
   at Mapster.TypeAdapterConfig.CreateMapExpression(CompileArgument arg)
   --- End of inner exception stack trace ---
   at Mapster.TypeAdapterConfig.CreateMapExpression(CompileArgument arg)
   at Mapster.TypeAdapterConfig.CreateMapExpression(TypeTuple tuple, MapType mapType)
   at Mapster.TypeAdapterConfig.CreateProjectionCallExpression(TypeTuple tuple)
   at Mapster.TypeAdapterConfig.<>c__DisplayClass60_0`1.<AddToHash>b__0(TypeTuple types)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Mapster.TypeAdapterConfig.AddToHash[T](ConcurrentDictionary`2 hash, TypeTuple key, Func`2 func)
   at Mapster.TypeAdapterConfig.GetProjectionCallExpression(Type sourceType, Type destinationType)
   at Mapster.Extensions.ProjectToType[TDestination](IQueryable source, TypeAdapterConfig config)
   ...

This is the mapperconfiguration. It does use extension methods... that might be the culprit.

public class MapperConfiguration : IMapperConfiguration
{
    public void Configure() =>
        TypeAdapterConfig<CommandRun, CommandRunDto>.NewConfig()
            .Map(
                dest => dest.CommandShortName,
                src => src.GetCommandShortName())
            .Map(
                dest => dest.CommandParameterDescription,
                src => src.GetCommandParameterDescription());
}

And indeed, when I re-write the expressions so they don't use extension methods, it works again:

        TypeAdapterConfig<CommandRun, CommandRunDto>.NewConfig()
            .Map(
                dest => dest.CommandShortName,
                src => CommandTypes.ShortNameFor(src.CommandType))
             .Map(
                dest => dest.CommandParameterDescription,
                src => ((ICommand)RequestFactory.Create(src.CommandType, src.CommandJson)).GetParameterDescription();

So there is a workaround...
But this is a regression, I'd say...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions