question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AllowNullCollections has no effect when a profile is used

See original GitHub issue

When a mapper configuration is set through a profile, the AllowNullCollections setting has no effect. The following pseudo test fails:

public class Source
{
    public System.Collections.Generic.IList<string> Collection { get; set; }
}

public class Destination
{
    public System.Collections.Generic.IList<string> Collection { get; set; }
}

public class TestProfile: AutoMapper.Profile
{
    protected override void Configure()
    {
        CreateMap<Source, Destination>();
    }
}

[Fact]
public void NullCollection()
{
    var mapper = new MapperConfiguration(cfg => {
        cfg.AllowNullCollections=true;
        cfg.AddProfile<TestProfile>();
    }).CreateMapper();
    var source = new Source();

    Destination destination = mapper.Map<Destination>(source);

    Assert.Null(destination.Collection);
}

When configured directly with CreateMap, the setting works as expected.

AutoMapper 4.2.1

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
engenbcommented, Oct 7, 2016

I am seeing this issue, possibly a regression, in version 5.1.1.

The following unit test fails

[Fact]
public void Test()
{
    var config = new MapperConfiguration(configure =>
    {
        configure.AddProfile<TestProfile>();
    });

    config.AssertConfigurationIsValid();
    var mapper = config.CreateMapper();

    var source = new Source();

    var result = mapper.Map<Destination>(source);

    Assert.Null(result.Dictionary);
}

public class Source
{
    public IDictionary<string, string> Dictionary { get; set; }
}

public class Destination
{
    public IDictionary<string, string> Dictionary { get; set; }
}

public class TestProfile : Profile
{
    public TestProfile()
    {
        AllowNullCollections = true;
        CreateMap<Source, Destination>();
    }
}
0reactions
lock[bot]commented, May 7, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Automapper: setting AllowNullCollections on the profile
There is a property on the Profile class called AllowNullCollections. It is not affecting the mapping. If I set cfg.AllowNullCollections to True ...
Read more >
AutoMapper Documentation
A convention-based object-object mapper. AutoMapper uses a fluent configuration API to define an object-object mapping strategy.
Read more >
Lists and Arrays
This behavior can be changed by setting the AllowNullCollections property to true when configuring the mapper. var configuration = new MapperConfiguration(cfg => ...
Read more >
C# (CSharp) ConfigurationStore Examples
C# (CSharp) ConfigurationStore - 60 examples found. These are the top rated real world C# (CSharp) examples of ConfigurationStore extracted from open source ......
Read more >
How to disable selection some item in a listview windows ...
Start with a copy of the list box item style then add a Setter to change the IsHitTestVisible property. For example:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found