{"action":"created","issue":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1903","repository_url":"https://api.github.com/repos/AutoMapper/AutoMapper","labels_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1903/labels{/name}","comments_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1903/comments","events_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1903/events","html_url":"https://github.com/AutoMapper/AutoMapper/issues/1903","id":200396728,"number":1903,"title":"Change in null collection behaviour between 3.2.1 and latest version","user":{"login":"adam-resdiary","id":4849264,"avatar_url":"https://avatars.githubusercontent.com/u/4849264?v=3","gravatar_id":"","url":"https://api.github.com/users/adam-resdiary","html_url":"https://github.com/adam-resdiary","followers_url":"https://api.github.com/users/adam-resdiary/followers","following_url":"https://api.github.com/users/adam-resdiary/following{/other_user}","gists_url":"https://api.github.com/users/adam-resdiary/gists{/gist_id}","starred_url":"https://api.github.com/users/adam-resdiary/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adam-resdiary/subscriptions","organizations_url":"https://api.github.com/users/adam-resdiary/orgs","repos_url":"https://api.github.com/users/adam-resdiary/repos","events_url":"https://api.github.com/users/adam-resdiary/events{/privacy}","received_events_url":"https://api.github.com/users/adam-resdiary/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2017-01-12T15:28:36Z","updated_at":"2017-01-13T11:43:09Z","closed_at":"2017-01-13T11:43:09Z","body":"I've noticed a change in behaviour between 3.2.1 and the latest version that's giving us grief. It seems that in version 3.2.1, if I return null from ResolveUsing for an IEnumerable<T> object it maps to null, whereas in the latest stable and pre-release versions it maps to an empty collection.\r\n\r\nHere's a test case that illustrates the issue:\r\n\r\n```csharp\r\npublic class Source\r\n{\r\n\tpublic IEnumerable<int> Ints { get; set; }\r\n}\r\n\r\npublic class Destination\r\n{\r\n\tpublic IEnumerable<int> Ints { get; set; }\r\n}\r\n\r\n[Test]\r\npublic void Test()\r\n{\r\n\tvar config =\r\n\t\tnew MapperConfiguration(\r\n\t\t\tcfg =>\r\n\t\t\t\tcfg.CreateMap<Source, Destination>()\r\n\t\t\t\t\t.ForMember(d => d.Ints, o => o.ResolveUsing((Func<Source, IEnumerable<int>>) (s => null))));\r\n\tvar mapper = config.CreateMapper();\r\n\r\n\tvar destination = mapper.Map<Destination>(new Source { Ints = null });\r\n\r\n\tAssert.IsNull(destination.Ints);\r\n}\r\n```\r\n\r\nTo me this behaviour seems odd, because as far as I understood it, when you use ResolveUsing you're basically saying that you want to be responsible for mapping the property yourself, and AutoMapper should use exactly the value you give it.\r\n\r\nFrom what I've found, I've got two workarounds:\r\n\r\n  1. Set AllowNullCollections on the AutoMapper configuration.\r\n  2. Use AfterMap to manually set my collection null.\r\n\r\nOption 1 is too risky for me to use because it impacts absolutely every mapping across the board, and there doesn't seem to be the option to do it on a per-map basis.\r\n\r\nOption 2 works, but it seems odd that I'd need to do this.\r\n\r\nAdditionally, if I use the AfterMap approach, I have to also Ignore the property to get it to work:\r\n\r\n```csharp\r\n[Test]\r\npublic void TestAfterMap()\r\n{\r\n\tvar config =\r\n\t\tnew MapperConfiguration(\r\n\t\t\tcfg =>\r\n\t\t\t\tcfg.CreateMap<Source, Destination>()\r\n\t\t\t\t\t.ForMember(d => d.Ints, o => o.Ignore())\r\n\t\t\t\t\t.AfterMap((d, context) =>\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\td.Ints = null;\r\n\t\t\t\t\t}));\r\n\tvar mapper = config.CreateMapper();\r\n\r\n\tvar destination = mapper.Map<Destination>(new Source { Ints = null });\r\n\r\n\tAssert.IsNull(destination.Ints);\r\n}\r\n```\r\n\r\nThis is counter-intuitive to me, because the name \"AfterMap\" indicates to me that it happens after AutoMapper has performed its mapping, so if I set the value of a property it seems odd to me that AutoMapper then seems to perform further work that then overwrites that unless I ignore the property.\r\n\r\nI realise that it's a bit odd mapping an IEnumerable to null, which is why I really don't want to have to enable `AllowNullCollections` globally, but in the scenario where we do use it the object can have a null, empty or populated collection, each of which means a slightly different thing.\r\n\r\nMy personal opinion is that `ResolveUsing` should just use exactly the result that it gets from the mapping function, but failing that it would be good to be able to specify `AllowNullCollections` at the mapper level.\r\n\r\nI've tested this against versions 3.2.1 (obviously with slightly different code because of the switch from the static config API), 5.2.0 and 5.3.0-alpha-01276. "},"comment":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/comments/272424817","html_url":"https://github.com/AutoMapper/AutoMapper/issues/1903#issuecomment-272424817","issue_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1903","id":272424817,"user":{"login":"adam-resdiary","id":4849264,"avatar_url":"https://avatars.githubusercontent.com/u/4849264?v=3","gravatar_id":"","url":"https://api.github.com/users/adam-resdiary","html_url":"https://github.com/adam-resdiary","followers_url":"https://api.github.com/users/adam-resdiary/followers","following_url":"https://api.github.com/users/adam-resdiary/following{/other_user}","gists_url":"https://api.github.com/users/adam-resdiary/gists{/gist_id}","starred_url":"https://api.github.com/users/adam-resdiary/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adam-resdiary/subscriptions","organizations_url":"https://api.github.com/users/adam-resdiary/orgs","repos_url":"https://api.github.com/users/adam-resdiary/repos","events_url":"https://api.github.com/users/adam-resdiary/events{/privacy}","received_events_url":"https://api.github.com/users/adam-resdiary/received_events","type":"User","site_admin":false},"created_at":"2017-01-13T11:43:09Z","updated_at":"2017-01-13T11:43:09Z","body":"Ok - I'm happy to close this since I've got a solution now."}}
{"actor":{"display_login":"adam-resdiary"}}