MapsterMapper / MapsterMapper/Mapster
Misleading exception message for ctor argument names
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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