Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NullReferenceException when mapping Name to a property value #54

Merged
merged 2 commits into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Zone.UmbracoMapper.Common/UmbracoMapperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,10 @@ protected static void MapNativeContentPropertyValue<T>(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;
Expand Down
21 changes: 19 additions & 2 deletions Zone.UmbracoMapper.V8.Tests/UmbracoMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -3446,13 +3462,14 @@ private static Mock<IPublishedElement> MockPublishedElement(string bodyTextValue
private static Mock<IPublishedContent> 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<IPublishedContent>();
SetUpPropertyMocks(bodyTextValue, recursiveCall, contentMock.As<IPublishedElement>());

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);
Expand Down