Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
IanKemp opened this issue Oct 14, 2024 · 6 comments · Fixed by #1536 or #1538
Labels
bug Something isn't working

Comments

@IanKemp
Copy link
Contributor

IanKemp commented Oct 14, 2024

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

@IanKemp IanKemp added the bug Something isn't working label Oct 14, 2024
@latonz
Copy link
Contributor

latonz commented Oct 14, 2024

Thanks for reporting. Feel free to contribute 😉
Hint: Riok.Mapperly.Descriptors.MappingBodyBuilders.SourceValueBuilder.TryBuildConstantSourceValue

@latonz
Copy link
Contributor

latonz commented Oct 14, 2024

While reading the code I already fixed it: #1536

@latonz
Copy link
Contributor

latonz commented Oct 14, 2024

Fix released with 4.1.0-next.1, let me know if it works for you 😊

@IanKemp
Copy link
Contributor Author

IanKemp commented Oct 15, 2024

@latonz 4.1.0-next.1 fixes the case whereby

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

raised RMG077, which is good... but that version now causes RMG078 to be raised for the previously-working

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

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

which is incorrect.

@latonz
Copy link
Contributor

latonz commented Oct 15, 2024

@IanKemp My bad... #1538 should fix it definitely (hopefully).

@IanKemp
Copy link
Contributor Author

IanKemp commented Oct 16, 2024

@latonz thanks for your hard work - will wait for next .next version to drop and report back with verification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants