Skip to content

AssertConfigurationIsValid succeeds when ReverseMap from a record to a class constructed using ForCtorParam #4416

@tsvx

Description

@tsvx

Source/destination types

public class INodeSettings
{
    public string IP { get; set; }
    public string Login { get; set; }
    public string Password { get; set; }
    public int RelayIndex { get; set; }
    public bool InvertSignal { get; set; }
}

public record INodePowerLine(
    string EndPoint,
    string Login,
    string Password,
    int RelayIndex,
    bool Inverted
);

Mapping configuration

CreateMap<INodeSettings, INodePowerLine>()
      .ForCtorParam(nameof(INodePowerLine.EndPoint), opt => opt.MapFrom(s => s.IP))
      .ForCtorParam(nameof(INodePowerLine.Inverted), opt => opt.MapFrom(s => s.InvertSignal))
      .ReverseMap();

Version: 13.0.1

Expected behavior

config.AssertConfigurationIsValid() should throw an exception because the reverse mapping (from INodePowerLine to INodeSettings) is invalid -- the fields IP and InvertSignal got default values anyway.

Actual behavior

Steps to reproduce

Test:

var config = new MapperConfiguration(cfg =>
{
    cfg.AddProfile<DefaultProfile>();
});
config.AssertConfigurationIsValid();

Prove that the reverse mapping from INodePowerLine to INodeSettings is invalid -- the fields IP and InvertSignal got default values:

	var mapper = config.CreateMapper();

	var settings = new INodeSettings
	{
		IP = "192.168.1.23:50",
		Login = "admin",
		Password = "pass",
		RelayIndex = 14,
		InvertSignal = true
	};
	settings.Dump();

	var powerLine = mapper.Map<INodePowerLine>(settings);
	powerLine.Dump();

	var settings2 = mapper.Map<INodeSettings>(powerLine);
	settings2.Dump();

Also LINQPad snippet: https://share.linqpad.net/t3lacfn7.linq

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions