MapsterMapper / MapsterMapper/Mapster
Polymorphic mapping with collections maps only base class properties
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 5.2k
- Forks
- 410
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
This might be related to #776. I have a class hierarchy and a base class DTO that combines all possible properties. When mapping a single item cast to the base class - derived properties are mapped, however, when mapping a collection of the base class items - derived properties are ignored. This doesn't seem like a consistent behavior to me and now it must be fixed with explicit map config.
```cs
var items = new List
{
new DerivedOne
{
Id = Guid.NewGuid(),
ExtraProperty = "A",
Property = "a",
},
new DerivedTwo
{
Id = Guid.NewGuid(),
Property = "b",
OtherExtraProperty = "B",
}
};
// TypeAdapterConfig.NewConfig()
// .Include()
// .Include();
// Here ExtraProperty and OtherExtraProperty are not mapped unless the config above is present
var mappedResultOne= b.Adapt();
// Here OtherExtraProperty is mapped.
var mappedResultTwo = b[1].Adapt();
public abstract class Base
{
public Guid Id { get; set; }
public required string Property { get; set; }
public abstract string Type { get; protected set; }
}
public class DerivedOne : Base
{
public required string ExtraProperty { get; set; }
public override string Type { get; protected set; } = "DerivedOne";
}
public class DerivedTwo : Base
{
public required string OtherExtraProperty { get; set; }
public override string Type { get; protected set; } = "DerivedTwo";
}
public record BaseDto
{
public Guid Id { get; init; }
public required string Property { get; init; }
public string? ExtraProperty { get; init; }
public string? OtherExtraProperty { get; init; }
public required string Type { get; init; }
}
```
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 with the reproducer in issue #793 and compare the single-item Adapt result with the collection Adapt result, including the optional Include configuration. Done means collection mapping preserves ExtraProperty and OtherExtraProperty for the derived runtime types without requiring that explicit configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100