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

DestinationTransform.EmptyCollectionIfNull overrides explicit null mapping

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

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

  • #960 của @wyf027 — đã đóng, không merge

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
68/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
csharp
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu với phần đăng ký DestinationTransform.EmptyCollectionIfNull và trường hợp DestinationTransformTests được nêu trong issue, sau đó tái hiện hành vi bằng TypeAdapterConfig và các kiểu Foo/FooDto được cung cấp. Theo dõi thời điểm global destination transform chạy so với kết quả .Map(...) rõ ràng; hoàn tất khi test pass với foo.Strings vẫn là null.

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

Mô tả

When DestinationTransform.EmptyCollectionIfNull is configured globally via config.Default, it still overrides explicitly defined mappings that return null.
This behavior is unexpected because explicit mappings (via .Map(...)) should take precedence over global/default transforms.

  • Mapster version: 10.0.8-pre06
  • .NET version: net10.0
class FooDto
{
    public string[]? Strings { get; set; }
}

record Foo(string?[] Strings);

public class DestinationTransformTests
{
    [Fact]
    public void ExplicitNullMapping_ShouldNotBeOverridden_ByDefaultEmptyCollectionTransform()
    {
        // Configure
        var config = new TypeAdapterConfig();

        config.Default
            .AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull);

        config.NewConfig<Foo, FooDto>()
            .Map(d => d.Strings, _ => (string[]?)null);

        // Arrange
        var fooDto = new Foo([]);

        // Act
        var foo = fooDto.Adapt<FooDto>(config);

        // Assert
        foo.Strings.ShouldBeNull();
    }
}

Expected behavior

The explicit mapping:

.Map(d => d.Strings, _ => (string[]?)null)

should be respected, resulting in foo.Strings == null.

Actual behavior

foo.Strings is transformed into an empty array, meaning that EmptyCollectionIfNull is applied after the explicit mapping and overrides it.

Notes

The transform is registered on config.Default, not per-type.
This suggests that global destination transforms are applied at a stage where they override even explicit mapping results.
This makes it impossible to explicitly map a collection property to null when EmptyCollectionIfNull is enabled globally.

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.