Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Polymorphic mapping with collections maps only base class properties

Đang mở
#793 11 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
csharp
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu với reproducer trong issue #793 và so sánh kết quả Adapt cho một phần tử đơn với kết quả Adapt<BaseDto[]> cho collection, bao gồm cả cấu hình Include tùy chọn. Được xem là hoàn thành khi việc ánh xạ collection giữ lại ExtraProperty và OtherExtraProperty cho các kiểu runtime dẫn xuất mà không yêu cầu cấu hình tường minh đó.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug configuration problem

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.

var items = new List<Base>
{
    new DerivedOne
    {
        Id = Guid.NewGuid(),
        ExtraProperty = "A",
        Property = "a",
    },
    new DerivedTwo
    {
        Id = Guid.NewGuid(),
        Property = "b",
        OtherExtraProperty = "B",
    }
};

// TypeAdapterConfig<Base, BaseDto>.NewConfig()
// .Include<DerivedOne, BaseDto>()
// .Include<DerivedTwo, BaseDto>();

// Here ExtraProperty and OtherExtraProperty are not mapped unless the config above is present
var mappedResultOne= b.Adapt<BaseDto[]>();

// Here OtherExtraProperty is mapped.
var mappedResultTwo = b[1].Adapt<BaseDto>();

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; }
}
Ngôn ngữ chính
C#
Star
5.2k
Fork
409
Merge trung bình
3 ngày 4 giờ
Pull request đã merge (30 ngày)
3

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của MapsterMapper/Mapster

Tất cả issue của MapsterMapper/Mapster

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.