Skip to content

Nullable collection coercion not applied to constructor parameters #119

Description

@superyyrrzz

Bug

ForgeMap v1.6 correctly applies nullable-safe collection coercion (via .ToDictionary() adapter) for property assignments, but does NOT apply it for constructor parameter expressions. This causes CS8620 under TreatWarningsAsErrors.

Reproduction

// Source (no nullable annotations):
public class Source
{
    public IDictionary<string, object> Metadata { get; set; }
}

// Destination (constructor-based, with nullable annotations):
public class Dest
{
    public Dest(IReadOnlyDictionary<string, object?>? metadata = null)
    {
        Metadata = metadata ?? new Dictionary<string, object?>();
    }
    public IReadOnlyDictionary<string, object?> Metadata { get; }
}

[ForgeMap(NullPropertyHandling = NullPropertyHandling.CoalesceToNew)]
public partial class TestForger
{
    public partial Dest Forge(Source source);
}

Generated code (constructor path):

return new Dest(
    metadata: new ReadOnlyDictionary<string, object>(source.Metadata)  // CS8620!
);

Expected: Should apply the same .ToDictionary() nullable adapter as property assignment:

return new Dest(
    metadata: Enumerable.ToDictionary(
        Enumerable.Select(source.Metadata, kv => new KeyValuePair<string, object?>(kv.Key, (object?)kv.Value)),
        kv => kv.Key, kv => kv.Value)
);

Impact

This prevents using constructor mapping for types with IReadOnlyDictionary<string, object?> constructor parameters when the source uses IDictionary<string, object>. The user must keep a manual mapping method.

Environment

  • ForgeMap 1.6.0
  • .NET 8.0 with TreatWarningsAsErrors

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions