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
classFooDto{publicstring[]?Strings{get;set;}}recordFoo(string?[]Strings);publicclassDestinationTransformTests{[Fact]publicvoidExplicitNullMapping_ShouldNotBeOverridden_ByDefaultEmptyCollectionTransform(){// Configurevarconfig=newTypeAdapterConfig();config.Default.AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull);config.NewConfig<Foo,FooDto>().Map(d =>d.Strings, _ =>(string[]?)null);// ArrangevarfooDto=newFoo([]);// Actvarfoo=fooDto.Adapt<FooDto>(config);// Assertfoo.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.
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.
Expected behavior
The explicit mapping:
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.