Skip to content

Commit

Permalink
Revert "The Value() method for IPublishedContent was not working with…
Browse files Browse the repository at this point in the history
… the defaultValue parameter" (#9989)
  • Loading branch information
mikecp authored Mar 17, 2021
1 parent 1570d49 commit 156c1c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/Umbraco.Web/PublishedContentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
Expand Down 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, defaultValue
return property == null ? defaultValue : property.Value<T>(culture, segment, defaultValue: defaultValue);
// vision of 'no value' (could be an empty enumerable) - otherwise, default
return property == null ? default : property.Value<T>(culture, segment, fallback, defaultValue);
}

#endregion
Expand Down
33 changes: 23 additions & 10 deletions src/Umbraco.Web/PublishedPropertyExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Models.PublishedContent;
Expand Down Expand Up @@ -37,13 +36,20 @@ public static T Value<T>(this IPublishedProperty property, string culture = null
// we have a value
// try to cast or convert it
var value = property.GetValue(culture, segment);
if (value is T valueAsT) return valueAsT;
if (value is T valueAsT)
{
return valueAsT;
}

var valueConverted = value.TryConvertTo<T>();
if (valueConverted) return valueConverted.Result;
if (valueConverted)
{
return valueConverted.Result;
}

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

// we don't have a value, try fallback
Expand All @@ -57,15 +63,22 @@ public static T Value<T>(this IPublishedProperty property, string culture = null
var noValue = property.GetValue(culture, segment);
if (noValue == null)
{
return defaultValue;
return default;
}

if (noValue is T noValueAsT)
{
return noValueAsT;
}
if (noValue is T noValueAsT) return noValueAsT;

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

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

#endregion
Expand Down

0 comments on commit 156c1c9

Please sign in to comment.