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,36 @@
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 =>
contentItem.Content[fieldDefinition.PartDefinition.Name] is JObject jPart &&
jPart[fieldDefinition.Name] is JObject jField &&
jField.ToObject<TField>() is { } field
Copy link
Member

Choose a reason for hiding this comment

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

does is { } mean != null. If so then != null is easier to comprehend.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a combined operation for pattern matching introduced in C# 8. It does != null check and assignment to the field variable. So it makes the code more compact and lowers the cognitive complexity (once you are used to it anyway). Back in C# 7 we'd use jField.ToObject<TField>() is var field for the same, but starting C# 8 this empty object pattern is the norm.

If I replaced this with != null then it would look like this:

        jField.ToObject<TField>() != null
            ? jField.ToObject<TField>()
            : null;

I think that's less efficient and visually noisier. Please comment if you want C# 8 style, C# 7 style, or the old fashioned null check and explicit assignment.

Copy link
Member

Choose a reason for hiding this comment

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

and lowers the cognitive complexity (once you are used to it anyway)

Like pain, after a while you don't realize it's there, so pain isn't real actually.
We had a meeting today and nobody could explain what it does. Everyone could explain != null though.

Please keep the old style, and add more lines if necessary for readability or prevent two calls to ToObject.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not like pain, more like learning a new technique. First it's weird and awkward, but it you give it a chance you start seeing the utility in practice and it grows on you. Change is good.
(also we use this pattern matching in Lombiq everywhere)

But it's you guys' repo, so I abide by your decision and have rewrote the method.

Copy link
Contributor

Choose a reason for hiding this comment

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

May be you can use jField is not null it’s more readable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I have already eliminated the null check here, but I applied your suggestion to the null check in the other method in this file.

However this is starting to get a bit confusing. Does the OC repo have a style guide? I couldn't find it. If not, do you have plans to make one?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That links to ASP.Net Core's engineering guidelines. If you can edit that, it would be good to add something about null checking conventions based on this discussion. If not, an OC-specific guide would still be valuable to keep a record of such decisions. Just a thought.

Copy link
Contributor

Choose a reason for hiding this comment

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

No need to add documentation about this. We are just giving comments here based on our coding preferences because it is easier to read. Basically, adding documentation will not prevent this from happening so we give our advice on every pull request. Getting code merged into source code can take more time than we think at first. You will get used to it over time. Everyone has comments and suggestions, that is why pull requests exist.

? field
: null;

/// <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)
{
if (fieldDefinition.GetContentField<TField>(contentItem) is not { } field) continue;
yield return (fieldDefinition, field);
}
}
}
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