-
-
Notifications
You must be signed in to change notification settings - Fork 155
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
Improve null handling #1614
Comments
I think that when doing this task, you should also take into account similar conversions from #1117:
Also pay attention to the results of these two tests: mapperly/test/Riok.Mapperly.Tests/Mapping/StaticMethodConversionTest.cs Lines 135 to 146 in 39376fa
|
Can null value assignments be configured separately for each property? [AllowNullPropertyAssignment(false, Include = ["ArrayProp1", "ListProp2"])]
public static partial void UpdateEntity(this UpdateDto dto, MyEntity entity); This way, null values can be used in the collection navigation properties to indicate that the collection navigation properties are not modified when update entity using EF Core. |
This is not yet possible. |
Greetings. I have a related use case. public class SourceItem
{
public required string Name { get; set; }
}
public class DestinationItem
{
public required string Name { get; set; }
}
public class Source
{
public SourceItem[]? Items { get; set; }
}
public class Destination
{
public required DestinationItem[] Items { get; set; }
}
[Mapper(ThrowOnPropertyMappingNullMismatch = false, ThrowOnMappingNullMismatch = false)]
public partial class TestMapper
{
public partial Destination Map(Source source);
} It generates the following method [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.0.0")]
public partial global::<Api.Destination Map(global::Api.Mapping.Source source)
{
var target = new global::Api.Mapping.Destination()
{
Items = source.Items != null ? MapToDestinationItemArray(source.Items) : throw new System.ArgumentNullException(nameof(source.Items)),
};
return target;
} BUT, at the same time, it emits a compilation error: I guess the best solution to just try to use a empty collection initalizer? Items = source.Items != null ? MapToDestinationItemArray(source.Items) : [] Currently I can workaround this problem by manually adding the following code: public partial DestinationItem Map(SourceItem sourceItem);
public DestinationItem[] Map(SourceItem[]? sourceItems)
{
return sourceItems?.Select(this.Map).ToArray() ?? [];
} Thanks for considering. |
Currently, Mapperly upgrades non-nullable reference types to annotated nullable reference types early in the mapping pipeline. Additionally, when nullables are involved, Mapperly incorporates null handling into the mapping process and continues the pipeline with non-nullable types. This approach centralizes null handling, making it simple and consistent.
However, this design has led to several bugs, indicating the need for a more robust solution. We need to define a clear concept to address these issues and improve the handling of nullable reference types without compromising simplicity or maintainability.
Related Bugs:
See also #1117 (comment).
The text was updated successfully, but these errors were encountered: