diff --git a/Zone.UmbracoMapper.Common/UmbracoMapperBase.cs b/Zone.UmbracoMapper.Common/UmbracoMapperBase.cs index ad71dc0..83236e5 100644 --- a/Zone.UmbracoMapper.Common/UmbracoMapperBase.cs +++ b/Zone.UmbracoMapper.Common/UmbracoMapperBase.cs @@ -329,7 +329,10 @@ protected static void MapNativeContentPropertyValue(T model, PropertyInfo pro // Concatenation and coalescing only supported/makes sense in this case too. if (property.PropertyType.Name == "String") { - var stringValue = value.ToString(); + // Prevent null reference exception if a value gets here from NuCache.PublishedContent.Name and the value is null. This can + // happen when content with a localised set of values references another piece of content, and that other piece of content + // does not have a published version for that language + var stringValue = value == null ? null : value.ToString(); if (concatenateToExistingValue) { var prefixValueWith = property.GetValue(model) + concatenationSeperator; diff --git a/Zone.UmbracoMapper.V8.Tests/UmbracoMapperTests.cs b/Zone.UmbracoMapper.V8.Tests/UmbracoMapperTests.cs index f3f8a4e..7798263 100644 --- a/Zone.UmbracoMapper.V8.Tests/UmbracoMapperTests.cs +++ b/Zone.UmbracoMapper.V8.Tests/UmbracoMapperTests.cs @@ -47,6 +47,22 @@ public void UmbracoMapper_MapFromIPublishedContent_MapsNativePropertiesWithMatch Assert.AreEqual(1000, model.Id); Assert.AreEqual("Test content", model.Name); } + + [TestMethod] + public void UmbracoMapper_MapFromIPublishedContent_MapsNativePropertiesWithNullName() + { + // Arrange + var model = new SimpleViewModel(); + var content = MockPublishedContent(name: null); + var mapper = GetMapper(); + + // Act + mapper.Map(content.Object, model); + + // Assert + Assert.AreEqual(1000, model.Id); + Assert.IsNull(model.Name); + } [TestMethod] public void UmbracoMapper_MapFromIPublishedContent_MapsNativePropertiesWithDifferentNames() @@ -3446,13 +3462,14 @@ private static Mock MockPublishedElement(string bodyTextValue private static Mock MockPublishedContent(int id = 1000, string bodyTextValue = "This is the body text", bool recursiveCall = false, - bool mockParent = true) + bool mockParent = true, + string name = "Test content") { var contentMock = new Mock(); SetUpPropertyMocks(bodyTextValue, recursiveCall, contentMock.As()); contentMock.Setup(c => c.Id).Returns(id); - contentMock.Setup(c => c.Name).Returns("Test content"); + contentMock.Setup(c => c.Name).Returns(name); contentMock.Setup(c => c.CreatorName).Returns("A.N. Editor"); SetUpParentContentMock(contentMock, mockParent);