MapsterMapper / MapsterMapper/Mapster

Misleading exception message for ctor argument names

Open
#719 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug configuration problem
Dominant language
C#
Stars
5.2k
Forks
410
Avg merge
2d 12h
Merged PRs (30d)
6

Description

There's an exception produced when mapping to a class with a ctor:
```csharp
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()
.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:

```csharp
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the config.Compile() call in the reported constructor-mapping example and trace how constructor argument names are matched and how the no-default-constructor exception is produced. Compare the behavior when the argument is named number versus id, then make the outcome and exception wording reflect the supported constructor-mapping options and verify the existing mapping scenario.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.