Skip to content

Commit

Permalink
fix: prevent null initializing readonly members (#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison authored Aug 28, 2023
1 parent 71cbfdd commit f3d78b4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ private void AddNullMemberInitializers(IMemberAssignmentMappingContainer contain
{
var nullablePath = new MemberPath(nullableTrailPath);
var type = nullablePath.Member.Type;

if (!nullablePath.Member.CanSet)
continue;

if (!BuilderContext.SymbolAccessor.HasAccessibleParameterlessConstructor(type))
{
BuilderContext.ReportDiagnostic(DiagnosticDescriptors.NoParameterlessConstructorFound, type);
Expand Down
22 changes: 22 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectPropertyFlatteningTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,28 @@ public void ManualUnflattenedPropertyNullablePath()
);
}

[Fact]
public void ManualUnflattenedPropertyReadOnlyNullablePath()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"[MapProperty($\"MyValueId\", \"Value.Id\")] partial B Map(A source);",
"class A { public string MyValueId { get; set; } }",
"class B { public C? Value { get; } }",
"class C { public string Id { get; set; } public string Id2 { get; set; } }"
);

TestHelper
.GenerateMapper(source)
.Should()
.HaveSingleMethodBody(
"""
var target = new global::B();
target.Value.Id = source.MyValueId;
return target;
"""
);
}

[Fact]
public void ManualUnflattenedPropertyDeepNullablePath()
{
Expand Down

0 comments on commit f3d78b4

Please sign in to comment.