From 14a4ed3e59c571f200b886a9839bba42631965e1 Mon Sep 17 00:00:00 2001 From: Silvan Date: Wed, 6 Dec 2023 10:57:56 +0100 Subject: [PATCH] fix: resolve correct mapper defaults for roslyn versions <= 4.4 (#958) --- src/Riok.Mapperly/SyntaxProvider.Roslyn4.0.cs | 9 +++---- .../Mapper/EnumMapper.cs | 23 ++++++++++++++++ .../Mapper/MapperDefaults.cs | 4 +++ .../Mapper/ProjectionMapper.cs | 2 +- .../Mapper/StaticTestMapper.cs | 2 +- .../Mapper/TestMapper.cs | 4 +-- .../MapperDefaultsTest.cs | 26 +++++++++++++++++++ ...tsTest.SnapshotGeneratedSource.verified.cs | 17 ++++++++++++ 8 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 test/Riok.Mapperly.IntegrationTests/Mapper/EnumMapper.cs create mode 100644 test/Riok.Mapperly.IntegrationTests/Mapper/MapperDefaults.cs create mode 100644 test/Riok.Mapperly.IntegrationTests/MapperDefaultsTest.cs create mode 100644 test/Riok.Mapperly.IntegrationTests/_snapshots/MapperDefaultsTest.SnapshotGeneratedSource.verified.cs diff --git a/src/Riok.Mapperly/SyntaxProvider.Roslyn4.0.cs b/src/Riok.Mapperly/SyntaxProvider.Roslyn4.0.cs index ea3df77e68..2532be4241 100644 --- a/src/Riok.Mapperly/SyntaxProvider.Roslyn4.0.cs +++ b/src/Riok.Mapperly/SyntaxProvider.Roslyn4.0.cs @@ -30,7 +30,7 @@ public static IncrementalValuesProvider GetMapperDeclarations .SyntaxProvider .CreateSyntaxProvider( static (s, _) => s is CompilationUnitSyntax { AttributeLists.Count: > 0 }, - static (ctx, ct) => GetMapperDefaultDeclarations(ctx, ct) + static (ctx, ct) => GetMapperDefaultDeclarations(ctx) ) .Collect() .Select((x, _) => x.FirstOrDefault()); @@ -45,12 +45,9 @@ public static IncrementalValuesProvider GetMapperDeclarations return HasAttribute(symbol, MapperGenerator.MapperAttributeName) ? new MapperDeclaration(symbol, declaration) : null; } - private static IAssemblySymbol? GetMapperDefaultDeclarations(GeneratorSyntaxContext ctx, CancellationToken cancellationToken) + private static IAssemblySymbol? GetMapperDefaultDeclarations(GeneratorSyntaxContext ctx) { - var declaration = (CompilationUnitSyntax)ctx.Node; - if (ctx.SemanticModel.GetDeclaredSymbol(declaration, cancellationToken) is not IAssemblySymbol symbol) - return null; - + var symbol = ctx.SemanticModel.Compilation.Assembly; return HasAttribute(symbol, MapperGenerator.MapperDefaultsAttributeName) ? symbol : null; } diff --git a/test/Riok.Mapperly.IntegrationTests/Mapper/EnumMapper.cs b/test/Riok.Mapperly.IntegrationTests/Mapper/EnumMapper.cs new file mode 100644 index 0000000000..b5fcc1a7d8 --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/Mapper/EnumMapper.cs @@ -0,0 +1,23 @@ +using Riok.Mapperly.Abstractions; + +// is used to test mapper defaults +namespace Riok.Mapperly.IntegrationTests.Mapper +{ + [Mapper] + public static partial class EnumMapper + { + public static partial Enum2 Map(Enum1 e); + } + + public enum Enum1 + { + Value1 = 4, + Value2 = 7, + } + + public enum Enum2 + { + Value1, + Value2, + } +} diff --git a/test/Riok.Mapperly.IntegrationTests/Mapper/MapperDefaults.cs b/test/Riok.Mapperly.IntegrationTests/Mapper/MapperDefaults.cs new file mode 100644 index 0000000000..706199dfda --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/Mapper/MapperDefaults.cs @@ -0,0 +1,4 @@ +using Riok.Mapperly.Abstractions; + +// this is tested with EnumMapper in MapperDefaultsTest +[assembly: MapperDefaults(EnumMappingStrategy = EnumMappingStrategy.ByName)] diff --git a/test/Riok.Mapperly.IntegrationTests/Mapper/ProjectionMapper.cs b/test/Riok.Mapperly.IntegrationTests/Mapper/ProjectionMapper.cs index abccfac006..2e22dcddce 100644 --- a/test/Riok.Mapperly.IntegrationTests/Mapper/ProjectionMapper.cs +++ b/test/Riok.Mapperly.IntegrationTests/Mapper/ProjectionMapper.cs @@ -5,7 +5,7 @@ namespace Riok.Mapperly.IntegrationTests.Mapper { - [Mapper] + [Mapper(EnumMappingStrategy = EnumMappingStrategy.ByValue)] public static partial class ProjectionMapper { public static partial IQueryable ProjectToDto(this IQueryable q); diff --git a/test/Riok.Mapperly.IntegrationTests/Mapper/StaticTestMapper.cs b/test/Riok.Mapperly.IntegrationTests/Mapper/StaticTestMapper.cs index f3c186b63d..8847be089e 100644 --- a/test/Riok.Mapperly.IntegrationTests/Mapper/StaticTestMapper.cs +++ b/test/Riok.Mapperly.IntegrationTests/Mapper/StaticTestMapper.cs @@ -6,7 +6,7 @@ namespace Riok.Mapperly.IntegrationTests.Mapper { - [Mapper] + [Mapper(EnumMappingStrategy = EnumMappingStrategy.ByValue)] public static partial class StaticTestMapper { public static partial int DirectInt(int value); diff --git a/test/Riok.Mapperly.IntegrationTests/Mapper/TestMapper.cs b/test/Riok.Mapperly.IntegrationTests/Mapper/TestMapper.cs index b4d84ef967..0baba471e5 100644 --- a/test/Riok.Mapperly.IntegrationTests/Mapper/TestMapper.cs +++ b/test/Riok.Mapperly.IntegrationTests/Mapper/TestMapper.cs @@ -12,9 +12,9 @@ namespace Riok.Mapperly.IntegrationTests.Mapper { #if NET8_0_OR_GREATER - [Mapper(IncludedMembers = MemberVisibility.All)] + [Mapper(IncludedMembers = MemberVisibility.All, EnumMappingStrategy = EnumMappingStrategy.ByValue)] #else - [Mapper] + [Mapper(EnumMappingStrategy = EnumMappingStrategy.ByValue)] #endif public partial class TestMapper { diff --git a/test/Riok.Mapperly.IntegrationTests/MapperDefaultsTest.cs b/test/Riok.Mapperly.IntegrationTests/MapperDefaultsTest.cs new file mode 100644 index 0000000000..3b7e84df6b --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/MapperDefaultsTest.cs @@ -0,0 +1,26 @@ +using System.Threading.Tasks; +using FluentAssertions; +using Riok.Mapperly.IntegrationTests.Mapper; +using VerifyXunit; +using Xunit; + +namespace Riok.Mapperly.IntegrationTests +{ + [UsesVerify] + public class MapperDefaultsTest : BaseMapperTest + { + [Fact] + public Task SnapshotGeneratedSource() + { + var path = GetGeneratedMapperFilePath(nameof(EnumMapper)); + return Verifier.VerifyFile(path); + } + + [Fact] + public void RunMappingShouldWork() + { + var enum2 = EnumMapper.Map(Enum1.Value1); + enum2.Should().Be(Enum2.Value1); + } + } +} diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperDefaultsTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperDefaultsTest.SnapshotGeneratedSource.verified.cs new file mode 100644 index 0000000000..fa66a7cfbd --- /dev/null +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperDefaultsTest.SnapshotGeneratedSource.verified.cs @@ -0,0 +1,17 @@ +// +#nullable enable +namespace Riok.Mapperly.IntegrationTests.Mapper +{ + public static partial class EnumMapper + { + public static partial global::Riok.Mapperly.IntegrationTests.Mapper.Enum2 Map(global::Riok.Mapperly.IntegrationTests.Mapper.Enum1 e) + { + return e switch + { + global::Riok.Mapperly.IntegrationTests.Mapper.Enum1.Value1 => global::Riok.Mapperly.IntegrationTests.Mapper.Enum2.Value1, + global::Riok.Mapperly.IntegrationTests.Mapper.Enum1.Value2 => global::Riok.Mapperly.IntegrationTests.Mapper.Enum2.Value2, + _ => throw new System.ArgumentOutOfRangeException(nameof(e), e, "The value of enum Enum1 is not supported"), + }; + } + } +} \ No newline at end of file