Skip to content

Commit 6fd5eee

Browse files
committed
Fixes: U4-3591 Creating PropertyValueConverter for a v7 prop editor (created with manifest) ends up with YSOD when outputting value on frontend
1 parent 4354840 commit 6fd5eee

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

src/Umbraco.Core/Models/PublishedContent/PublishedPropertyType.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,38 @@ private void InitializeConverters()
8282
{
8383
var converters = PropertyValueConvertersResolver.Current.Converters.ToArray();
8484

85-
// todo: remove Union() once we drop IPropertyEditorValueConverter support.
85+
var defaultConverters = converters
86+
.Where(x => x.GetType().GetCustomAttribute<DefaultPropertyValueConverterAttribute>(false) != null)
87+
.ToArray();
88+
8689
_converter = null;
87-
foreach (var converter in converters.Union(GetCompatConverters()).Where(x => x.IsConverter(this)))
90+
91+
//get all converters for this property type
92+
// todo: remove Union() once we drop IPropertyEditorValueConverter support.
93+
var foundConverters = converters.Union(GetCompatConverters()).Where(x => x.IsConverter(this)).ToArray();
94+
if (foundConverters.Length == 1)
8895
{
89-
if (_converter == null)
90-
{
91-
_converter = converter;
92-
}
93-
else
96+
_converter = foundConverters[0];
97+
}
98+
else if (foundConverters.Length > 1)
99+
{
100+
//more than one was found, we need to first figure out if one of these is an Umbraco default value type converter
101+
var nonDefault = foundConverters.Except(defaultConverters).ToArray();
102+
if (nonDefault.Length > 1)
94103
{
95-
throw new InvalidOperationException(string.Format("Type '{2}' cannot be an IPropertyValueConverter"
96-
+ " for property '{1}' of content type '{0}' because type '{3}' has already been detected as a converter"
97-
+ " for that property, and only one converter can exist for a property.",
98-
ContentType.Alias, PropertyTypeAlias,
99-
converter.GetType().FullName, _converter.GetType().FullName));
104+
//this is not allowed, there cannot be more than 1 custom converter
105+
throw new InvalidOperationException(
106+
string.Format("Type '{2}' cannot be an IPropertyValueConverter"
107+
+ " for property '{1}' of content type '{0}' because type '{3}' has already been detected as a converter"
108+
+ " for that property, and only one converter can exist for a property.",
109+
ContentType.Alias, PropertyTypeAlias,
110+
nonDefault[1].GetType().FullName, nonDefault[0].GetType().FullName));
100111
}
101-
}
102112

113+
//there's only 1 custom converter registered that so use it
114+
_converter = nonDefault[0];
115+
}
116+
103117
// get the cache levels, quietely fixing the inconsistencies (no need to throw, really)
104118
_sourceCacheLevel = GetCacheLevel(_converter, PropertyCacheValue.Source);
105119
_objectCacheLevel = GetCacheLevel(_converter, PropertyCacheValue.Object);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Umbraco.Core.PropertyEditors
4+
{
5+
/// <summary>
6+
/// Indicates that this is a default property value converter (shipped with Umbraco)
7+
/// </summary>
8+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
9+
internal class DefaultPropertyValueConverterAttribute : Attribute
10+
{
11+
}
12+
}

src/Umbraco.Core/PropertyEditors/ValueConverters/JsonValueConverter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
namespace Umbraco.Core.PropertyEditors.ValueConverters
88
{
9+
/// <summary>
10+
/// The default converter for all property editors that expose a JSON value type
11+
/// </summary>
12+
/// <remarks>
13+
/// Since this is a default (umbraco) converter it will be ignored if another converter found conflicts with this one.
14+
/// </remarks>
15+
[DefaultPropertyValueConverter]
916
[PropertyValueType(typeof(JToken))]
1017
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
1118
public class JsonValueConverter : PropertyValueConverterBase

src/Umbraco.Core/Umbraco.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@
331331
<Compile Include="Models\TaggableObjectTypes.cs" />
332332
<Compile Include="Models\TemplateNode.cs" />
333333
<Compile Include="Packaging\PackageBinaryInspector.cs" />
334+
<Compile Include="PropertyEditors\DefaultPropertyValueConverterAttribute.cs" />
334335
<Compile Include="PropertyEditors\IValueEditor.cs" />
335336
<Compile Include="PropertyEditors\PropertyCacheValue.cs" />
336337
<Compile Include="PropertyEditors\PropertyValueCacheAttribute.cs" />

0 commit comments

Comments
 (0)