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

UseDeepCloning generate Dictionary<string?, xxx> when mapping from nullable disable class file #1615

Open
3 tasks done
latop2604 opened this issue Nov 29, 2024 · 0 comments
Open
3 tasks done
Labels
bug Something isn't working

Comments

@latop2604
Copy link

latop2604 commented Nov 29, 2024

  • I have read the documentation, including the FAQ
  • I can reproduce the bug using the latest prerelease version
  • I have searched existing discussion and issue to avoid duplicates

Describe the bug
The source generator does not respect generic type parameter constraint notnull on Dictionary TKey when mapping a class with #nullable disable

Instead of Dictionary<string?, xxx>, it should generate Dictionary<string, int>

Declaration code

#nullable enable
using Riok.Mapperly.Abstractions;

DataMapper.MapData(new Data());


[Mapper(UseDeepCloning = true)]
public static partial class DataMapper
{
    public static partial Data MapData(Data data);
}


#nullable disable
public class Data
{
    public int Id { get; set; }
    public Dictionary<string, int> Attributes { get; set; }
}

Actual relevant generated code

// <auto-generated />
#nullable enable
public static partial class DataMapper
{
   //...

    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.1.0")]
    private static global::System.Collections.Generic.Dictionary<string?, int> MapToDictionaryOfStringAndInt32(global::System.Collections.Generic.IReadOnlyDictionary<string?, int> source)
    {
        var target = new global::System.Collections.Generic.Dictionary<string?, int>(source.Count);
        foreach (var item in source)
        {
            target[item.Key == null ? default : item.Key] = item.Value;
        }
        return target;
    }
}

Expected relevant generated code

// <auto-generated />
#nullable enable
public static partial class DataMapper
{
    // ...

    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.1.0")]
    private static global::System.Collections.Generic.Dictionary<string, int> MapToDictionaryOfStringAndInt32(global::System.Collections.Generic.IReadOnlyDictionary<string, int> source)
    {
        var target = new global::System.Collections.Generic.Dictionary<string, int>(source.Count);
        foreach (var item in source)
        {
            target[item.Key == null ? default : item.Key] = item.Value;
        }
        return target;
    }
}

Reported relevant diagnostics

  • Argument of type 'Dictionary<string, int>' cannot be used for parameter 'source' of type 'IReadOnlyDictionary<string?, int>' in 'Dictionary<string?, int> DataMapper.MapToDictionaryOfStringAndInt32(IReadOnlyDictionary<string?, int> source)' due to differences in the nullability of reference types.
  • The type 'string?' cannot be used as type parameter 'TKey' in the generic type or method 'Dictionary<TKey, TValue>'. Nullability of type argument 'string?' doesn't match 'notnull' constraint.

Environment (please complete the following information):

  • Mapperly Version: 4.1.1
  • Nullable reference types: enable
  • .NET Version: .NET 8.0
  • Target Framework: net8.0
  • Compiler Version: Compiler version: '4.12.0-3.24558.5 (21192bfc)'. Language version: 12.0.
  • C# Language Version: 13
  • IDE: console
  • OS: windows 24H2
@latop2604 latop2604 added the bug Something isn't working label Nov 29, 2024
@latop2604 latop2604 changed the title Generate Dictionary<string?, xxx> when mapping from nullable disable class file UseDeepCloning generate Dictionary<string?, xxx> when mapping from nullable disable class file Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant