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

Unpublishing and editing content item with an uninitialized ContentPickerField breaks (Lombiq Technologies: OCORE-92) #11528

Merged
merged 7 commits into from
Apr 28, 2022
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
Copy link
Contributor

@Skrypt Skrypt Apr 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some thought here. Could we do the same but without using Newtonsoft.Json and replace it with System.Text.Json while we are at it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ContentItem.Content points to OrchardCore.ContentManagement.ContentElement.Data which is a Newtonsoft JObject. This class doesn't do any serializing, just access this existing property so trying to use System.Text.Json here would require conversion. Better table it until ContentElement itself is ported to STJ, I think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, forget it for now then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will Linq be concern here for allocation and performance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We pass on the enumerables without using an intermediary list like before, so that should improve allocation. I'm not sure about the performance, but we don't do anything complex so it should be fine. Also these providers are only called when you update a content item's field; that's relatively rare so you shouldn't be concerned.


namespace OrchardCore.ContentManagement.Metadata.Models;

public static class ContentPartFieldDefinitionExtensions
{
/// <summary>
/// Returns the value of the defined content field from the <paramref name="contentItem"/>.
/// </summary>
public static TField GetContentField<TField>(
this ContentPartFieldDefinition fieldDefinition,
ContentItem contentItem)
where TField : ContentField
{
if (contentItem.Content[fieldDefinition.PartDefinition.Name] is not JObject jPart ||
jPart[fieldDefinition.Name] is not JObject jField)
{
return null;
}

return jField.ToObject<TField>();
}

/// <summary>
/// Returns each field from <paramref name="fieldDefinitions"/> that exists in <paramref name="contentItem"/> in a
/// tuple along with its <see cref="ContentPartFieldDefinition"/>.
/// </summary>
public static IEnumerable<(ContentPartFieldDefinition Definition, TField Field)> GetContentFields<TField>(
this IEnumerable<ContentPartFieldDefinition> fieldDefinitions,
ContentItem contentItem)
where TField : ContentField
{
foreach (var fieldDefinition in fieldDefinitions)
{
var field = fieldDefinition.GetContentField<TField>(contentItem);
if (field is not null) yield return (fieldDefinition, field);
Skrypt marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using YesSql.Indexes;

namespace OrchardCore.ContentFields.Indexing.SQL
Expand All @@ -18,7 +18,7 @@ public class BooleanFieldIndex : ContentFieldIndex
public class BooleanFieldIndexProvider : ContentFieldIndexProvider
{
private readonly IServiceProvider _serviceProvider;
private readonly HashSet<string> _ignoredTypes = new HashSet<string>();
private readonly HashSet<string> _ignoredTypes = new();
private IContentDefinitionManager _contentDefinitionManager;

public BooleanFieldIndexProvider(IServiceProvider serviceProvider)
Expand Down Expand Up @@ -67,40 +67,20 @@ public override void Describe(DescribeContext<ContentItem> context)
return null;
}

var results = new List<BooleanFieldIndex>();

foreach (var fieldDefinition in fieldDefinitions)
{
var jPart = (JObject)contentItem.Content[fieldDefinition.PartDefinition.Name];

if (jPart == null)
{
continue;
}

var jField = (JObject)jPart[fieldDefinition.Name];

if (jField == null)
{
continue;
}

var field = jField.ToObject<BooleanField>();

results.Add(new BooleanFieldIndex
{
Latest = contentItem.Latest,
Published = contentItem.Published,
ContentItemId = contentItem.ContentItemId,
ContentItemVersionId = contentItem.ContentItemVersionId,
ContentType = contentItem.ContentType,
ContentPart = fieldDefinition.PartDefinition.Name,
ContentField = fieldDefinition.Name,
Boolean = field.Value
});
}

return results;
return fieldDefinitions
.GetContentFields<BooleanField>(contentItem)
.Select(pair =>
new BooleanFieldIndex
{
Latest = contentItem.Latest,
Published = contentItem.Published,
ContentItemId = contentItem.ContentItemId,
ContentItemVersionId = contentItem.ContentItemVersionId,
ContentType = contentItem.ContentType,
ContentPart = pair.Definition.PartDefinition.Name,
ContentField = pair.Definition.Name,
Boolean = pair.Field.Value,
});
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using YesSql.Indexes;

namespace OrchardCore.ContentFields.Indexing.SQL
Expand All @@ -18,7 +18,7 @@ public class ContentPickerFieldIndex : ContentFieldIndex
public class ContentPickerFieldIndexProvider : ContentFieldIndexProvider
{
private readonly IServiceProvider _serviceProvider;
private readonly HashSet<string> _ignoredTypes = new HashSet<string>();
private readonly HashSet<string> _ignoredTypes = new();
private IContentDefinitionManager _contentDefinitionManager;

public ContentPickerFieldIndexProvider(IServiceProvider serviceProvider)
Expand Down Expand Up @@ -67,44 +67,23 @@ public override void Describe(DescribeContext<ContentItem> context)
return null;
}

var results = new List<ContentPickerFieldIndex>();

// Get all field values
foreach (var fieldDefinition in fieldDefinitions)
{
var jPart = (JObject)contentItem.Content[fieldDefinition.PartDefinition.Name];

if (jPart == null)
{
continue;
}

var jField = (JObject)jPart[fieldDefinition.Name];

if (jField == null)
{
continue;
}

var field = jField.ToObject<ContentPickerField>();

foreach (var contentItemId in field.ContentItemIds)
{
results.Add(new ContentPickerFieldIndex
return fieldDefinitions
.GetContentFields<ContentPickerField>(contentItem)
.SelectMany(pair =>
pair.Field.ContentItemIds.Select(id => (pair.Definition, ContentItemId: id)))
.Select(pair =>
new ContentPickerFieldIndex
{
Latest = contentItem.Latest,
Published = contentItem.Published,
ContentItemId = contentItem.ContentItemId,
ContentItemVersionId = contentItem.ContentItemVersionId,
ContentType = contentItem.ContentType,
ContentPart = fieldDefinition.PartDefinition.Name,
ContentField = fieldDefinition.Name,
SelectedContentItemId = contentItemId
ContentPart = pair.Definition.PartDefinition.Name,
ContentField = pair.Definition.Name,
SelectedContentItemId = pair.ContentItemId,
});
}
}

return results;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using YesSql.Indexes;

namespace OrchardCore.ContentFields.Indexing.SQL
Expand All @@ -18,7 +18,7 @@ public class DateFieldIndex : ContentFieldIndex
public class DateFieldIndexProvider : ContentFieldIndexProvider
{
private readonly IServiceProvider _serviceProvider;
private readonly HashSet<string> _ignoredTypes = new HashSet<string>();
private readonly HashSet<string> _ignoredTypes = new();
private IContentDefinitionManager _contentDefinitionManager;

public DateFieldIndexProvider(IServiceProvider serviceProvider)
Expand Down Expand Up @@ -67,40 +67,19 @@ public override void Describe(DescribeContext<ContentItem> context)
return null;
}

var results = new List<DateFieldIndex>();

foreach (var fieldDefinition in fieldDefinitions)
{
var jPart = (JObject)contentItem.Content[fieldDefinition.PartDefinition.Name];

if (jPart == null)
{
continue;
}

var jField = (JObject)jPart[fieldDefinition.Name];

if (jField == null)
{
continue;
}

var field = jField.ToObject<DateField>();

results.Add(new DateFieldIndex
return fieldDefinitions
.GetContentFields<DateField>(contentItem)
.Select(pair => new DateFieldIndex
{
Latest = contentItem.Latest,
Published = contentItem.Published,
ContentItemId = contentItem.ContentItemId,
ContentItemVersionId = contentItem.ContentItemVersionId,
ContentType = contentItem.ContentType,
ContentPart = fieldDefinition.PartDefinition.Name,
ContentField = fieldDefinition.Name,
Date = field.Value
ContentPart = pair.Definition.PartDefinition.Name,
ContentField = pair.Definition.Name,
Date = pair.Field.Value,
});
}

return results;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using YesSql.Indexes;

namespace OrchardCore.ContentFields.Indexing.SQL
Expand All @@ -18,7 +18,7 @@ public class DateTimeFieldIndex : ContentFieldIndex
public class DateTimeFieldIndexProvider : ContentFieldIndexProvider
{
private readonly IServiceProvider _serviceProvider;
private readonly HashSet<string> _ignoredTypes = new HashSet<string>();
private readonly HashSet<string> _ignoredTypes = new();
private IContentDefinitionManager _contentDefinitionManager;

public DateTimeFieldIndexProvider(IServiceProvider serviceProvider)
Expand Down Expand Up @@ -67,40 +67,20 @@ public override void Describe(DescribeContext<ContentItem> context)
return null;
}

var results = new List<DateTimeFieldIndex>();

foreach (var fieldDefinition in fieldDefinitions)
{
var jPart = (JObject)contentItem.Content[fieldDefinition.PartDefinition.Name];

if (jPart == null)
{
continue;
}

var jField = (JObject)jPart[fieldDefinition.Name];

if (jField == null)
{
continue;
}

var field = jField.ToObject<DateTimeField>();

results.Add(new DateTimeFieldIndex
{
Latest = contentItem.Latest,
Published = contentItem.Published,
ContentItemId = contentItem.ContentItemId,
ContentItemVersionId = contentItem.ContentItemVersionId,
ContentType = contentItem.ContentType,
ContentPart = fieldDefinition.PartDefinition.Name,
ContentField = fieldDefinition.Name,
DateTime = field.Value
});
}

return results;
return fieldDefinitions
.GetContentFields<DateTimeField>(contentItem)
.Select(pair =>
new DateTimeFieldIndex
{
Latest = contentItem.Latest,
Published = contentItem.Published,
ContentItemId = contentItem.ContentItemId,
ContentItemVersionId = contentItem.ContentItemVersionId,
ContentType = contentItem.ContentType,
ContentPart = pair.Definition.PartDefinition.Name,
ContentField = pair.Definition.Name,
DateTime = pair.Field.Value,
});
});
}
}
Expand Down
Loading