Skip to content

EmptyCollectionIfNull no longer initializes empty collections in reccords for nullable source #943

@moikot

Description

@moikot

I believe commit 1117602 introduced in v10.0.7-pre1 a regression in Mapster.

Previously, when using the EmptyCollectionIfNull destination transform, a null collection in the source would correctly map to an empty collection in the destination. After this change, that behavior no longer works.

class FooDto
{
    public string[] Strings { get; set; }
}

record Foo(List<string> Strings);

// Configure
TypeAdapterConfig.GlobalSettings.Default
    .AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull);

// Arrange
var fooDto = new FooDto();

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

// Assert
foo.Strings.ShouldNotBeNull();

Expected Behavior

foo.Strings should be initialized as an empty array (string[0]), not null.

Actual Behavior

foo.Strings is null.

Impact

This regression makes migration from AutoMapper significantly more difficult, as AutoMapper applies this behavior by default (null collections are mapped to empty ones).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions