Skip to content

Additional mapping method parameter not mapped when source/target is nullable #2241

@kockicica

Description

@kockicica

Please do the checklist before filing an issue:

  • 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
When the mapping source/target is nullable and the mapping method has an additional parameter, that parameter is either ignored entirely (when [MapperIgnoreSource] is present) or silently dropped from the generated code (when it's absent). The id parameter should be mapped to target.Id but is not.

5.0.0-next.3 works fine - expected generated code is actually from next.3 version.

Repro
Same repository demonstrates the issue: https://github.com/kockicica/mapperly-test

Declaration code

[Mapper]
public partial class Mapper
{
    [MapperIgnoreSource(nameof(@Model.Scenario.Id))]
    public partial Data.Scenario? ModelToEntity(Model.Scenario? source, Guid? id=null);
    
    
}

Actual relevant generated code

    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "5.0.0.0")]
    [return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(nameof(source))]
    public partial global::Data.Scenario? ModelToEntity(global::Model.Scenario? source, global::System.Guid? id)
    {
        if (source == null)
            return default;
        var target = new global::Data.Scenario();
        target.Name = source.Name;
        target.Description = source.Description;
        target.AudioFileName = source.AudioFileName;
        target.AudioFilePath = source.AudioFilePath;
        if (source.Actions != null)
        {
            target.Actions = MapToListOfScenarioAction(source.Actions);
        }
        else
        {
            target.Actions = null;
        }
        return target;
    }

Expected relevant generated code

    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "5.0.0.0")]
    [return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(nameof(source))]
    public partial global::Data.Scenario? ModelToEntity(global::Model.Scenario? source, global::System.Guid? id)
    {
        if (source == null)
            return default;
        var target = new global::Data.Scenario();
        if (id != null)
        {
            target.Id = id.Value;
        }
        target.Name = source.Name;
        target.Description = source.Description;
        target.AudioFileName = source.AudioFileName;
        target.AudioFilePath = source.AudioFilePath;
        if (source.Actions != null)
        {
            target.Actions = MapToListOfScenarioAction(source.Actions);
        }
        else
        {
            target.Actions = null;
        }
        return target;
    }

Environment (please complete the following information):

  • Mapperly Version: 5.0.0-next.6
  • Target Framework: net10.0
  • Compiler Version: 5.3.0-2.26153.122 (4d3023de605a78ba3e59e50c657eed70f125c68a)
  • C# Language Version: 14.0
  • OS: Windows 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions