MapsterMapper / MapsterMapper/Mapster

Polymorphic mapping with collections maps only base class properties

Open
#793 11 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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.