Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,18 @@ public static void BuildInitMemberMappings(
// set the member mapped as it is an init only member
// diagnostics are already reported
// and no further mapping attempts should be undertaken
ctx.BuilderContext.ReportDiagnostic(
targetMember.IsRequired ? DiagnosticDescriptors.RequiredMemberNotMapped : DiagnosticDescriptors.SourceMemberNotFound,
targetMember.Name,
ctx.Mapping.TargetType,
ctx.Mapping.SourceType
);
if (
targetMember.IsRequired
|| ctx.BuilderContext.Configuration.Members.RequiredMappingStrategy.HasFlag(RequiredMappingStrategy.Target)
)
{
ctx.BuilderContext.ReportDiagnostic(
targetMember.IsRequired ? DiagnosticDescriptors.RequiredMemberNotMapped : DiagnosticDescriptors.SourceMemberNotFound,
targetMember.Name,
ctx.Mapping.TargetType,
ctx.Mapping.SourceType
);
}
ctx.SetTargetMemberMapped(targetMember);
}
}
Expand Down
32 changes: 32 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectPropertyInitPropertyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,36 @@ public record B : BBase
"""
);
}

[Fact]
public void InitOnlyPropertyWithSourceRequiredMappingShouldOnlyDiagnosticForRequired()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
[MapperRequiredMapping(RequiredMappingStrategy.Source)]
partial B Map(A source);
""",
"""
public record A(int Value);
public class B { public int Value { get; init; } public required int Value2 { get; init; } public int Value3 { get; init; } }
"""
);
TestHelper
.GenerateMapper(source, TestHelperOptions.AllowDiagnostics)
.Should()
.HaveMapMethodBody(
"""
var target = new global::B()
{
Value = source.Value,
};
return target;
"""
)
.HaveDiagnostic(
DiagnosticDescriptors.RequiredMemberNotMapped,
"Required member Value2 on mapping target type B was not found on the mapping source type A"
)
.HaveAssertedAllDiagnostics();
}
}
Loading