From 60d1e70cc33af610b20732f1fc27616faac0af75 Mon Sep 17 00:00:00 2001 From: latonz Date: Mon, 16 Nov 2026 20:47:36 +0100 Subject: [PATCH] feat: prefer nullable user mappings for nullable parents --- .../MemberMappingBuilder.cs | 34 ++++- .../Descriptors/MappingBuilderContext.cs | 6 + .../MappingBuilders/MappingBuilder.cs | 3 + .../Descriptors/MappingCollection.cs | 30 +++-- .../Descriptors/ParameterScope.cs | 5 + .../UnsafeAccess/UnsafeAccessorTypeContext.cs | 25 ++++ .../UnsafeAccess/UnsafeFieldAccessor.cs | 7 +- .../UnsafeAccess/UnsafeGetPropertyAccessor.cs | 7 +- .../Mapping/ObjectPropertyNullableTest.cs | 69 ++++++++++ .../ObjectPropertyUseNamedMappingTest.cs | 37 +++++ .../Mapping/UnsafeAccessorTest.cs | 60 +++++++++ .../Mapping/UseStaticMapperTest.cs | 127 ++++++++++++++++++ ...leFieldInGenericClass#Mapper.g.verified.cs | 2 +- ...ropertyInGenericClass#Mapper.g.verified.cs | 2 +- 14 files changed, 396 insertions(+), 18 deletions(-) diff --git a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/MemberMappingBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/MemberMappingBuilder.cs index 78dfa24125..8423ef64f9 100644 --- a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/MemberMappingBuilder.cs +++ b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/MemberMappingBuilder.cs @@ -66,11 +66,7 @@ public static bool TryBuild( [NotNullWhen(true)] out SourceValueBuilder.MappedValue? result ) { - var mappingKey = memberMappingInfo.ToTypeMappingKey(); - var delegateMapping = ctx.BuilderContext.FindOrBuildLooseNullableMapping( - mappingKey, - diagnosticLocation: memberMappingInfo.Configuration?.Location - ); + var delegateMapping = FindDelegateMapping(ctx.BuilderContext, memberMappingInfo); var sourceMember = memberMappingInfo.SourceMember!; var targetMember = memberMappingInfo.TargetMember; @@ -147,6 +143,34 @@ public static bool TryBuild( return true; } + private static INewInstanceMapping? FindDelegateMapping(MappingBuilderContext ctx, MemberMappingInfo memberMappingInfo) + { + var mappingKey = memberMappingInfo.ToTypeMappingKey(); + + // Null-conditional access lifts a value type leaf to Nullable when a parent is nullable. + // Only prefer an existing user implementation; otherwise retain the usual mapping and null handling. + // Expression trees cannot use the null-conditional access emitted for these mappings. + if ( + !ctx.IsExpression + && memberMappingInfo.IsSourceNullable + && mappingKey.Source.IsValueType + && !mappingKey.Source.IsRefLikeType + && !mappingKey.Source.IsNullableValueType() + && mappingKey.Configuration.UseNamedMapping == null + ) + { + var nullableSource = ctx + .Types.Get(typeof(Nullable<>)) + .Construct(mappingKey.Source.NonNullable()) + .WithNullableAnnotation(NullableAnnotation.Annotated); + var nullableKey = new TypeMappingKey(nullableSource, mappingKey.Target, mappingKey.Configuration); + if (ctx.FindUserImplementedMapping(nullableKey) is { } mapping) + return mapping; + } + + return ctx.FindOrBuildLooseNullableMapping(mappingKey, diagnosticLocation: memberMappingInfo.Configuration?.Location); + } + private static MemberPathGetter? GetTargetValueGetterIfNeeded( IMembersBuilderContext ctx, INewInstanceMapping delegateMapping, diff --git a/src/Riok.Mapperly/Descriptors/MappingBuilderContext.cs b/src/Riok.Mapperly/Descriptors/MappingBuilderContext.cs index 2fed34cdf4..c6a9f5dcda 100644 --- a/src/Riok.Mapperly/Descriptors/MappingBuilderContext.cs +++ b/src/Riok.Mapperly/Descriptors/MappingBuilderContext.cs @@ -153,6 +153,12 @@ private static ParameterScope BuildParameterScope(IUserMapping? userMapping) => public virtual INewInstanceMapping? FindMapping(TypeMappingKey mappingKey, ParameterScope? scope = null) => MappingBuilder.Find(mappingKey, scope); + /// + /// Finds a user-implemented mapping without marking rejected mappings or their additional parameters as used. + /// + public UserImplementedMethodMapping? FindUserImplementedMapping(TypeMappingKey mappingKey) => + MappingBuilder.FindUserImplemented(mappingKey, ParameterScope); + /// /// Tries to find an existing mapping for the provided types. /// If none is found, a new one is created. diff --git a/src/Riok.Mapperly/Descriptors/MappingBuilders/MappingBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBuilders/MappingBuilder.cs index e4320dde35..fc7789187b 100644 --- a/src/Riok.Mapperly/Descriptors/MappingBuilders/MappingBuilder.cs +++ b/src/Riok.Mapperly/Descriptors/MappingBuilders/MappingBuilder.cs @@ -45,6 +45,9 @@ public class MappingBuilder(MappingCollection mappings, MapperDeclaration mapper public INewInstanceMapping? Find(TypeMappingKey mapping, ParameterScope? scope = null) => mappings.FindNewInstanceMapping(mapping, scope); + public UserImplementedMethodMapping? FindUserImplemented(TypeMappingKey mapping, ParameterScope? scope = null) => + mappings.FindNewInstanceUserImplementedMapping(mapping, scope); + public INewInstanceMapping? FindOrResolveNamed(SimpleMappingBuilderContext ctx, string name, out bool ambiguousName) { if (!ctx.Configuration.Mapper.AutoUserMappings && _resolvedMappingNames.Add(name)) diff --git a/src/Riok.Mapperly/Descriptors/MappingCollection.cs b/src/Riok.Mapperly/Descriptors/MappingCollection.cs index ec18274a0f..52796f95b9 100644 --- a/src/Riok.Mapperly/Descriptors/MappingCollection.cs +++ b/src/Riok.Mapperly/Descriptors/MappingCollection.cs @@ -91,6 +91,19 @@ public class MappingCollection(GenericTypeChecker genericTypeChecker) public INewInstanceUserMapping? FindNewInstanceUserMapping(IMethodSymbol method) => _newInstanceMappings.FindUserMapping(method); + public UserImplementedMethodMapping? FindNewInstanceUserImplementedMapping(TypeMappingKey mappingKey, ParameterScope? scope = null) + { + if ( + _newInstanceMappings.Find(mappingKey, scope, static m => m is UserImplementedMethodMapping) + is UserImplementedMethodMapping mapping + ) + { + return mapping; + } + + return mappingKey.Configuration.UseNamedMapping == null ? TryMatchGenericNewInstanceTemplate(mappingKey) : null; + } + public INewInstanceMapping? FindNamedNewInstanceMapping(string name, out bool ambiguousName) => _newInstanceMappings.FindNamed(name, out ambiguousName); @@ -206,7 +219,7 @@ public void AddNamedExistingInstanceUserMapping(string name, IExistingTargetUser _existingTargetMappings.AddNamedUserMapping(name, mapping); } - private INewInstanceMapping? TryMatchGenericNewInstanceTemplate(TypeMappingKey mappingKey) + private UserImplementedGenericMethodMapping? TryMatchGenericNewInstanceTemplate(TypeMappingKey mappingKey) { if (_genericNewInstanceTemplates.Count == 0) return null; @@ -313,19 +326,18 @@ private sealed class MappingCollectionInstance public TUserMapping? FindUserMapping(IMethodSymbol method) => _userMappingsByMethod.GetValueOrDefault(method); - public T? Find(TypeMappingKey mappingKey, ParameterScope? scope = null) + public T? Find(TypeMappingKey mappingKey, ParameterScope? scope = null, Func? filter = null) { - if (scope is { IsEmpty: false } && FindByParameters(mappingKey, scope) is { } parameterizedMapping) + if (scope is { IsEmpty: false } && FindByParameters(mappingKey, scope, filter) is { } parameterizedMapping) { _usedMappingKeys.Add(mappingKey); return parameterizedMapping; } - if (_defaultMappings.TryGetValue(mappingKey, out var mapping)) - { - _usedMappingKeys.Add(mappingKey); - } + if (!_defaultMappings.TryGetValue(mappingKey, out var mapping) || filter?.Invoke(mapping) == false) + return default; + _usedMappingKeys.Add(mappingKey); return mapping; } @@ -341,13 +353,13 @@ private sealed class MappingCollectionInstance return mapping; } - private TUserMapping? FindByParameters(TypeMappingKey key, ParameterScope scope) + private TUserMapping? FindByParameters(TypeMappingKey key, ParameterScope scope, Func? filter) { var candidates = _userMappings.GetOrEmpty(key).Where(m => m is IParameterizedMapping { AdditionalSourceParameters.Count: > 0 }); foreach (var mapping in candidates) { - if (scope.TryUseParameters(mapping)) + if (filter?.Invoke(mapping) != false && scope.TryUseParameters(mapping)) return mapping; } diff --git a/src/Riok.Mapperly/Descriptors/ParameterScope.cs b/src/Riok.Mapperly/Descriptors/ParameterScope.cs index ef187bb083..a8c016036b 100644 --- a/src/Riok.Mapperly/Descriptors/ParameterScope.cs +++ b/src/Riok.Mapperly/Descriptors/ParameterScope.cs @@ -4,6 +4,11 @@ namespace Riok.Mapperly.Descriptors; +/// +/// Manages additional source parameters available to mappings and tracks their usage. +/// Child scopes share their parent's parameters and delegate usage tracking to the root scope +/// for unused-parameter diagnostics. +/// public class ParameterScope { private static readonly IReadOnlyDictionary _emptyParameters = new Dictionary(); diff --git a/src/Riok.Mapperly/Descriptors/UnsafeAccess/UnsafeAccessorTypeContext.cs b/src/Riok.Mapperly/Descriptors/UnsafeAccess/UnsafeAccessorTypeContext.cs index 5afeea4375..643f71510c 100644 --- a/src/Riok.Mapperly/Descriptors/UnsafeAccess/UnsafeAccessorTypeContext.cs +++ b/src/Riok.Mapperly/Descriptors/UnsafeAccess/UnsafeAccessorTypeContext.cs @@ -55,6 +55,31 @@ public UnsafeConstructorAccessor GetOrBuildConstructor(IMethodSymbol ctorSymbol) ); } + internal static ExpressionSyntax BuildNullConditionalAccess( + ISymbol member, + INamedTypeSymbol containingType, + ExpressionSyntax receiver, + ExpressionSyntax access + ) + { + // Accessors are cached by the original definition, so resolve the member type + // on the actual containing type before lifting value types to Nullable. + var constructedMember = containingType + .GetMembers(member.Name) + .First(m => SymbolEqualityComparer.Default.Equals(m.OriginalDefinition, member.OriginalDefinition)); + var memberType = constructedMember switch + { + IPropertySymbol property => property.Type, + IFieldSymbol field => field.Type, + _ => throw new ArgumentException("Only properties and fields support null-conditional unsafe access", nameof(member)), + }; + ExpressionSyntax nullValue = + memberType.IsValueType && !memberType.IsNullableValueType() && !memberType.IsRefLikeType + ? CastExpression(NullableType(NonNullableIdentifier(memberType)), NullLiteral()) + : DefaultLiteral(); + return Conditional(IsNotNull(receiver, containingType), access, nullValue); + } + internal MemberDeclarationSyntax BuildSyntax(SourceEmitterContext ctx, CancellationToken cancellationToken) { var accessorCtx = ctx.AddIndentation(); diff --git a/src/Riok.Mapperly/Descriptors/UnsafeAccess/UnsafeFieldAccessor.cs b/src/Riok.Mapperly/Descriptors/UnsafeAccess/UnsafeFieldAccessor.cs index d8ac2b3990..e8be6f8f20 100644 --- a/src/Riok.Mapperly/Descriptors/UnsafeAccess/UnsafeFieldAccessor.cs +++ b/src/Riok.Mapperly/Descriptors/UnsafeAccess/UnsafeFieldAccessor.cs @@ -64,7 +64,12 @@ public ExpressionSyntax BuildAccess(ExpressionSyntax? baseAccess, INamedTypeSymb if (!nullConditional) return invocation; - return Conditional(IsNotNull(baseAccess, containingType ?? symbol.ContainingType), invocation, DefaultLiteral()); + return UnsafeAccessorTypeContext.BuildNullConditionalAccess( + symbol, + containingType ?? symbol.ContainingType, + baseAccess, + invocation + ); } public ExpressionSyntax BuildAssignment( diff --git a/src/Riok.Mapperly/Descriptors/UnsafeAccess/UnsafeGetPropertyAccessor.cs b/src/Riok.Mapperly/Descriptors/UnsafeAccess/UnsafeGetPropertyAccessor.cs index 8561124089..24db46ba36 100644 --- a/src/Riok.Mapperly/Descriptors/UnsafeAccess/UnsafeGetPropertyAccessor.cs +++ b/src/Riok.Mapperly/Descriptors/UnsafeAccess/UnsafeGetPropertyAccessor.cs @@ -69,6 +69,11 @@ public ExpressionSyntax BuildAccess(ExpressionSyntax? baseAccess, INamedTypeSymb if (!nullConditional) return invocation; - return Conditional(IsNotNull(baseAccess, containingType ?? symbol.ContainingType), invocation, DefaultLiteral()); + return UnsafeAccessorTypeContext.BuildNullConditionalAccess( + symbol, + containingType ?? symbol.ContainingType, + baseAccess, + invocation + ); } } diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyNullableTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyNullableTest.cs index d862421371..c10d6c01b3 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyNullableTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyNullableTest.cs @@ -1603,4 +1603,73 @@ class B """ ); } + + [Fact] + public void NullableParentShouldPreferNullableUserMapping() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + """ + [MapProperty("NullableItem.Value", "NullableValue")] + [MapProperty("Item.Value", "Value")] + partial B Map(A source); + int MapNullable(int? source) => source ?? -1; + int MapNonNullable(int source) => source + 1; + """, + "record A(C? NullableItem, C Item);", + "record C(int Value);", + "class B { public int NullableValue { get; init; } public int Value { get; init; } }" + ); + + TestHelper + .GenerateMapper(source) + .Should() + .HaveMapMethodBody( + """ + var target = new global::B() + { + NullableValue = MapNullable(source.NullableItem?.Value), + Value = MapNonNullable(source.Item.Value), + }; + return target; + """ + ); + } + + [Fact] + public void NullableParentWithGeneratedNullableMappingsShouldKeepNullFallback() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + """ + [MapProperty("Item.Value", "Value")] + partial B Map(A source); + private partial int MapNullable(int? source); + private partial int OtherNullable(int? source); + """, + TestSourceBuilderOptions.Default with + { + ThrowOnMappingNullMismatch = true, + }, + "record A(C? Item);", + "record C(int Value);", + "record B(int Value);" + ); + + TestHelper + .GenerateMapper(source, TestHelperOptions.AllowDiagnostics) + .Should() + .HaveDiagnostic( + DiagnosticDescriptors.NullableSourceValueToNonNullableTargetValue, + "Mapping the nullable source property Item.Value of A to the target property Value of B which is not nullable" + ) + .HaveDiagnostic(DiagnosticDescriptors.NullableSourceTypeToNonNullableTargetType) + .HaveAssertedAllDiagnostics() + .HaveMapMethodBody( + """ + var target = new global::B( + source.Item?.Value ?? throw new global::System.ArgumentNullException(nameof(source.Item.Value)) + ); + return target; + """ + ); + } } diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyUseNamedMappingTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyUseNamedMappingTest.cs index 64bc379e49..03794d592d 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyUseNamedMappingTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyUseNamedMappingTest.cs @@ -945,4 +945,41 @@ class B """ ); } + + [Fact] + public void NullableParentShouldKeepExplicitMapping() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + """ + [MapProperty("Item.Value", "Value", Use = nameof(MapNonNullable))] + partial B Map(A source); + int MapNullable(int? source) => source ?? -1; + int MapNonNullable(int source) => source + 1; + """, + TestSourceBuilderOptions.Default with + { + ThrowOnMappingNullMismatch = true, + }, + "record A(C? Item);", + "record C(int Value);", + "record B(int Value);" + ); + + TestHelper + .GenerateMapper(source, TestHelperOptions.AllowDiagnostics) + .Should() + .HaveDiagnostic( + DiagnosticDescriptors.NullableSourceValueToNonNullableTargetValue, + "Mapping the nullable source property Item.Value of A to the target property Value of B which is not nullable" + ) + .HaveAssertedAllDiagnostics() + .HaveMapMethodBody( + """ + var target = new global::B( + source.Item != null ? MapNonNullable(source.Item.Value) : throw new global::System.ArgumentNullException(nameof(source.Item.Value)) + ); + return target; + """ + ); + } } diff --git a/test/Riok.Mapperly.Tests/Mapping/UnsafeAccessorTest.cs b/test/Riok.Mapperly.Tests/Mapping/UnsafeAccessorTest.cs index bd80a2ae16..52d0657895 100644 --- a/test/Riok.Mapperly.Tests/Mapping/UnsafeAccessorTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/UnsafeAccessorTest.cs @@ -826,4 +826,64 @@ public partial class CarMapper return TestHelper.VerifyGenerator(source); } + + [Fact] + public void NullableGenericParentWithInheritedPrivatePropertyShouldPassNullToUserMapping() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + """ + [MapProperty("Item.Value", "Value")] + partial B Map(A source); + int MapNullable(int? source) => source ?? -1; + """, + TestSourceBuilderOptions.WithMemberVisibility(MemberVisibility.All), + "class A { public D? Item { get; set; } }", + "class B { public int Value { get; set; } }", + "class C { private T Value { get; set; } }", + "class D : C { }" + ); + + TestHelper + .GenerateMapper(source) + .Should() + .HaveMapMethodBody( + """ + var target = new global::B(); + target.Value = MapNullable( + source.Item != null ? CAccessor.GetValue(source.Item) : (int?)null + ); + return target; + """ + ); + } + + [Fact] + public void NullableGenericParentWithInheritedPrivateFieldShouldPassNullToUserMapping() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + """ + [MapProperty("Item.Value", "Value")] + partial B Map(A source); + int MapNullable(int? source) => source ?? -1; + """, + TestSourceBuilderOptions.WithMemberVisibility(MemberVisibility.All), + "class A { public D? Item { get; set; } }", + "class B { public int Value { get; set; } }", + "class C { private T Value; }", + "class D : C { }" + ); + + TestHelper + .GenerateMapper(source) + .Should() + .HaveMapMethodBody( + """ + var target = new global::B(); + target.Value = MapNullable( + source.Item != null ? CAccessor.GetValue(source.Item) : (int?)null + ); + return target; + """ + ); + } } diff --git a/test/Riok.Mapperly.Tests/Mapping/UseStaticMapperTest.cs b/test/Riok.Mapperly.Tests/Mapping/UseStaticMapperTest.cs index 9f0d36ee70..cf2efb9315 100644 --- a/test/Riok.Mapperly.Tests/Mapping/UseStaticMapperTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/UseStaticMapperTest.cs @@ -1087,4 +1087,131 @@ public static partial class Mapper """ ); } + + [Fact] + public void NullableStructParentShouldUseGenericNullableConverter() + { + var source = TestSourceBuilder.CSharp( + """ + using Riok.Mapperly.Abstractions; + + record Source(SourceItem? Item); + record struct SourceItem(int FirstInt, int SecondInt, long LongValue); + record Target(int FirstInt, int SecondInt, long LongValue); + + [Mapper(ThrowOnPropertyMappingNullMismatch = true, ThrowOnMappingNullMismatch = true)] + [UseStaticMapper(typeof(WhenNull))] + partial class Mapper + { + [MapProperty("Item.FirstInt", "FirstInt")] + [MapProperty("Item.SecondInt", "SecondInt")] + [MapProperty("Item.LongValue", "LongValue")] + public partial Target Map(Source source); + } + + static class WhenNull + { + public static T Convert(T? value) where T : struct => value ?? default; + } + """ + ); + + TestHelper + .GenerateMapper(source) + .Should() + .HaveMapMethodBody( + """ + var target = new global::Target( + global::WhenNull.Convert(source.Item?.FirstInt), + global::WhenNull.Convert(source.Item?.SecondInt), + global::WhenNull.Convert(source.Item?.LongValue) + ); + return target; + """ + ); + } + + [Fact] + public void NullableParentProjectionShouldKeepNullHandling() + { + var source = TestSourceBuilder.CSharp( + """ + using System.Linq; + using Riok.Mapperly.Abstractions; + + record A(C? Item); + record C(int Value); + record B(int ItemValue); + + [Mapper(QueryableProjectionNullHandling = QueryableProjectionNullHandling.NullSafe)] + [UseStaticMapper(typeof(WhenNull))] + partial class Mapper + { + public partial IQueryable Map(IQueryable source); + } + + static class WhenNull + { + public static int Convert(int? value) => value ?? -1; + } + """ + ); + + TestHelper + .GenerateMapper(source, TestHelperOptions.AllowInfoDiagnostics) + .Should() + .HaveDiagnostic(DiagnosticDescriptors.NullableSourceValueToNonNullableTargetValue) + .HaveAssertedAllDiagnostics() + .HaveMapMethodBody( + """ + #nullable disable + return global::System.Linq.Queryable.Select(source, x => new global::B(x.Item != null ? x.Item.Value : default)); + #nullable enable + """ + ); + } + + [Fact] + public void NullableParentShouldUseNullableConverterForPropertiesWithNoNullAssignment() + { + var source = TestSourceBuilder.CSharp( + """ + using Riok.Mapperly.Abstractions; + + record SourceItem(int? SourceNumber, int SourceId); + record Source(SourceItem? Item); + class Target + { + public int TargetNumber { get; set; } + public int TargetId { get; set; } + } + + [Mapper(AllowNullPropertyAssignment = false, ThrowOnPropertyMappingNullMismatch = true)] + [UseStaticMapper] + partial class Mapper + { + [MapProperty("Item.SourceNumber", "TargetNumber")] + [MapProperty("Item.SourceId", "TargetId")] + public partial Target Map(Source source); + } + + class WhenNull + { + public static int Convert(int? value) => value ?? 0; + } + """ + ); + + TestHelper + .GenerateMapper(source) + .Should() + .HaveMapMethodBody( + """ + var target = new global::Target(); + target.TargetNumber = global::WhenNull.Convert(source.Item?.SourceNumber); + target.TargetId = global::WhenNull.Convert(source.Item?.SourceId); + return target; + """ + ); + } } diff --git a/test/Riok.Mapperly.Tests/_snapshots/UnsafeAccessorTest.PrivateNestedNullableFieldInGenericClass#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/UnsafeAccessorTest.PrivateNestedNullableFieldInGenericClass#Mapper.g.verified.cs index e914fc5c66..c9bbe162e2 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/UnsafeAccessorTest.PrivateNestedNullableFieldInGenericClass#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/UnsafeAccessorTest.PrivateNestedNullableFieldInGenericClass#Mapper.g.verified.cs @@ -7,7 +7,7 @@ public partial class Mapper partial global::B Map(global::A source) { var target = new global::B(); - BAccessor.GetValue(target) = AAccessor.GetNested(source) != null ? CAccessor.GetValue(AAccessor.GetNested(source)) : default; + BAccessor.GetValue(target) = AAccessor.GetNested(source) != null ? CAccessor.GetValue(AAccessor.GetNested(source)) : (int?)null; return target; } } diff --git a/test/Riok.Mapperly.Tests/_snapshots/UnsafeAccessorTest.PrivateNestedNullablePropertyInGenericClass#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/UnsafeAccessorTest.PrivateNestedNullablePropertyInGenericClass#Mapper.g.verified.cs index 7eae393f7c..c5b558ec37 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/UnsafeAccessorTest.PrivateNestedNullablePropertyInGenericClass#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/UnsafeAccessorTest.PrivateNestedNullablePropertyInGenericClass#Mapper.g.verified.cs @@ -7,7 +7,7 @@ public partial class Mapper partial global::B Map(global::A source) { var target = new global::B(); - BAccessor.SetValue(target, AAccessor.GetNested(source) != null ? CAccessor.GetValue(AAccessor.GetNested(source)) : default); + BAccessor.SetValue(target, AAccessor.GetNested(source) != null ? CAccessor.GetValue(AAccessor.GetNested(source)) : (int?)null); return target; } }