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

Fix inconsistent handling of mixed nullable contexts #1558

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Riok.Mapperly/Descriptors/MappingBuilderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ bool ignoreDerivedTypes
Location? diagnosticLocation = null
)
{
return FindMapping(mappingKey) ?? BuildMapping(mappingKey, options, diagnosticLocation);
return FindMapping(mappingKey)
?? FindMapping(mappingKey.TargetNonNullable())
?? BuildMapping(mappingKey, options, diagnosticLocation);
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Riok.Mapperly/Descriptors/TypeMappingKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public TypeMappingKey(ITypeMapping mapping, TypeMappingConfiguration? config = n

public TypeMappingKey NonNullable() => new(Source.NonNullable(), Target.NonNullable(), Configuration);

public TypeMappingKey TargetNonNullable() => new(Source, Target.NonNullable(), Configuration);

public override bool Equals(object? obj) =>
obj is TypeMappingKey other
&& _comparer.Equals(Source, other.Source)
Expand Down
29 changes: 29 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectPropertyNullableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -899,4 +899,33 @@ private A(C value)
"""
);
}

[Fact]
public Task MixedNullableContextsWithDerivedTypesShouldWork()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
[MapDerivedType<A, B>]
public partial BBase Map(ABase src);
""",
"""
#nullable disable
public abstract record BBase
{
public List<BBase> Objects { get; init; } = [];
}

public record B : BBase;

#nullable enable
public abstract record ABase
{
public List<ABase> Objects { get; init; } = [];
}

public record A: ABase;
"""
);
return TestHelper.VerifyGenerator(source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//HintName: Mapper.g.cs
// <auto-generated />
#nullable enable
public partial class Mapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
public partial global::BBase Map(global::ABase src)
{
return src switch
{
global::A x => MapToB(x),
_ => throw new System.ArgumentException($"Cannot map {src.GetType()} to BBase as there is no known derived type mapping", nameof(src)),
};
}

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
private global::B MapToB(global::A source)
{
var target = new global::B()
{
Objects = MapToListOfBBase(source.Objects),
};
return target;
}

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
private global::System.Collections.Generic.List<global::BBase?> MapToListOfBBase(global::System.Collections.Generic.IReadOnlyCollection<global::ABase> source)
{
var target = new global::System.Collections.Generic.List<global::BBase?>(source.Count);
foreach (var item in source)
{
target.Add(Map(item));
}
return target;
}
}
Loading