Skip to content

Commit

Permalink
The Value() method for IPublishedContent was not working with the def…
Browse files Browse the repository at this point in the history
…aultValue parameter (#9888)

* The Value() method for IPublishedContent was not working with the defaultValue parameter

* Update src/Umbraco.Web/PublishedContentExtensions.cs

* Update src/Umbraco.Web/PublishedPropertyExtension.cs

* Update src/Umbraco.Web/PublishedPropertyExtension.cs

* Update src/Umbraco.Web/PublishedPropertyExtension.cs
  • Loading branch information
Shazwazza authored Mar 2, 2021
1 parent ece7008 commit 52b6709
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Umbraco.Web/PublishedContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ public static T Value<T>(this IPublishedContent content, string alias, string cu
return value;

// else... if we have a property, at least let the converter return its own
// vision of 'no value' (could be an empty enumerable) - otherwise, default
return property == null ? default : property.Value<T>(culture, segment);
// vision of 'no value' (could be an empty enumerable) - otherwise, defaultValue
return property == null ? defaultValue : property.Value<T>(culture, segment, defaultValue: defaultValue);
}

#endregion
Expand Down
15 changes: 11 additions & 4 deletions src/Umbraco.Web/PublishedPropertyExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,31 @@ public static T Value<T>(this IPublishedProperty property, string culture = null
var valueConverted = value.TryConvertTo<T>();
if (valueConverted) return valueConverted.Result;

// cannot cast nor convert the value, nothing we can return but 'default'
// cannot cast nor convert the value, nothing we can return but 'defaultValue'
// note: we don't want to fallback in that case - would make little sense
return default;
return defaultValue;
}

// we don't have a value, try fallback
if (PublishedValueFallback.TryGetValue(property, culture, segment, fallback, defaultValue, out var fallbackValue))
{
return fallbackValue;
}

// we don't have a value - neither direct nor fallback
// give a chance to the converter to return something (eg empty enumerable)
var noValue = property.GetValue(culture, segment);
if (noValue == null)
{
return defaultValue;
}
if (noValue is T noValueAsT) return noValueAsT;

var noValueConverted = noValue.TryConvertTo<T>();
if (noValueConverted) return noValueConverted.Result;

// cannot cast noValue nor convert it, nothing we can return but 'default'
return default;
// cannot cast noValue nor convert it, nothing we can return but 'defaultValue'
return defaultValue;
}

#endregion
Expand Down

0 comments on commit 52b6709

Please sign in to comment.