Closed DdarkSideE closed 7 months ago
I am not sure if it is useful, but based on the following provided code, Mapperly generates generic map better. Not sure what is the difference.
Declaration code
namespace A1
{
public class Class
{
public Enum Value { get; set; }
}
public enum Enum
{
Unknown = 0,
}
}
namespace A2
{
public enum Enum
{
Unknown = 0,
}
}
[Mapper]
partial class Mapper
{
public partial TTarget Map<TTarget>(Object source);
private A2.Enum Map(A1.Class source) => MapEnums(source.Value);
private partial A2.Enum MapEnums(A1.Enum source);
}
Generated code
#nullable enable
partial class Mapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.4.0.0")]
public partial TTarget? Map<TTarget>(object? source)
{
return source switch
{
global::A1.Enum x when typeof(TTarget).IsAssignableFrom(typeof(global::A2.Enum)) => (TTarget?)(object)MapEnums(x),
global::A1.Class x when typeof(TTarget).IsAssignableFrom(typeof(global::A2.Enum)) => (TTarget?)(object)Map(x),
null => throw new System.ArgumentNullException(nameof(source)),
_ => throw new System.ArgumentException($"Cannot map {source.GetType()} to {typeof(TTarget)} as there is no known type mapping", nameof(source)),
};
}
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.4.0.0")]
private partial global::A2.Enum MapEnums(global::A1.Enum source)
{
return (global::A2.Enum)source;
}
}
I cannot reproduce this... If I try it locally it generates
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.4.0.0")]
public static partial TTarget MapFruit<TTarget>(global::Fruit source)
{
return source switch
{
global::Banana x when typeof(TTarget).IsAssignableFrom(typeof(global::BananaDto)) => (TTarget)(object)MapBanana(x),
global::Apple x when typeof(TTarget).IsAssignableFrom(typeof(global::AppleDto)) => (TTarget)(object)MapApple(x),
null => throw new System.ArgumentNullException(nameof(source)),
_ => throw new System.ArgumentException($"Cannot map {source.GetType()} to {typeof(TTarget)} as there is no known type mapping", nameof(source)),
};
}
which seems to be ok to me. Tested with 3.4.0
and 3.5.0-next.3
.
Can you create a repro on github?
Could you check this, please? I tried it on another PC and it didn't work.
UPD. It seems like it doesn't work if nullable reference types disabled on a project level. When I turn it on, it generates normal code.
Any workarounds how to make it work with the option disabled?
This is a bug, thanks for reporting 😊
It looks like Mapperly doesn't handle disabled nullable reference types not correctly.
The only workaround I can think of currently is to enable the nullable reference types for the relevant parts (e.g. the mapper definition file) with #nullable enable
.
:tada: This issue has been resolved in version 3.5.0-next.4 :tada:
The release is available on:
v3.5.0-next.4
Your semantic-release bot :package::rocket:
Describe the bug Seems like generic mapping does not work. Even the example from the site won't work.
Declaration code
Actual relevant generated code
Expected relevant generated code It should use the MapBanana and MapApple methods to implement MapFruit. Now it basically generates an empty version of the method with no actual mapping.
Environment: