{"action":"created","issue":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1382","repository_url":"https://api.github.com/repos/AutoMapper/AutoMapper","labels_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1382/labels{/name}","comments_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1382/comments","events_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1382/events","html_url":"https://github.com/AutoMapper/AutoMapper/issues/1382","id":162684249,"number":1382,"title":"MapFrom not applying Null Checking all of the way in 5.0","user":{"login":"richardlawley","id":1608083,"avatar_url":"https://avatars.githubusercontent.com/u/1608083?v=3","gravatar_id":"","url":"https://api.github.com/users/richardlawley","html_url":"https://github.com/richardlawley","followers_url":"https://api.github.com/users/richardlawley/followers","following_url":"https://api.github.com/users/richardlawley/following{/other_user}","gists_url":"https://api.github.com/users/richardlawley/gists{/gist_id}","starred_url":"https://api.github.com/users/richardlawley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/richardlawley/subscriptions","organizations_url":"https://api.github.com/users/richardlawley/orgs","repos_url":"https://api.github.com/users/richardlawley/repos","events_url":"https://api.github.com/users/richardlawley/events{/privacy}","received_events_url":"https://api.github.com/users/richardlawley/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/labels/Bug","name":"Bug","color":"e11d21"}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/milestones/22","html_url":"https://github.com/AutoMapper/AutoMapper/milestones/5.0.1","labels_url":"https://api.github.com/repos/AutoMapper/AutoMapper/milestones/22/labels","id":1852732,"number":22,"title":"5.0.1","description":"","creator":{"login":"jbogard","id":104498,"avatar_url":"https://avatars.githubusercontent.com/u/104498?v=3","gravatar_id":"","url":"https://api.github.com/users/jbogard","html_url":"https://github.com/jbogard","followers_url":"https://api.github.com/users/jbogard/followers","following_url":"https://api.github.com/users/jbogard/following{/other_user}","gists_url":"https://api.github.com/users/jbogard/gists{/gist_id}","starred_url":"https://api.github.com/users/jbogard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jbogard/subscriptions","organizations_url":"https://api.github.com/users/jbogard/orgs","repos_url":"https://api.github.com/users/jbogard/repos","events_url":"https://api.github.com/users/jbogard/events{/privacy}","received_events_url":"https://api.github.com/users/jbogard/received_events","type":"User","site_admin":false},"open_issues":2,"closed_issues":16,"state":"open","created_at":"2016-06-28T13:25:44Z","updated_at":"2016-06-29T19:05:47Z","due_on":null,"closed_at":null},"comments":8,"created_at":"2016-06-28T13:21:06Z","updated_at":"2016-06-30T12:30:29Z","closed_at":"2016-06-29T14:33:42Z","body":"In <5.0.0, MapFrom would check for nulls all through an expression, and return null instead of throwing an exception.  The following mapping works in 4.2.1:\r\n\r\n\tCreateMap<Person, PersonDTO>()\r\n\t\t.ForMember(dest => dest.NameOfFirstBorn, opt => opt.MapFrom(src => src.Family.Children.FirstOrDefault().Name))\r\n\t\t;\r\n\r\nIf `src.Family` is null, `.FirstOrDefault()` is never called and the destination receives a null.  In 5.0.0, AssertConfigurationIsValid still succeed, but when called with a source object where Family is null, a MappingException is generated with the inner message being `ArgumentNullException` from `FirstOrDefault`."},"comment":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/comments/229644131","html_url":"https://github.com/AutoMapper/AutoMapper/issues/1382#issuecomment-229644131","issue_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1382","id":229644131,"user":{"login":"richardlawley","id":1608083,"avatar_url":"https://avatars.githubusercontent.com/u/1608083?v=3","gravatar_id":"","url":"https://api.github.com/users/richardlawley","html_url":"https://github.com/richardlawley","followers_url":"https://api.github.com/users/richardlawley/followers","following_url":"https://api.github.com/users/richardlawley/following{/other_user}","gists_url":"https://api.github.com/users/richardlawley/gists{/gist_id}","starred_url":"https://api.github.com/users/richardlawley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/richardlawley/subscriptions","organizations_url":"https://api.github.com/users/richardlawley/orgs","repos_url":"https://api.github.com/users/richardlawley/repos","events_url":"https://api.github.com/users/richardlawley/events{/privacy}","received_events_url":"https://api.github.com/users/richardlawley/received_events","type":"User","site_admin":false},"created_at":"2016-06-30T12:30:29Z","updated_at":"2016-06-30T12:30:29Z","body":"This doesn't fix the problem.  I've modified the unit test included in the commit to demonstrate using the code from my initial post:\r\n\r\n    public class Issue1382 : AutoMapperSpecBase\r\n    {\r\n        class Person\r\n        {\r\n            public Family Family { get; set; }\r\n            public string Name { get; set; }\r\n        }\r\n\r\n        class Family\r\n        {\r\n            public List<Person> Children { get; set; } = new List<Person>();\r\n        }\r\n\r\n        class PersonDTO\r\n        {\r\n            public string Name { get; set; }\r\n            public string NameOfFirstBorn { get; set; }\r\n        }\r\n\r\n        protected override MapperConfiguration Configuration => new MapperConfiguration(cfg =>\r\n        {\r\n            cfg.CreateMap<Person, PersonDTO>().ForMember(d => d.NameOfFirstBorn, o => o.MapFrom(p => p.Family.Children.FirstOrDefault().Name));\r\n        });\r\n\r\n        [Fact]\r\n        public void We_should_catch_it()\r\n        {\r\n            Mapper.Map<PersonDTO>(new Person()).NameOfFirstBorn.ShouldBeNull();\r\n        }\r\n    }\r\n\r\nI believe the problem still occurs because `FirstOrDefault()` throws `ArgumentNullException` when it is called on null, rather than `NullReferenceException` which is being caught in the code."}}
{"actor":{"display_login":"richardlawley"}}