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

Misleading exception message for ctor argument names #719

Open
Ekkeir opened this issue Jun 21, 2024 · 1 comment
Open

Misleading exception message for ctor argument names #719

Ekkeir opened this issue Jun 21, 2024 · 1 comment

Comments

@Ekkeir
Copy link

Ekkeir commented Jun 21, 2024

There's an exception produced when mapping to a class with a ctor:

public class Destination
{
    public Destination(int number)
    {
        Id = number;
    }

    public int Id { get; }
}

public class Source
{
    public int Number { get; set; }
}

[Fact]
public void Should_Map()
{
    var config = new TypeAdapterConfig();

    config.ForType<Source, Destination>()
        .Map(dest => dest.Id, source => source.Number);

    config.Compile(); // throws an exception
}

The following exception is produced:

System.InvalidOperationException : No default constructor for type 'Destination', please use 'ConstructUsing' or 'MapWith'.

The exception message seems misleading as (I may have missed it in documentation) mapster seems to depend on argument names.
Renaming Destination ctor argument from number to id is enough for the test to succeed. That is, changing to the following:

public class Destination
{
    public Destination(int id)
    {
        Id = id;
    }

    public int Id { get; }
}

Suggestions:

  • maybe this name-dependency could be reduced?
  • it would be great if the exception message could mention that there's an option of renaming ctor arguments to match the properties
@stagep
Copy link

stagep commented Jul 4, 2024

What should happen in this scenario if name-dependency was reduced?

public class Destination
{
    public Destination(int number1, int number2)
    {
        Id1 = number2;
        Id2 = number1;
    }
    public int Id1 { get; }
    public int Id2 { get; }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants