Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Neil 37 posts 201 karma points
    Feb 23, 2022 @ 09:55
    Neil
    0

    IPropertyValueConvertor value being discarded (returns null JToken)

    I've got a couple of custom Property editor that save data as JSON strings, these are then passed to a separate property value convertor to turn them from JSON to a Newtonsoft JObject, and then loop the object to build a custom Model.

    For one of these, the property passed to the view is the custom model I expect, the other just returns null, with the type as Newtonsoft.Json.Linq.JToken.

    Stepping through it in the debugger, the Property Value Convertor is running, building and returning the correct object, and passing the IsValue() checks... but at some point after this the result is discarded and replaced with the nulled JToken mentioned.

    The only real difference between the two convertors (asides from the logic to actually populate the object) is that the one returns a single instance of the Model (with GetPropertyValueType() returning "typeof(CustomModel)") and the one that doesn't work is returning an IEnumerable

    Has anyone else ever come across a similiar issue, and have any ideas on the next step for debugging?

    Simplified example of my IPropertyValueConverter code;

      public class CustomModelValueConvertor : IPropertyValueConverter
      {
        public Type GetPropertyValueType(IPublishedPropertyType propertyType) => typeof(IEnumerable<CustomModel>);
    
        public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) => PropertyCacheLevel.None; // Caching disabled while debugging.
    
        public bool IsConverter(IPublishedPropertyType propertyType) => propertyType.EditorAlias.Equals("CustomModelPropertyEditor");
    
        public bool? IsValue(object value, PropertyValueLevel level) => level switch
        {
          PropertyValueLevel.Source => null,
          PropertyValueLevel.Inter => null,
          PropertyValueLevel.Object => value is IEnumerable<CustomModel>,
          _ => throw new NotSupportedException($"Invalid level: {level}."),
        };
    
        public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview) => return JsonConvert.DeserializeObject(sourceString.ToString()); // try/catch logic removed for brevity.
    
        public object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) => inter?.ToString() ?? string.Empty;
    
        public object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
        {
          List<CustomModel> customModelList = new();
    
          if (inter is JArray instances)
          {
            for (int i = 0; i < instances.Count; ++i)
            {
              customModelList.Add(new CustomModel()); // JObject conversion removed for brevity.
            }
          }
    
          return customModelList; // At this point, customModelList is a correctly formatted List of CustomModels, with all the data expected.
        }
      }
    
  • Neil 37 posts 201 karma points
    Feb 23, 2022 @ 10:44
    Neil
    100

    I have found the cause of the issue, it was the Model builder caching data types.

    I noticed that the return type in CustomModel.generated.cs was still JToken, so removed the property editor from the Document Type, re-added it, and restarted IIS.

    Hope that helps anyone else with the same issue.

Please Sign in or register to post replies

Write your reply to:

Draft