From 2ab9fe08ccd27acda2662e218d3ffd9d5be65dd3 Mon Sep 17 00:00:00 2001 From: latonz Date: Thu, 24 Oct 2024 23:00:59 +0200 Subject: [PATCH] analyze property getter and setters correctly --- .../Symbols/Members/PropertyMember.cs | 7 +++-- .../Mapping/ObjectPropertyInitPropertyTest.cs | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/Riok.Mapperly/Symbols/Members/PropertyMember.cs b/src/Riok.Mapperly/Symbols/Members/PropertyMember.cs index c902ece621..b7d10a3ad5 100644 --- a/src/Riok.Mapperly/Symbols/Members/PropertyMember.cs +++ b/src/Riok.Mapperly/Symbols/Members/PropertyMember.cs @@ -26,11 +26,12 @@ public class PropertyMember(IPropertySymbol symbol, SymbolAccessor symbolAccesso public bool CanGet => !Symbol.IsWriteOnly && (Symbol.GetMethod == null || symbolAccessor.IsMemberAccessible(Symbol.GetMethod)); public bool CanGetDirectly => - !Symbol.IsWriteOnly && (Symbol.GetMethod == null || symbolAccessor.IsDirectlyAccessible(Symbol.GetMethod)); + Symbol is { IsWriteOnly: false, GetMethod: not null } && symbolAccessor.IsDirectlyAccessible(Symbol.GetMethod); - public bool CanSet => !Symbol.IsReadOnly && (Symbol.SetMethod == null || symbolAccessor.IsMemberAccessible(Symbol.SetMethod)); + public bool CanSet => Symbol is { IsReadOnly: false, SetMethod: not null } && symbolAccessor.IsMemberAccessible(Symbol.SetMethod); - public bool CanSetDirectly => !Symbol.IsReadOnly && (Symbol.SetMethod == null || symbolAccessor.IsDirectlyAccessible(Symbol.SetMethod)); + public bool CanSetDirectly => + Symbol is { IsReadOnly: false, SetMethod: not null } && symbolAccessor.IsDirectlyAccessible(Symbol.SetMethod); public bool IsInitOnly => Symbol.SetMethod?.IsInitOnly == true; diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs index a8583a0dce..ccc3a49370 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs @@ -443,4 +443,35 @@ public void ClassInitOnlyPropertyWithStringFormat() """ ); } + + [Fact] + public void InheritedInitOnlyPropertyWithNoSetterOverrideShouldBeNotBeMapped() + { + var source = TestSourceBuilder.Mapping( + "A", + "B", + """ + public record A(Guid Value); + + public abstract record BBase + { + public virtual Guid Value { get; init; } + } + + public record B : BBase + { + public override Guid Value => Guid.Empty; + } + """ + ); + TestHelper + .GenerateMapper(source) + .Should() + .HaveMapMethodBody( + """ + var target = new global::B(); + return target; + """ + ); + } }