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

Dictionary value type parameter generalisation leads to unassignable types #1152

Closed
Draeggiar opened this issue Mar 6, 2024 · 2 comments · Fixed by #1155
Closed

Dictionary value type parameter generalisation leads to unassignable types #1152

Draeggiar opened this issue Mar 6, 2024 · 2 comments · Fixed by #1155
Labels
bug Something isn't working released on @next

Comments

@Draeggiar
Copy link

Describe the bug
After update to version 3.4.0 from 3.3.0 Mapperly started to generate non-compilable code.
In this example i have two classes with identical property MyDictionary. The problem lays in signature of generated MapToIDictionary method. It used to accept List as dictionary value parameter but it changed to IReadonlyCollection.

Declaration code

public class A
{
    public int Value { get; set; }
}

public class B
{
    public int Value { get; set; }
}

public class ModelA
{
    public IDictionary<int, List<A>>? MyDictionary { get; set; }
}

public class ModelB
{
    public IDictionary<int, List<B>>? MyDictionary { get; set; }
}

[Mapper]
public partial class MyMapper
{
    public partial ModelB MapA(ModelA source);
}

Actual generated code

    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.4.0.0")]
    private global::System.Collections.Generic.Dictionary<int, global::System.Collections.Generic.List<global::B>> MapToDictionary(global::System.Collections.Generic.IDictionary<int, global::System.Collections.Generic.IReadOnlyCollection<global::A>> source)
    {
        var target = new global::System.Collections.Generic.Dictionary<int, global::System.Collections.Generic.List<global::B>>(source.Count);
        foreach (var item in source)
        {
            target[item.Key] = MapToList(item.Value);
        }
        return target;
    }

Expected generated code (This was generated in version 3.3.)

    private global::System.Collections.Generic.IDictionary<int, global::System.Collections.Generic.List<global::B>> MapToIDictionary(global::System.Collections.Generic.IDictionary<int, global::System.Collections.Generic.List<global::A>> source)
    {
        var target = new global::System.Collections.Generic.Dictionary<int, global::System.Collections.Generic.List<global::B>>(source.Count);
        foreach (var item in source)
        {
            target[item.Key] = MapToList(item.Value);
        }
        return target;
    }

Environment (please complete the following information):

  • Mapperly Version: 3.4.0
  • .NET Version: .NET 8.0.100
  • Target Framework: .net8.0
  • Compiler Version: 4.8.0-3.23524.11 (f43cd10b)
  • C# Language Version: 12.0
  • IDE: Rider 2023.3.3
  • OS: Windows 11
@Draeggiar Draeggiar added the bug Something isn't working label Mar 6, 2024
@latonz
Copy link
Contributor

latonz commented Mar 6, 2024

Thanks for reporting this, it definitely looks like a bug! We should never generalise the type arguments.

@latonz latonz changed the title Invalid mappings generated for nullable IDictionary value Dictionary value type parameter generalisation leads to unassignable types Mar 8, 2024
Copy link

github-actions bot commented Mar 9, 2024

🎉 This issue has been resolved in version 3.5.0-next.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released on @next
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants