Question about RMG037 warning when source enum has fewer members than target enum #2243
|
Mapperly version: 4.3.1 ScenarioI have two enums:
CodeMy mapping configuration: [Mapper(EnumMappingStrategy = EnumMappingStrategy.ByName)]
internal static partial class MapExtensions
{
[MapperRequiredMapping(RequiredMappingStrategy.None)]
internal static partial Command Map(this MqDto dto);
}
public sealed class MqDto
{
public required CompanyTypeMq CompanyType { get; init; }
}
public enum CompanyTypeMq
{
CompanyA = 1,
CompanyB = 2,
}
public sealed class Command
{
public required CompanyType CompanyType { get; init; }
}
public enum CompanyType
{
CompanyA = 1,
CompanyB = 2,
CompanyC = 3,
}Generated codeMapperly generates the following mapping (which is actually correct for my needs): private static global::CompanyType MapToCompanyType(global::CompanyTypeMq source)
{
return source switch
{
global::CompanyTypeMq.CompanyA => global::CompanyType.CompanyA,
global::CompanyTypeMq.CompanyB => global::CompanyType.CompanyB,
_ => throw new global::System.ArgumentOutOfRangeException(nameof(source), source, "The value of enum CompanyTypeMq is not supported"),
};
}ProblemI get warning RMG037:
I understand why this warning exists, but in my case:
What I triedI tried different strategies with [MapperRequiredMapping]: [MapperRequiredMapping(RequiredMappingStrategy.None)]
[MapperRequiredMapping(RequiredMappingStrategy.Source)]
[MapperRequiredMapping(RequiredMappingStrategy.Target)]None of these suppress the RMG037 warning for the missing CompanyC. QuestionI think this might be a bug in how RMG037 works. In my case, the target enum (CompanyType) has an extra member CompanyC that does not exist in the source enum (CompanyTypeMq). However, this member is never expected to be mapped from the source — it's only used in other scenarios (e.g., direct API calls). This seems like a false positive. The warning should only appear when the mapping requires a member that doesn't exist, but here the mapping doesn't require CompanyC at all — it's perfectly fine for the target to have more members than the source. Is this by design or a bug? And is there any way to make Mapperly understand that extra target enum members are acceptable without suppressing the warning globally (since RMG037 could still be useful for other cases)? |
Replies: 1 comment
|
This looks like expected strict-enum behavior, not a generated-code bug. Mapperly's default required enum strategy checks both sides, so it warns that If extra target enum values are acceptable for this mapper, configure enum required mapping as source-only: [Mapper(
EnumMappingStrategy = EnumMappingStrategy.ByName,
RequiredEnumMappingStrategy = RequiredMappingStrategy.Source)]
internal static partial class MapExtensions
{
internal static partial Command Map(this MqDto dto);
}If you want to keep stricter enum checks globally, define an explicit enum mapping method and configure that method instead: [MapperRequiredMapping(RequiredMappingStrategy.Source)]
private static partial CompanyType Map(CompanyTypeMq source);The reason your |
This looks like expected strict-enum behavior, not a generated-code bug.
Mapperly's default required enum strategy checks both sides, so it warns that
CompanyType.CompanyCcannot be produced fromCompanyTypeMq. The generated switch is still correct for runtime values that can come from the source enum.If extra target enum values are acceptable for this mapper, configure enum required mapping as source-only:
If you want to keep stricter enum checks globally,…