MapsterMapper / MapsterMapper/Mapster
Mapping an object with getter only properties seems to be broken in v10.0.6 (works as expected in 7.4.0)
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 5.2k
- Forks
- 410
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
Hello. I am trying to map an object with getter-only properties using a custom `Map` functon. This seems to be broken in v10.0.6, while it was fine in v7.4.0. See the following sample:
```c#
var typeAdapterConfig = new Mapster.TypeAdapterConfig();
typeAdapterConfig.Scan(typeof(Program).Assembly);
typeAdapterConfig.Compile();
var mapper = new MapsterMapper.Mapper(typeAdapterConfig);
var dto = new Dto { Data = new("new") };
Console.WriteLine(mapper.Map(dto, new Entity { Data = new("old") }).Data?.Value); // outputs "old" with v10.0.6, outputs "new" with v7.4.0
Console.WriteLine(mapper.Map(dto, new Entity()).Data?.Value); // outputs "new" with both v10.0.6 and v7.4.0
Console.WriteLine(mapper.Map(dto).Data?.Value); // outputs "new" with both v10.0.6 and v7.4.0
class Dto { public Data? Data { get; set; } }
class Entity { public Data? Data { get; set; } }
class Data(string value) { public string Value => value; }
class Mappings : Mapster.IRegister
{
public void Register(Mapster.TypeAdapterConfig config)
{
config.NewConfig()
.Map(x => x.Data, x => x.Data == null ? null : new Data(x.Data.Value));
}
}
```
Specifically this case seems to be broken:
```c#
var dto = new Dto { Data = new("new") };
var entity = mapper.Map(dto, new Entity { Data = new("old") });
```
I would expect the result entity's `Data` property to be mapped correctly from the source dto, as specified in the mapping function:
`config.NewConfig().Map(x => x.Data, x => x.Data == null ? null : new Data(x.Data.Value));`
As noted, Mapster v7.4.0 seems to be find, but v10.0.* seems to be broken.
Or maybe I am missing something here, please advise, thank you.
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 by running the supplied C# reproduction with Mapster v10.0.6 and compare the custom Dto-to-Entity mapping with v7.4.0. Trace the behavior of mapping onto an existing Entity when Data has a getter-only Value, and confirm done when the existing entity receives the new Data value while the other shown cases still pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 50/100