mapperly: Configuration apis using enums are broken in visual studio 16.7+

Describe the bug Starting with 3.0.0 the generator fails when using PropertyNameMappingStrategy.CaseInsensitive with error message.

Could not resolve enum reflection type of PropertyNameMappingStrategy or Riok.Mapperly.Abstractions.PropertyNameMappingStrategy is not supported.

Cleaning and rebuilding as well as cleaning, closing VS and deleting the .vs folder and reopening did not resolve the issue. Everything seems to build fine using the dotnet cli.

To Reproduce Try to build the sample code below from visual studio 2022.

Expected behavior The generator should complete without error and Stringfield should be mapped to StringField

Code snippets

using Riok.Mapperly.Abstractions;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var mapper = new Mapper();
            var from = new FromObject
            {
                Stringfield = "test"
            };

            var to = mapper.Map(from);

            Console.WriteLine(to.StringField);
        }
    }

    public class FromObject
    {
        public string Stringfield { get; set; }
    }

    public class ToObject
    {
        public string StringField { get; set; }
    }

    [Mapper(PropertyNameMappingStrategy = PropertyNameMappingStrategy.CaseInsensitive)]
    public partial class Mapper
    {
        public partial ToObject Map(FromObject input);
    }
}

Environment (please complete the following information):

  • Mapperly Version: 3.0.0
  • .NET Version: 6.0.411
  • Target Framework: net6.0
  • IDE: Visual studio 17.6.2
  • OS: Windows 10

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 20 (8 by maintainers)

Most upvoted comments

Nice digging 👍

I can recreate the issue, VS doesn’t run for me. Even after restarting

As I’m using macOS I don’t have access to VS and it works with the cli and in Rider. Does it work again if you downgrade to 2.8.0?

@latonz Yep, that version works! Thanks!

@pnquest could you verify this issue is resolved with 3.1.0-next.2?

I’ve tested it on a old VS Version (17.5), where the Code Snippet still works. After upgrading to 17,7 I also get that error

a bit more digging and I was able to get a stack trace from the generator:

'System.InvalidOperationException: Could not resolve enum reflection type of PropertyNameMappingStrategy or Riok.Mapperly.Abstractions.PropertyNameMappingStrategy is not supported
   at Riok.Mapperly.Configuration.AttributeDataAccessor.GetEnumValue(TypedConstant arg, Type targetType) in /_/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs:line 137
   at Riok.Mapperly.Configuration.AttributeDataAccessor.BuildArgumentValue(TypedConstant arg, Type targetType) in /_/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs:line 96
   at Riok.Mapperly.Configuration.AttributeDataAccessor.<Access>d__5`2.MoveNext() in /_/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs:line 56
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at Riok.Mapperly.Configuration.MapperConfiguration..ctor(SymbolAccessor symbolAccessor, ISymbol mapperSymbol) in /_/src/Riok.Mapperly/Configuration/MapperConfiguration.cs:line 16
   at Riok.Mapperly.Descriptors.DescriptorBuilder..ctor(Compilation compilation, ClassDeclarationSyntax mapperSyntax, INamedTypeSymbol mapperSymbol, WellKnownTypes wellKnownTypes, SymbolAccessor symbolAccessor) in /_/src/Riok.Mapperly/Descriptors/DescriptorBuilder.cs:line 36
   at Riok.Mapperly.MapperGenerator.BuildDescriptors(Compilation compilation, ImmutableArray`1 mappers, CancellationToken cancellationToken) in /_/src/Riok.Mapperly/MapperGenerator.cs:line 86
   at Riok.Mapperly.MapperGenerator.<>c.<Initialize>b__4_1(ValueTuple`2 x, CancellationToken cancellationToken) in /_/src/Riok.Mapperly/MapperGenerator.cs:line 32
   at Microsoft.CodeAnalysis.UserFunctionExtensions.<>c__DisplayClass0_0`2.<WrapUserFunction>b__0(TInput input, CancellationToken token)'.

Line numbers seem to have changed a bit, but that looks to be from here. https://github.com/riok/mapperly/blob/main/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs#L186

So it looks like this is returning null: https://github.com/riok/mapperly/blob/main/src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs#L191

Maybe visual studio is loading libraries into the assembly differently than the cli.