Skip to content

Commit

Permalink
fix: use correct argument types in user defined enumerable mappings w…
Browse files Browse the repository at this point in the history
…ith an array source parameter (#1125)

Do not generalize enumerable and dictionary source parameter types if there it is a direct user mapping (with a user symbol)
  • Loading branch information
latonz authored Feb 20, 2024
1 parent 8d66715 commit 8b14725
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Riok.Mapperly/Descriptors/MappingBuilderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ bool clearDerivedTypes

protected IMethodSymbol? UserSymbol { get; }

public bool HasUserSymbol => UserSymbol != null;

/// <summary>
/// Whether the current mapping code is generated for a <see cref="System.Linq.Expressions.Expression"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,20 @@ private static CollectionInfo BuildCollectionTypeForIDictionary(
ITypeSymbol value
)
{
// the types cannot be changed for mappings with a user symbol
// as the types are defined by the user
if (ctx.HasUserSymbol)
return info;

var dictionaryType = info.ImplementedTypes.HasFlag(CollectionType.IReadOnlyDictionary)
? BuildDictionaryType(ctx, CollectionType.IReadOnlyDictionary, key, value)
: info.ImplementedTypes.HasFlag(CollectionType.IDictionary)
? BuildDictionaryType(ctx, CollectionType.IDictionary, key, value)
: info.Type;
: null;

return CollectionInfoBuilder.BuildCollectionInfo(ctx.Types, ctx.SymbolAccessor, dictionaryType, info.EnumeratedType);
return dictionaryType == null
? info
: CollectionInfoBuilder.BuildCollectionInfo(ctx.Types, ctx.SymbolAccessor, dictionaryType, info.EnumeratedType);
}

private static INamedTypeSymbol BuildDictionaryType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,20 @@ or CollectionType.ICollection

private static CollectionInfo BuildCollectionTypeForICollection(MappingBuilderContext ctx, CollectionInfo info)
{
// the types cannot be changed for mappings with a user symbol
// as the types are defined by the user
if (ctx.HasUserSymbol)
return info;

var collectionType = info.ImplementedTypes.HasFlag(CollectionType.IReadOnlyCollection)
? BuildCollectionType(ctx, CollectionType.IReadOnlyCollection, info.EnumeratedType)
: info.ImplementedTypes.HasFlag(CollectionType.ICollection)
? BuildCollectionType(ctx, CollectionType.ICollection, info.EnumeratedType)
: info.Type;
return CollectionInfoBuilder.BuildCollectionInfo(ctx.Types, ctx.SymbolAccessor, collectionType, info.EnumeratedType);
: null;

return collectionType == null
? info
: CollectionInfoBuilder.BuildCollectionInfo(ctx.Types, ctx.SymbolAccessor, collectionType, info.EnumeratedType);
}

private static INamedTypeSymbol BuildCollectionType(MappingBuilderContext ctx, CollectionType type, ITypeSymbol enumeratedType)
Expand Down
8 changes: 8 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,14 @@ public Task ArrayToCollectionShouldUpgradeNullability()
return TestHelper.VerifyGenerator(source, TestHelperOptions.DisabledNullable);
}

[Fact]
public Task ArrayToList()
{
var source = TestSourceBuilder.Mapping("int[]", "List<string>");

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task ArrayToListShouldUpgradeNullability()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//HintName: Mapper.g.cs
// <auto-generated />
#nullable enable
public partial class Mapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
private partial global::System.Collections.Generic.List<string> Map(int[] source)
{
var target = new global::System.Collections.Generic.List<string>(source.Length);
foreach (var item in source)
{
target.Add(item.ToString());
}
return target;
}
}

0 comments on commit 8b14725

Please sign in to comment.