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

Skip to content

MapValueAttribute does not support mapping non-nullable constant values, to nullable properties of the same type #1534

@IanKemp

Description

@IanKemp

Describe the bug
The following

enum TargetEnum
{
    Bar,
}

class Source
{
    string Quux { get; set; }
}

class Target
{
    string Quux { get; set; }
    TargetEnum? TheTarget { get; set; }
}

[MapValue(nameof(Target.TheTarget), TargetEnum.Bar))
static partial Target Map(this Source source);

incorrectly yields

RMG077: Cannot assign constant value TargetEnum.Bar of type TargetEnum to Target.TheTarget of type TargetEnum?

but should result in no error and valid generated code, because a non-null value of a type is always assignable to a nullable value of the same type.

The below cannot be used as a workaround, as typecasts are not permitted in attributes.

[MapValue(nameof(Target.TheTarget), (TargetEnum?)TargetEnum.Bar))

The below is a valid workaround, but undesirable.

[MapValue(nameof(Target.TheTarget), Use = nameof(GetNullableTargetEnumValue)))
static partial Target Map(this Source source);

static TargetEnum? GetNullableTargetEnumValue() => TargetEnum.Bar;

Environment (please complete the following information):

  • Mapperly Version: 4.0.0
  • Nullable reference types: enabled
  • .NET Version: 8.0.403
  • Target Framework: .net8.0
  • Compiler Version: 4.11.0-3.24460.3 (5649376e)
  • C# Language Version: 12.0
  • IDE: VS2022.17.11.5
  • OS: Windows 11 Pro 23H2 (22631.4169)

Additional context
#631

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions