Skip to content

Commit

Permalink
analyze property getter and setters correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz committed Oct 24, 2024
1 parent e229bf2 commit 2ab9fe0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Riok.Mapperly/Symbols/Members/PropertyMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
31 changes: 31 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
"""
);
}
}

0 comments on commit 2ab9fe0

Please sign in to comment.