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

Fixes NullReferenceException with recursive GetVortoValue #100

Merged
merged 2 commits into from
Aug 17, 2018
Merged
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
109 changes: 56 additions & 53 deletions src/Our.Umbraco.Vorto/Extensions/IPublishedContentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Threading;
using System.Threading;
using Newtonsoft.Json;
using Our.Umbraco.Vorto.Helpers;
using Our.Umbraco.Vorto.Models;
Expand Down Expand Up @@ -95,62 +95,65 @@ private static T DoInnerGetVortoValue<T>(this IPublishedContent content, string
bool recursive = false, T defaultValue = default(T))
{
var prop = content.GetProperty(propertyAlias);
var vortoModel = prop.Value as VortoValue;
if (vortoModel?.Values != null)
// prevent generation of NullReferenceException: allow return of defaultValue or traversal up the tree
if (prop != null)
{
// Get the serialized value
var bestMatchCultureName = vortoModel.FindBestMatchCulture(cultureName);
if (!bestMatchCultureName.IsNullOrWhiteSpace()
&& vortoModel.Values.ContainsKey(bestMatchCultureName)
&& vortoModel.Values[bestMatchCultureName] != null
&& !vortoModel.Values[bestMatchCultureName].ToString().IsNullOrWhiteSpace())
var vortoModel = prop.Value as VortoValue;
if (vortoModel?.Values != null)
{
var value = vortoModel.Values[bestMatchCultureName];

// Get target datatype
var targetDataType = VortoHelper.GetTargetDataTypeDefinition(vortoModel.DtdGuid);

// Umbraco has the concept of a IPropertyEditorValueConverter which it
// also queries for property resolvers. However I'm not sure what these
// are for, nor can I find any implementations in core, so am currently
// just ignoring these when looking up converters.
// NB: IPropertyEditorValueConverter not to be confused with
// IPropertyValueConverter which are the ones most people are creating
var properyType = CreateDummyPropertyType(
targetDataType.Id,
targetDataType.PropertyEditorAlias,
content.ContentType);

var inPreviewMode = UmbracoContext.Current != null && UmbracoContext.Current.InPreviewMode;

// Try convert data to source
// We try this first as the value is stored as JSON not
// as XML as would occur in the XML cache as in the act
// of converting to XML this would ordinarily get called
// but with JSON it doesn't, so we try this first
var converted1 = properyType.ConvertDataToSource(value, inPreviewMode);
if (converted1 is T) return (T)converted1;

var convertAttempt = converted1.TryConvertTo<T>();
if (convertAttempt.Success) return convertAttempt.Result;

// Try convert source to object
// If the source value isn't right, try converting to object
var converted2 = properyType.ConvertSourceToObject(converted1, inPreviewMode);
if (converted2 is T) return (T)converted2;

convertAttempt = converted2.TryConvertTo<T>();
if (convertAttempt.Success) return convertAttempt.Result;

// Try just converting
convertAttempt = value.TryConvertTo<T>();
if (convertAttempt.Success) return convertAttempt.Result;

// Still not right type so return default value
return defaultValue;
// Get the serialized value
var bestMatchCultureName = vortoModel.FindBestMatchCulture(cultureName);
if (!bestMatchCultureName.IsNullOrWhiteSpace()
&& vortoModel.Values.ContainsKey(bestMatchCultureName)
&& vortoModel.Values[bestMatchCultureName] != null
&& !vortoModel.Values[bestMatchCultureName].ToString().IsNullOrWhiteSpace())
{
var value = vortoModel.Values[bestMatchCultureName];

// Get target datatype
var targetDataType = VortoHelper.GetTargetDataTypeDefinition(vortoModel.DtdGuid);

// Umbraco has the concept of a IPropertyEditorValueConverter which it
// also queries for property resolvers. However I'm not sure what these
// are for, nor can I find any implementations in core, so am currently
// just ignoring these when looking up converters.
// NB: IPropertyEditorValueConverter not to be confused with
// IPropertyValueConverter which are the ones most people are creating
var properyType = CreateDummyPropertyType(
targetDataType.Id,
targetDataType.PropertyEditorAlias,
content.ContentType);

var inPreviewMode = UmbracoContext.Current != null && UmbracoContext.Current.InPreviewMode;

// Try convert data to source
// We try this first as the value is stored as JSON not
// as XML as would occur in the XML cache as in the act
// of converting to XML this would ordinarily get called
// but with JSON it doesn't, so we try this first
var converted1 = properyType.ConvertDataToSource(value, inPreviewMode);
if (converted1 is T) return (T)converted1;

var convertAttempt = converted1.TryConvertTo<T>();
if (convertAttempt.Success) return convertAttempt.Result;

// Try convert source to object
// If the source value isn't right, try converting to object
var converted2 = properyType.ConvertSourceToObject(converted1, inPreviewMode);
if (converted2 is T) return (T)converted2;

convertAttempt = converted2.TryConvertTo<T>();
if (convertAttempt.Success) return convertAttempt.Result;

// Try just converting
convertAttempt = value.TryConvertTo<T>();
if (convertAttempt.Success) return convertAttempt.Result;

// Still not right type so return default value
return defaultValue;
}
}
}

return recursive && content.Parent != null
? content.Parent.DoInnerGetVortoValue<T>(propertyAlias, cultureName, recursive, defaultValue)
: defaultValue;
Expand Down