From 44ac9ff022ab9546aaced6fde4bf49a2cb6a7316 Mon Sep 17 00:00:00 2001 From: Jan Trejbal Date: Mon, 13 Nov 2023 20:10:55 +0100 Subject: [PATCH] Add MixedStaticPartialMethodWithNonStaticFactory --- .../ObjectFactories/ObjectFactoryBuilder.cs | 1 + .../SimpleMappingBuilderContext.cs | 2 ++ ...InstantiableMapperWithStaticMethodsTest.cs | 28 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/Riok.Mapperly/Descriptors/ObjectFactories/ObjectFactoryBuilder.cs b/src/Riok.Mapperly/Descriptors/ObjectFactories/ObjectFactoryBuilder.cs index 2f6bd4b8d3d..c526853b7a4 100644 --- a/src/Riok.Mapperly/Descriptors/ObjectFactories/ObjectFactoryBuilder.cs +++ b/src/Riok.Mapperly/Descriptors/ObjectFactories/ObjectFactoryBuilder.cs @@ -28,6 +28,7 @@ public static ObjectFactoryCollection ExtractObjectFactories(SimpleMappingBuilde || methodSymbol.IsPartialDefinition || methodSymbol.MethodKind != MethodKind.Ordinary || methodSymbol.ReturnsVoid + || (methodSymbol.IsStatic is false && ctx.Static) ) { ctx.ReportDiagnostic(DiagnosticDescriptors.InvalidObjectFactorySignature, methodSymbol, methodSymbol.Name); diff --git a/src/Riok.Mapperly/Descriptors/SimpleMappingBuilderContext.cs b/src/Riok.Mapperly/Descriptors/SimpleMappingBuilderContext.cs index c00de0464f5..71e0cc7c902 100644 --- a/src/Riok.Mapperly/Descriptors/SimpleMappingBuilderContext.cs +++ b/src/Riok.Mapperly/Descriptors/SimpleMappingBuilderContext.cs @@ -58,6 +58,8 @@ protected SimpleMappingBuilderContext(SimpleMappingBuilderContext ctx) public WellKnownTypes Types => _compilationContext.Types; + public bool Static => _descriptor.Static; + public SymbolAccessor SymbolAccessor { get; } public AttributeDataAccessor AttributeAccessor { get; } diff --git a/test/Riok.Mapperly.Tests/Mapping/InstantiableMapperWithStaticMethodsTest.cs b/test/Riok.Mapperly.Tests/Mapping/InstantiableMapperWithStaticMethodsTest.cs index fcf5a2f9ced..b4101c2e8ab 100644 --- a/test/Riok.Mapperly.Tests/Mapping/InstantiableMapperWithStaticMethodsTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/InstantiableMapperWithStaticMethodsTest.cs @@ -153,6 +153,34 @@ public partial class Mapper .HaveAssertedAllDiagnostics(); } + [Fact] + public void MixedStaticPartialMethodWithNonStaticFactory() + { + var source = TestSourceBuilder.CSharp( + """ + using Riok.Mapperly.Abstractions; + + [Mapper] + public partial class CarMapper + { + [ObjectFactory] + B CreateB() => new B(); + + public static partial B Map(A a); + } + + class A { public string StringValue { get; set; } } + class B { public string StringValue { get; set; } } + """ + ); + + TestHelper + .GenerateMapper(source, TestHelperOptions.AllowAllDiagnostics) + .Should() + .HaveDiagnostic(DiagnosticDescriptors.InvalidObjectFactorySignature) + .HaveAssertedAllDiagnostics(); + } + [Fact] public void MixedStaticMethodWithInstanceMethod() {