-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
Nullable error when target type comes from external reference #690
Comments
I can reproduce the issue, but I don't really understand it. ...
if (issueDto.Property1 != null)
{
target.Property1 = MapToSubTargetArray(issueDto.Property1);
}
...
private static global::dtoTest.SubTarget[] MapToSubTargetArray(global::NullableMapperlyTest.SubSource[] source)
... The compiler reports @TimothyMakkison what are your thoughts on this? |
Yeah my brain also crashed investigating what was going wrong. But the problem in our case is that ALL dto's are coming from a referenced project, and we treat all warnings as errors in our project, so this issue is preventing our code base to build. As far as I could find mapperly doesn't have an option to disable nullable context in the generated code does it? Because that would be a workaround for us for the time being |
@PaulVrugt nope Mapperly doesn't have such an option. The problem only exiss on arrays, correct? Sorry that you can't use Mapperly for now 🙄 |
The nullability being on the array element threw me + the difference between VS and rider. Perhaps in external references, arrays don't correctly annotate array element/generic types, so they all annotated with I'm strugglng to debug this. I can recreate the issue if I look at the generated code. Running it in debug mode generates the following code with no nullability issues 😭 public static partial global::Lib.ContainerDto MapTo(global::Riok.Mapperly.Sample.Container src)
{
var target = new global::Lib.ContainerDto();
target.Arrays = MapToElementDtoArray(src.Arrays);
return target;
}
private static global::Lib.ElementDto MapToElementDto(global::Riok.Mapperly.Sample.Element source)
{
var target = new global::Lib.ElementDto();
target.Value = source.Value;
return target;
}
private static global::Lib.ElementDto[] MapToElementDtoArray(global::Riok.Mapperly.Sample.Element[] source)
{
var target = new global::Lib.ElementDto[source.Length];
for (var i = 0; i < source.Length; i++)
{
target[i] = MapToElementDto(source[i]);
}
return target;
} |
I think I've fixed the issue. Looks like external array types treat the nullability of type arguments differently. Not sure if this is a roslyn bug 🤷 I realised that the same issue wouldn't occur if the external collection is a list, despite having similar code generated. // returns a `None` annotated generic type if type is an array
if (type.ImplementsGeneric(types.Get(typeof(IEnumerable<>)), out var enumerableIntf))
return enumerableIntf.TypeArguments[0];
// will have the correct nullability annotation
((IArrayTypeSymbol)type).ElementType;
I ended up creating a static |
Awesome! Hopefully this fixes it |
🎉 This issue has been resolved in version 3.2.0-next.2 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
🎉 This issue has been resolved in version 3.2.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Describe the bug
Nullable error when target type comes from external reference
To Reproduce
Steps to reproduce the behavior:
Download the sample project from #679
Expected behavior
No nullable erros
Code snippets
source:
and a mapper:
and the target, but this target is included via a references project:
Environment (please complete the following information):
Additional context
It only happens when the target class is in a references project
The text was updated successfully, but these errors were encountered: