From de6150f77ed34f499dd1fa62caab2adb1838b983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Thu, 22 Feb 2024 14:09:41 +0100 Subject: [PATCH 01/31] Update NuGet versions to pre-release. --- .../Lombiq.HelpfulLibraries.AspNetCore.csproj | 4 -- .../Lombiq.HelpfulLibraries.LinqToDb.csproj | 2 +- ...Lombiq.HelpfulLibraries.OrchardCore.csproj | 38 +++++++++---------- .../Lombiq.HelpfulLibraries.Samples.csproj | 12 +++--- 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/Lombiq.HelpfulLibraries.AspNetCore/Lombiq.HelpfulLibraries.AspNetCore.csproj b/Lombiq.HelpfulLibraries.AspNetCore/Lombiq.HelpfulLibraries.AspNetCore.csproj index 1c1bc970..3e646d01 100644 --- a/Lombiq.HelpfulLibraries.AspNetCore/Lombiq.HelpfulLibraries.AspNetCore.csproj +++ b/Lombiq.HelpfulLibraries.AspNetCore/Lombiq.HelpfulLibraries.AspNetCore.csproj @@ -27,10 +27,6 @@ - - - - diff --git a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj index 9438c489..6d77c4de 100644 --- a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj +++ b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj @@ -25,6 +25,6 @@ - + diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj index 2ef95736..b8bd4b50 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj +++ b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj @@ -24,27 +24,27 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - + + + + diff --git a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj index cf3eae78..36b19a6c 100644 --- a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj +++ b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj @@ -12,12 +12,12 @@ - - - - - - + + + + + + From 765156a23b2a48d89f1a0e83d1dc9e571f5b1dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Thu, 22 Feb 2024 14:34:40 +0100 Subject: [PATCH 02/31] Prevent package downgrade. --- .../Lombiq.HelpfulLibraries.OrchardCore.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj index b8bd4b50..c2b92f21 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj +++ b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj @@ -39,7 +39,7 @@ - + From c417dc2607da6899a5d5d9f4561d2c6ad00e2c6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Thu, 22 Feb 2024 14:42:20 +0100 Subject: [PATCH 03/31] Fix obsolete code. --- .../GraphQL/TotalOfContentTypeBuilder.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/GraphQL/TotalOfContentTypeBuilder.cs b/Lombiq.HelpfulLibraries.OrchardCore/GraphQL/TotalOfContentTypeBuilder.cs index 03f824d8..58df67a9 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/GraphQL/TotalOfContentTypeBuilder.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/GraphQL/TotalOfContentTypeBuilder.cs @@ -5,6 +5,7 @@ using OrchardCore.ContentManagement.GraphQL.Queries.Types; using OrchardCore.ContentManagement.Metadata.Models; using OrchardCore.ContentManagement.Records; +using System; using YesSql; namespace Lombiq.HelpfulLibraries.OrchardCore.GraphQL; @@ -30,14 +31,15 @@ public void Build(FieldType contentQuery, ContentTypeDefinition contentTypeDefin { var name = contentTypeDefinition.Name; - var builder = contentItemType.Field() - .Name("totalOfContentType") + var builder = contentItemType + .Field("totalOfContentType") .Description(S["Gets the total count of all published content items with the type {0}.", name]); builder.ResolveAsync(async context => { - var serviceProvider = context.RequestServices; - var session = serviceProvider.GetService(); + var session = context.RequestServices?.GetService() ?? throw new InvalidOperationException( + $"Couldn't retrieve {nameof(ISession)} service from {nameof(context)} of \"{name}\"."); + return await session.QueryIndex(index => index.Published && index.Latest && From 59055e5dc988c39f06e015fda31bdb4b745c5a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Fri, 23 Feb 2024 20:24:04 +0100 Subject: [PATCH 04/31] Fix compilation errors. --- .../Utilities/ICopier.cs | 14 ++++++++++++++ .../ContentPartDefinitionBuilderExtensions.cs | 10 ++++++++++ 2 files changed, 24 insertions(+) create mode 100644 Lombiq.HelpfulLibraries.Common/Utilities/ICopier.cs diff --git a/Lombiq.HelpfulLibraries.Common/Utilities/ICopier.cs b/Lombiq.HelpfulLibraries.Common/Utilities/ICopier.cs new file mode 100644 index 00000000..1449e447 --- /dev/null +++ b/Lombiq.HelpfulLibraries.Common/Utilities/ICopier.cs @@ -0,0 +1,14 @@ +namespace Lombiq.HelpfulLibraries.Common.Utilities; + +/// +/// Types implementing this interface can copy their values to a object. +/// +/// The type of the object to copy to. +public interface ICopier +{ + /// + /// Copies the applicable contents of the current instance into , overwriting its original + /// values. + /// + void CopyTo(TTarget target); +} diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentPartDefinitionBuilderExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentPartDefinitionBuilderExtensions.cs index 0fcd125f..5da2efcc 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentPartDefinitionBuilderExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentPartDefinitionBuilderExtensions.cs @@ -1,6 +1,9 @@ +using Lombiq.HelpfulLibraries.Common.Utilities; +using OrchardCore.ContentManagement.Metadata.Models; using OrchardCore.ContentManagement.Metadata.Settings; using System; using System.Linq.Expressions; +using System.Text.Json.Nodes; namespace OrchardCore.ContentManagement.Metadata.Builders; @@ -18,6 +21,13 @@ public static ContentPartDefinitionBuilder AsPart(this ContentPart /// public static ContentPartFieldDefinitionBuilder WithEditor(this ContentPartFieldDefinitionBuilder builder, Enum editor) => builder.MergeSettings(x => x.Editor = editor.ToString()); + + public static void CopySettingsTo(this ContentPartFieldDefinition definition, T target) + where T : class, ICopier + { + var settings = definition.Settings.ToObject(); + settings.CopyTo(target); + } } public class ContentPartDefinitionBuilder From 768d11834a624ec30059e7ec15e478c9f40cb0f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sat, 24 Feb 2024 02:05:24 +0100 Subject: [PATCH 05/31] Remove "using Newtonsoft.Json" --- .../Extensions/JsonStringExtensions.cs | 5 +- .../LocalizedHtmlStringConverter.cs | 39 +++--- .../LocalizedHtmlStringExtensions.cs | 9 +- .../Docs/Extensions.md | 2 +- .../Extensions/JsonExtensions.cs | 12 +- .../Lombiq.HelpfulLibraries.Common.csproj | 1 - .../Utilities/JsonHelpers.cs | 111 ++++++++++-------- .../Contents/ContentExtensions.cs | 13 +- .../Contents/JsonSectionDisplayDriver.cs | 6 +- .../Contents/TaxonomyHelper.cs | 3 +- .../Data/ManualConnectingIndexService.cs | 4 +- .../Mvc/OrchardControllerExtensions.cs | 4 +- .../Mvc/TypedRoute.cs | 4 +- .../Helpers/RefitHelper.cs | 12 +- .../Lombiq.HelpfulLibraries.Refit.csproj | 1 - 15 files changed, 119 insertions(+), 107 deletions(-) diff --git a/Lombiq.HelpfulLibraries.AspNetCore/Extensions/JsonStringExtensions.cs b/Lombiq.HelpfulLibraries.AspNetCore/Extensions/JsonStringExtensions.cs index 25ae413e..e7f60292 100644 --- a/Lombiq.HelpfulLibraries.AspNetCore/Extensions/JsonStringExtensions.cs +++ b/Lombiq.HelpfulLibraries.AspNetCore/Extensions/JsonStringExtensions.cs @@ -1,5 +1,5 @@ using Microsoft.AspNetCore.Html; -using Newtonsoft.Json; +using System.Text.Json; namespace System; @@ -10,6 +10,5 @@ public static class JsonStringExtensions /// tags in a Razor view. /// public static IHtmlContent JsonHtmlContent(this string htmlString) => - new HtmlString(JsonConvert.SerializeObject( - htmlString, new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.EscapeHtml })); + new HtmlString(JsonSerializer.Serialize(htmlString)); } diff --git a/Lombiq.HelpfulLibraries.AspNetCore/Localization/LocalizedHtmlStringConverter.cs b/Lombiq.HelpfulLibraries.AspNetCore/Localization/LocalizedHtmlStringConverter.cs index 51923962..1cd4d740 100644 --- a/Lombiq.HelpfulLibraries.AspNetCore/Localization/LocalizedHtmlStringConverter.cs +++ b/Lombiq.HelpfulLibraries.AspNetCore/Localization/LocalizedHtmlStringConverter.cs @@ -1,49 +1,40 @@ using Microsoft.AspNetCore.Mvc.Localization; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; namespace Lombiq.HelpfulLibraries.AspNetCore.Localization; public class LocalizedHtmlStringConverter : JsonConverter { - public override void WriteJson(JsonWriter writer, LocalizedHtmlString value, JsonSerializer serializer) + public override void Write(Utf8JsonWriter writer, LocalizedHtmlString value, JsonSerializerOptions options) { writer.WriteStartObject(); - writer.WritePropertyName(nameof(LocalizedHtmlString.Name)); - writer.WriteValue(value.Name); - - writer.WritePropertyName(nameof(LocalizedHtmlString.Value)); - writer.WriteValue(value.Html()); - - writer.WritePropertyName(nameof(LocalizedHtmlString.IsResourceNotFound)); - writer.WriteValue(value.IsResourceNotFound); + writer.WriteString(nameof(LocalizedHtmlString.Name), value.Name); + writer.WriteString(nameof(LocalizedHtmlString.Value), value.Html()); + writer.WriteBoolean(nameof(LocalizedHtmlString.IsResourceNotFound), value.IsResourceNotFound); writer.WriteEndObject(); } - public override LocalizedHtmlString ReadJson( - JsonReader reader, - Type objectType, - LocalizedHtmlString existingValue, - bool hasExistingValue, - JsonSerializer serializer) + public override LocalizedHtmlString Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - var token = JToken.Load(reader); + var token = JsonNode.Parse(ref reader); - if (token.Type == JTokenType.String) + if (token is JsonValue jsonValue) { - var text = token.Value(); + var text = jsonValue.GetValue(); return new LocalizedHtmlString(text, text); } - if (token is JObject jObject) + if (token is JsonObject jsonObject) { - var name = jObject.GetMaybe(nameof(LocalizedHtmlString.Name))?.ToObject(); - var value = jObject.GetMaybe(nameof(LocalizedHtmlString.Value))?.ToObject() ?? name; - var isResourceNotFound = jObject.GetMaybe(nameof(LocalizedHtmlString.IsResourceNotFound))?.ToObject(); + var name = jsonObject[nameof(LocalizedHtmlString.Name)]?.Deserialize(); + var value = jsonObject.GetMaybe(nameof(LocalizedHtmlString.Value))?.Deserialize() ?? name; + var isResourceNotFound = jsonObject.GetMaybe(nameof(LocalizedHtmlString.IsResourceNotFound))?.Deserialize(); name ??= value; if (string.IsNullOrEmpty(name)) throw new InvalidOperationException("Missing name."); diff --git a/Lombiq.HelpfulLibraries.AspNetCore/Localization/LocalizedHtmlStringExtensions.cs b/Lombiq.HelpfulLibraries.AspNetCore/Localization/LocalizedHtmlStringExtensions.cs index ea482ee7..8d56ca91 100644 --- a/Lombiq.HelpfulLibraries.AspNetCore/Localization/LocalizedHtmlStringExtensions.cs +++ b/Lombiq.HelpfulLibraries.AspNetCore/Localization/LocalizedHtmlStringExtensions.cs @@ -1,9 +1,9 @@ using Microsoft.AspNetCore.Html; -using Newtonsoft.Json; using System.IO; using System.Linq; using System.Text; using System.Text.Encodings.Web; +using System.Text.Json; namespace Microsoft.AspNetCore.Mvc.Localization; @@ -15,9 +15,7 @@ public static class LocalizedHtmlStringExtensions /// public static IHtmlContent Json(this LocalizedHtmlString htmlString) => htmlString?.Html() is { } html - ? new HtmlString(JsonConvert.SerializeObject( - html, - new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.EscapeHtml })) + ? new HtmlString(JsonSerializer.Serialize(html)) : new HtmlString("null"); /// @@ -47,6 +45,9 @@ public static LocalizedHtmlString Concat(this LocalizedHtmlString first, params return new LocalizedHtmlString(html, html); } + /// + /// Concatenates the with the provided in-between. + /// public static LocalizedHtmlString Join(this IHtmlContent separator, params LocalizedHtmlString[] items) { if (items.Length == 0) return null; diff --git a/Lombiq.HelpfulLibraries.Common/Docs/Extensions.md b/Lombiq.HelpfulLibraries.Common/Docs/Extensions.md index 50120275..2d5eb37d 100644 --- a/Lombiq.HelpfulLibraries.Common/Docs/Extensions.md +++ b/Lombiq.HelpfulLibraries.Common/Docs/Extensions.md @@ -10,7 +10,7 @@ - `ExpressionExtensions`: Adds `System.Linq.Expressions`. For example, `StripResult()` turns a `Func` expression into an `Action` one. - `HttpContextExtensions`: Some shortcuts for managing cookies. - `IoExtensions`: Adds extensions for `String.IO` types. For example, `TextWriter.WriteLineInvariant()` writes interpolated string in a culture invariant manner. -- `JsonExtensions`: Adds extensions for `Newtonsoft.Json.Linq` types. For example, `JObject.TryParse(out var result)` attempts to convert the JSON object into a C# object. +- `JsonExtensions`: Adds extensions for `System.Text.Json.Nodes` types. For example, `JsonObject.TryParse(out var result)` attempts to convert the JSON object into a C# object. - `MemoryCacheExtensions`: Adds extensions for `IMemoryCache` manipulation. For example, `GetOrNew()` type-safely returns the item or creates a new instance. - `MulticastDelegateExtensions`: Extensions for `MulticastDelegate`s, e.g. to invoke async delegates in a safe fashion. - `NumberExtensions`: Adds extensions for primitive numeric types. For example, `ToTechnicalString()` converts `int` into culture invariant `string`. diff --git a/Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs b/Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs index cee75516..05852bbd 100644 --- a/Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs +++ b/Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs @@ -1,12 +1,18 @@ -namespace Newtonsoft.Json.Linq; +using System.Text.Json; +using System.Text.Json.Nodes; public static class JsonExtensions { - public static bool TryParse(this JObject jObject, out T result) + /// + /// Attempts to parse the provided . If successful, returns and + /// contains the serialized object. Otherwise returns and + /// contains of . + /// + public static bool TryParse(this JsonNode jsonNode, out T result) { try { - result = jObject.ToObject(); + result = jsonNode.Deserialize(); return true; } catch diff --git a/Lombiq.HelpfulLibraries.Common/Lombiq.HelpfulLibraries.Common.csproj b/Lombiq.HelpfulLibraries.Common/Lombiq.HelpfulLibraries.Common.csproj index 4f0144b8..67856e7f 100644 --- a/Lombiq.HelpfulLibraries.Common/Lombiq.HelpfulLibraries.Common.csproj +++ b/Lombiq.HelpfulLibraries.Common/Lombiq.HelpfulLibraries.Common.csproj @@ -26,7 +26,6 @@ - diff --git a/Lombiq.HelpfulLibraries.Common/Utilities/JsonHelpers.cs b/Lombiq.HelpfulLibraries.Common/Utilities/JsonHelpers.cs index 9d3dc7dc..d26a0921 100644 --- a/Lombiq.HelpfulLibraries.Common/Utilities/JsonHelpers.cs +++ b/Lombiq.HelpfulLibraries.Common/Utilities/JsonHelpers.cs @@ -1,6 +1,7 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Nodes; using System.Threading.Tasks; namespace Lombiq.HelpfulLibraries.Common.Utilities; @@ -11,84 +12,90 @@ public static class JsonHelpers /// Attempts to validate a string that contains JSON by parsing it. /// /// - /// if string is empty or parsing was successful, otherwise. + /// if the string is null or empty, if parsing was successful, + /// otherwise. /// - public static bool ValidateJsonIfNotNull(string json) + public static bool? ValidateJsonIfNotNull(string json) { - if (!string.IsNullOrEmpty(json)) + if (string.IsNullOrEmpty(json)) return null; + return TryParse(json, out _); + } + + /// + /// Attempts to validate a string that contains JSON by parsing it. + /// + /// + /// if parsing was successful, otherwise. + /// + public static bool TryParse(string json, out JsonNode result) + { + try { - try - { - JObject.Parse(json); - return true; - } - catch (JsonReaderException) - { - return false; - } + result = JsonNode.Parse(json); + return true; + } + catch (JsonException) + { + result = default; + return false; } - - return true; } /// - /// Alters a by iterating through all their inner JObject nodes deeply and executing the - /// provided alter operation on it. + /// Walks through the properties of recursively and invokes + /// on each property with an object value. /// - /// to alter. - /// Operation that alters a deep node. - /// Name of the deep node. - public static void AlterDeep(JObject jObject, Action alter, string propertyName = null) + /// + /// The action to be invoked. The first parameter is the current property name, the second is its value. + /// + public static void AlterDeep(JsonObject jsonObject, Action alter) => + AlterDeep(jsonObject, alter, propertyName: null); + + private static void AlterDeep(JsonObject jsonObject, Action alter, string propertyName) { - alter(propertyName, jObject); + if (propertyName != null) alter(propertyName, jsonObject); - foreach (var (key, value) in jObject) + foreach (var (key, value) in jsonObject) { - if (value is JObject innerObject) + if (value is JsonObject innerObject) { AlterDeep(innerObject, alter, key); } - - if (value is JArray innerArray) + else if (value is JsonArray innerArray) { - foreach (var innerToken in innerArray) - { - if (innerToken is JObject innerTokenObject) - { - AlterDeep(innerTokenObject, alter, key); - } - } + innerArray + .CastWhere() + .ForEach(innerTokenObject => AlterDeep(innerTokenObject, alter, key)); } } } /// - /// Alters a by iterating through all their inner JObject nodes deeply and executing the - /// provided asynchronous alter operation on it. + /// Walks through the properties of recursively and invokes on each property with an object value. /// - /// to alter. - /// Async operation that alters a deep node. - /// Name of the deep node. - public static async Task AlterDeepAsync(JObject jObject, Func alterAsync, string propertyName = null) + /// + /// The asynchronous operation to be invoked. The first parameter is the current property name, the second is its + /// value. + /// + public static Task AlterDeepAsync(JsonObject jsonObject, Func alterAsync) => + AlterDeepAsync(jsonObject, alterAsync, propertyName: null); + + public static async Task AlterDeepAsync(JsonObject jsonObject, Func alterAsync, string propertyName) { - await alterAsync(propertyName, jObject); + if (propertyName != null) await alterAsync(propertyName, jsonObject); - foreach (var (key, value) in jObject) + foreach (var (key, value) in jsonObject) { - if (value is JObject innerObject) + if (value is JsonObject innerObject) { await AlterDeepAsync(innerObject, alterAsync, key); } - - if (value is JArray innerArray) + else if (value is JsonArray innerArray) { - foreach (var innerToken in innerArray) - { - if (innerToken is JObject innerTokenObject) - { - await AlterDeepAsync(innerTokenObject, alterAsync, key); - } - } + await innerArray + .CastWhere() + .AwaitEachAsync(innerTokenObject => AlterDeepAsync(innerTokenObject, alterAsync, key)); } } } diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs index 725b34e1..f5618256 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs @@ -1,8 +1,10 @@ using Lombiq.HelpfulLibraries.OrchardCore.Contents; -using Newtonsoft.Json.Linq; using OrchardCore.Alias.Models; using OrchardCore.ContentManagement.Records; using System; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Settings; using System.Threading.Tasks; using YesSql; @@ -173,4 +175,13 @@ public static bool IsNew(this IContent content) => !content.ContentItem.Latest && !content.ContentItem.Published && content.ContentItem.Id == 0; + + /// + /// Deserializes the content item's JSON property. + /// + public static T GetProperty(this ContentItem contentItem, string name) + { + var data = (JsonObject)contentItem.Content; + return data[name].Deserialize(); + } } diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Contents/JsonSectionDisplayDriver.cs b/Lombiq.HelpfulLibraries.OrchardCore/Contents/JsonSectionDisplayDriver.cs index 4fdc6ddd..f6ed2002 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Contents/JsonSectionDisplayDriver.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Contents/JsonSectionDisplayDriver.cs @@ -1,12 +1,12 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; -using Newtonsoft.Json; using OrchardCore.DisplayManagement.Entities; using OrchardCore.DisplayManagement.Handlers; using OrchardCore.DisplayManagement.Views; using OrchardCore.Security.Permissions; using OrchardCore.Settings; +using System.Text.Json; using System.Threading.Tasks; namespace Lombiq.HelpfulLibraries.OrchardCore.Contents; @@ -36,7 +36,7 @@ await AuthorizeAsync() ShapeType, async settings => { - settings.Json = JsonConvert.SerializeObject(section); + settings.Json = JsonSerializer.Serialize(section); settings.AdditionalData = await GetAdditionalDataAsync(section, context); }) .Location(Location) @@ -76,7 +76,7 @@ private static bool TryParseJson(string json, out TSection result) { if (string.IsNullOrEmpty(json)) return false; - result = JsonConvert.DeserializeObject(json); + result = JsonSerializer.Deserialize(json); return true; } catch diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Contents/TaxonomyHelper.cs b/Lombiq.HelpfulLibraries.OrchardCore/Contents/TaxonomyHelper.cs index 1fa131f8..25ba69b6 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Contents/TaxonomyHelper.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Contents/TaxonomyHelper.cs @@ -1,4 +1,3 @@ -using Newtonsoft.Json.Linq; using OrchardCore.ContentManagement; using OrchardCore.Taxonomies.Models; using System.Collections.Generic; @@ -40,7 +39,7 @@ public static IList GetAllChildren(ContentItem contentItem, bool in if (includeSelf) results.Add(contentItem); var partTerms = contentItem.As()?.Terms ?? Enumerable.Empty(); - var itemTerms = (contentItem.Content.Terms as JArray)?.ToObject>() ?? Enumerable.Empty(); + var itemTerms = contentItem.GetProperty>(nameof(TaxonomyPart.Terms)) ?? Enumerable.Empty(); foreach (var child in partTerms.Concat(itemTerms)) { results.AddRange(GetAllChildren(child, includeSelf: true)); diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Data/ManualConnectingIndexService.cs b/Lombiq.HelpfulLibraries.OrchardCore/Data/ManualConnectingIndexService.cs index a1fb30bb..92dbf4a8 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Data/ManualConnectingIndexService.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Data/ManualConnectingIndexService.cs @@ -1,12 +1,12 @@ using Dapper; using Microsoft.Extensions.Logging; -using Newtonsoft.Json; using OrchardCore.Data; using System; using System.Collections.Generic; using System.Data.Common; using System.Linq; using System.Reflection; +using System.Text.Json; using System.Threading.Tasks; using YesSql; using YesSql.Indexes; @@ -59,7 +59,7 @@ public Task AddAsync(T item, ISession session, long? setDocumentId = null) => _logger.LogError( "Failed to execute the following SQL query:\n{Sql}\nArguments:\n{Item}", sql, - JsonConvert.SerializeObject(item)); + JsonSerializer.Serialize(item)); throw; } }); diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/OrchardControllerExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/OrchardControllerExtensions.cs index 9cb02bb2..96dc9e01 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/OrchardControllerExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/OrchardControllerExtensions.cs @@ -3,9 +3,9 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; -using Newtonsoft.Json; using OrchardCore.ContentManagement; using System; +using System.Text.Json; using System.Threading.Tasks; namespace Microsoft.AspNetCore.Mvc; @@ -48,7 +48,7 @@ public static async Task SafeJsonAsync(this Controller controller logger.LogError( exception, "An error has occurred while generating a JSON result. (Request Route Values: {RouteValues})", - JsonConvert.SerializeObject(context.Request.RouteValues)); + JsonSerializer.Serialize(context.Request.RouteValues)); return controller.Json(context.IsDevelopmentAndLocalhost() ? new { error = exception.Message, data = exception.ToString() } diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs index 300c02a5..0e362ff3 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs @@ -4,7 +4,6 @@ using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; -using Newtonsoft.Json; using OrchardCore.Admin; using OrchardCore.Environment.Extensions; using OrchardCore.Modules.Manifest; @@ -17,6 +16,7 @@ using System.Linq.Expressions; using System.Net; using System.Reflection; +using System.Text.Json; namespace Lombiq.HelpfulLibraries.OrchardCore.Mvc; @@ -238,6 +238,6 @@ private static string ValueToString(object value) => DateTime date => date.ToString("s", CultureInfo.InvariantCulture), byte or sbyte or short or ushort or int or uint or long or ulong or float or double or decimal => string.Format(CultureInfo.InvariantCulture, "{0}", value), - _ => JsonConvert.SerializeObject(value), + _ => JsonSerializer.Serialize(value), }; } diff --git a/Lombiq.HelpfulLibraries.Refit/Helpers/RefitHelper.cs b/Lombiq.HelpfulLibraries.Refit/Helpers/RefitHelper.cs index ba356616..e49d362d 100644 --- a/Lombiq.HelpfulLibraries.Refit/Helpers/RefitHelper.cs +++ b/Lombiq.HelpfulLibraries.Refit/Helpers/RefitHelper.cs @@ -14,10 +14,12 @@ public static class RefitHelper /// Optional action for configuring other settings. /// Interface to create the implementation for. /// An instance that implements . + [Obsolete("As of Orchard Core 1.9 Newtonsoft.Json is no longer supported.")] public static T WithNewtonsoftJson(string hostUrl, Action configure = null) => WithNewtonsoftJson(new Uri(hostUrl), configure); /// + [Obsolete("As of Orchard Core 1.9 Newtonsoft.Json is no longer supported.")] public static T WithNewtonsoftJson(Uri hostUrl, Action configure = null) => RestService.For(hostUrl.AbsoluteUri, CreateSettingsWithNewtonsoftJson(configure)); @@ -29,13 +31,11 @@ public static T WithNewtonsoftJson(Uri hostUrl, Action configu /// Optional action for configuring other settings. /// Interface to create the implementation for. /// An instance that implements . + [Obsolete("As of Orchard Core 1.9 Newtonsoft.Json is no longer supported.")] public static T WithNewtonsoftJson(HttpClient httpClient, Action configure = null) => RestService.For(httpClient, CreateSettingsWithNewtonsoftJson(configure)); - private static RefitSettings CreateSettingsWithNewtonsoftJson(Action configure) - { - var settings = new RefitSettings(new NewtonsoftJsonContentSerializer()); - configure?.Invoke(settings); - return settings; - } + [Obsolete("As of Orchard Core 1.9 Newtonsoft.Json is no longer supported.")] + private static RefitSettings CreateSettingsWithNewtonsoftJson(Action configure) => + throw new NotSupportedException("As of Orchard Core 1.9 Newtonsoft.Json is no longer supported."); } diff --git a/Lombiq.HelpfulLibraries.Refit/Lombiq.HelpfulLibraries.Refit.csproj b/Lombiq.HelpfulLibraries.Refit/Lombiq.HelpfulLibraries.Refit.csproj index d088c7ed..d0373f7a 100644 --- a/Lombiq.HelpfulLibraries.Refit/Lombiq.HelpfulLibraries.Refit.csproj +++ b/Lombiq.HelpfulLibraries.Refit/Lombiq.HelpfulLibraries.Refit.csproj @@ -26,7 +26,6 @@ - From 84edd2e884a05652905d0e5a5120c2d9ea987b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sat, 24 Feb 2024 14:46:08 +0100 Subject: [PATCH 06/31] Let GetProperty resolve any JSON path. --- .../Contents/ContentExtensions.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs index f5618256..0a3120d4 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs @@ -177,11 +177,12 @@ public static bool IsNew(this IContent content) => content.ContentItem.Id == 0; /// - /// Deserializes the content item's JSON property. + /// Deserializes the 's first JSON node that matches . /// - public static T GetProperty(this ContentItem contentItem, string name) + public static T GetProperty(this ContentElement contentElement, string path) + where T : class { - var data = (JsonObject)contentItem.Content; - return data[name].Deserialize(); + var data = (JsonObject)contentElement.Content; + return data.SelectNode(path)?.Deserialize(); } } From bfbc67637666d26aae7afddac23f74a0dcacf124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sun, 25 Feb 2024 13:48:14 +0100 Subject: [PATCH 07/31] Add missing namespace. --- Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs b/Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs index 05852bbd..fb73f5f3 100644 --- a/Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs +++ b/Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs @@ -1,5 +1,6 @@ -using System.Text.Json; -using System.Text.Json.Nodes; +using System.Text.Json.Nodes; + +namespace System.Text.Json; public static class JsonExtensions { From bb837a0627d936e62e18c33d71879be3a41fc985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sun, 25 Feb 2024 16:12:24 +0100 Subject: [PATCH 08/31] Add ToComparable extension method. --- .../Extensions/JsonExtensions.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs b/Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs index fb73f5f3..0d812e4b 100644 --- a/Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs +++ b/Lombiq.HelpfulLibraries.Common/Extensions/JsonExtensions.cs @@ -22,4 +22,21 @@ public static bool TryParse(this JsonNode jsonNode, out T result) return false; } } + + /// + /// Converts the provided into the appropriate primitive types of , + /// , or . If the is + /// array or object then it's serialized into JSON . + /// + public static IComparable ToComparable(this JsonNode node) => + node.GetValueKind() switch + { + JsonValueKind.String => node.GetValue(), + JsonValueKind.Number => node.GetValue(), + JsonValueKind.True => true, + JsonValueKind.False => false, + JsonValueKind.Object => node.ToString(), + JsonValueKind.Array => node.ToString(), + _ => null, + }; } From 0a004fa909578313cf6cdcb68cabda6fd891aca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Mon, 4 Mar 2024 00:22:13 +0100 Subject: [PATCH 09/31] Update OC to latest (because of bug fix for WorkflowTypeStep) --- .../Lombiq.HelpfulLibraries.LinqToDb.csproj | 2 +- ...Lombiq.HelpfulLibraries.OrchardCore.csproj | 38 +++++++++---------- .../Lombiq.HelpfulLibraries.Samples.csproj | 12 +++--- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj index 6d77c4de..7da8beb0 100644 --- a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj +++ b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj @@ -25,6 +25,6 @@ - + diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj index c2b92f21..cebc32d2 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj +++ b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj @@ -24,27 +24,27 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - + + + + diff --git a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj index 36b19a6c..15096e40 100644 --- a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj +++ b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj @@ -12,12 +12,12 @@ - - - - - - + + + + + + From 5edfc73d32a03e95449f072ba06b7ae6f13c64f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Mon, 4 Mar 2024 21:30:02 +0100 Subject: [PATCH 10/31] Add RemoveByImplementation extension method. --- .../Extensions/ServiceCollectionExtensions.cs | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/Lombiq.HelpfulLibraries.Common/Extensions/ServiceCollectionExtensions.cs b/Lombiq.HelpfulLibraries.Common/Extensions/ServiceCollectionExtensions.cs index 11e07084..49ec1d76 100644 --- a/Lombiq.HelpfulLibraries.Common/Extensions/ServiceCollectionExtensions.cs +++ b/Lombiq.HelpfulLibraries.Common/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; namespace Microsoft.Extensions.DependencyInjection; @@ -9,16 +10,57 @@ namespace Microsoft.Extensions.DependencyInjection; public static class ServiceCollectionExtensions { /// - /// Removes implementations of type from an instance. + /// Removes all service descriptors where the is . + /// + /// + /// If , it also considers keyed services by checking the . + /// + public static IServiceCollection RemoveByImplementation(this IServiceCollection services, bool includeKeyed = true) => + services.RemoveByImplementation(typeof(T), includeKeyed); + + /// + /// Removes all service descriptors where the is . /// + /// + /// If , it also considers keyed services by checking the . + /// + public static IServiceCollection RemoveByImplementation( + this IServiceCollection services, + Type implementationType, + bool includeKeyed = true) + { + // Have to check "service.IsKeyedService" to avoid new breaking behavior described here: + // https://github.com/dotnet/runtime/issues/95789 + services.RemoveAll(service => + (includeKeyed || !service.IsKeyedService) && + (service.IsKeyedService ? service.KeyedImplementationType : service.ImplementationType) == implementationType); + + return services; + } + + [Obsolete($"Use {nameof(RemoveImplementationsOf)} instead (renamed for clarity).")] public static IServiceCollection RemoveImplementations(this IServiceCollection services) => - RemoveImplementations(services, typeof(T).FullName); + services.RemoveImplementationsOf(); + + [Obsolete($"Use {nameof(RemoveImplementationsOf)} instead (renamed for clarity).")] + public static IServiceCollection RemoveImplementations(this IServiceCollection services, string serviceFullName) => + services.RemoveImplementationsOf(serviceFullName) + + /// + /// Removes implementations of type from an instance. + /// + public static IServiceCollection RemoveImplementationsOf(this IServiceCollection services) => + RemoveImplementationsOf(services, typeof(T).FullName); /// /// Removes the implementations specified in from an /// instance. /// - public static IServiceCollection RemoveImplementations(this IServiceCollection services, string serviceFullName) + public static IServiceCollection RemoveImplementationsOf(this IServiceCollection services, string serviceFullName) { var servicesToRemove = services .Where(service => service.ServiceType?.FullName == serviceFullName) From cee975ed144c4fca254f22952ff96c68464c6afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Mon, 4 Mar 2024 21:30:31 +0100 Subject: [PATCH 11/31] Fix ReplaceDefaultShapeTableManager which was crashing due to keyed services. --- .../Shapes/PerTenantShapeTableManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Shapes/PerTenantShapeTableManager.cs b/Lombiq.HelpfulLibraries.OrchardCore/Shapes/PerTenantShapeTableManager.cs index fd2aa2ee..8b858328 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Shapes/PerTenantShapeTableManager.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Shapes/PerTenantShapeTableManager.cs @@ -184,7 +184,7 @@ private bool IsBaseTheme(string themeFeatureId, string themeId) => public static void ReplaceDefaultShapeTableManager(IServiceCollection services) { - services.RemoveAll(service => service.ImplementationType == typeof(DefaultShapeTableManager)); + services.RemoveByImplementation(); services.AddTransient(); } } From c6b864865e11558e8ad73d15734f364d23ccfb1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Mon, 4 Mar 2024 21:53:34 +0100 Subject: [PATCH 12/31] Fix typo. --- .../Extensions/ServiceCollectionExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.HelpfulLibraries.Common/Extensions/ServiceCollectionExtensions.cs b/Lombiq.HelpfulLibraries.Common/Extensions/ServiceCollectionExtensions.cs index 49ec1d76..82f2919e 100644 --- a/Lombiq.HelpfulLibraries.Common/Extensions/ServiceCollectionExtensions.cs +++ b/Lombiq.HelpfulLibraries.Common/Extensions/ServiceCollectionExtensions.cs @@ -48,7 +48,7 @@ public static IServiceCollection RemoveImplementations(this IServiceCollectio [Obsolete($"Use {nameof(RemoveImplementationsOf)} instead (renamed for clarity).")] public static IServiceCollection RemoveImplementations(this IServiceCollection services, string serviceFullName) => - services.RemoveImplementationsOf(serviceFullName) + services.RemoveImplementationsOf(serviceFullName); /// /// Removes implementations of type from an instance. From dfcb4f7c68c2b7910a2bb8b7dec055868513a51f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Fri, 8 Mar 2024 00:09:12 +0100 Subject: [PATCH 13/31] Update OC preview version. --- .../Lombiq.HelpfulLibraries.LinqToDb.csproj | 2 +- ...Lombiq.HelpfulLibraries.OrchardCore.csproj | 38 +++++++++---------- .../Lombiq.HelpfulLibraries.Samples.csproj | 12 +++--- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj index 7da8beb0..393b2712 100644 --- a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj +++ b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj @@ -25,6 +25,6 @@ - + diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj index cebc32d2..68119f2c 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj +++ b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj @@ -24,27 +24,27 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - + + + + diff --git a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj index 15096e40..c1d54555 100644 --- a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj +++ b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj @@ -12,12 +12,12 @@ - - - - - - + + + + + + From a42b8fc6e1bdf6f7813e794ff3d77c6cd2ac5d74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Wed, 13 Mar 2024 06:15:05 +0100 Subject: [PATCH 14/31] Update OC package --- .../Lombiq.HelpfulLibraries.LinqToDb.csproj | 2 +- ...Lombiq.HelpfulLibraries.OrchardCore.csproj | 38 +++++++++---------- .../Lombiq.HelpfulLibraries.Samples.csproj | 12 +++--- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj index 313bc230..fc7a3aab 100644 --- a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj +++ b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj @@ -25,6 +25,6 @@ - + diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj index 68119f2c..e683c5dc 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj +++ b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj @@ -24,27 +24,27 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - + + + + diff --git a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj index c1d54555..91297b99 100644 --- a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj +++ b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj @@ -12,12 +12,12 @@ - - - - - - + + + + + + From 37b181f9363e0160ac6f342c8c6087ea10a99e74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Wed, 13 Mar 2024 23:52:22 +0100 Subject: [PATCH 15/31] Fix GetProperty. --- .../Contents/ContentExtensions.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs index 0a3120d4..79106062 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs @@ -182,7 +182,9 @@ public static bool IsNew(this IContent content) => public static T GetProperty(this ContentElement contentElement, string path) where T : class { - var data = (JsonObject)contentElement.Content; + // Re-serializing ensures that the SelectNode will query from the current root. + var data = JsonSerializer.SerializeToNode((JsonObject)contentElement.Content); + return data.SelectNode(path)?.Deserialize(); } } From a88c4e7d49203fc28dbc79fc95d3ed60b35d6b6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sat, 16 Mar 2024 21:41:51 +0100 Subject: [PATCH 16/31] Add MvcActionContextExtensions. --- ...Lombiq.HelpfulLibraries.OrchardCore.csproj | 1 + .../Mvc/MvcActionContextExtensions.cs | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 Lombiq.HelpfulLibraries.OrchardCore/Mvc/MvcActionContextExtensions.cs diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj index e683c5dc..aea08587 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj +++ b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj @@ -44,6 +44,7 @@ + diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/MvcActionContextExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/MvcActionContextExtensions.cs new file mode 100644 index 00000000..048585e7 --- /dev/null +++ b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/MvcActionContextExtensions.cs @@ -0,0 +1,40 @@ +using OrchardCore.Mvc.Core.Utilities; +using System; +using System.Collections.Generic; +using SettingsAdminController = OrchardCore.Settings.Controllers.AdminController; + +namespace Microsoft.AspNetCore.Mvc; + +public static class MvcActionContextExtensions +{ + private static readonly string settingsAdminControllerName = typeof(SettingsAdminController).ControllerName(); + + /// + /// Returns a value indicating whether the requested page matches the provided non-empty route values. + /// + public static bool IsMvcRoute( + this ActionContext context, + string action = null, + string controller = null, + string area = null) + { + var routeValues = context.ActionDescriptor.RouteValues; + + if (!string.IsNullOrEmpty(action) && routeValues["Action"]?.EqualsOrdinalIgnoreCase(action) != true) return false; + if (!string.IsNullOrEmpty(controller) && routeValues["Controller"]?.EqualsOrdinalIgnoreCase(controller) != true) return false; + if (!string.IsNullOrEmpty(area) && routeValues["Area"]?.EqualsOrdinalIgnoreCase(area) != true) return false; + + return true; + } + + /// + /// Returns a value indicating whether the requested page is a site setting editor for the provided . + /// + public static bool IsSiteSettingsPage(this ActionContext context, string groupId) => + context.IsMvcRoute( + nameof(SettingsAdminController.Index), + settingsAdminControllerName, + $"{nameof(OrchardCore)}.{nameof(OrchardCore.Settings)}") && + context.RouteData.Values.GetMaybe("GroupId")?.ToString() == groupId; +} From 3cb4f28a750680ee3cbfbf89f980cd2cc1e645e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Mon, 18 Mar 2024 00:01:13 +0100 Subject: [PATCH 17/31] Update STJ version to v8. --- .../Lombiq.HelpfulLibraries.SourceGenerators.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.HelpfulLibraries.SourceGenerators/Lombiq.HelpfulLibraries.SourceGenerators/Lombiq.HelpfulLibraries.SourceGenerators.csproj b/Lombiq.HelpfulLibraries.SourceGenerators/Lombiq.HelpfulLibraries.SourceGenerators/Lombiq.HelpfulLibraries.SourceGenerators.csproj index 133e66a9..cd936580 100644 --- a/Lombiq.HelpfulLibraries.SourceGenerators/Lombiq.HelpfulLibraries.SourceGenerators/Lombiq.HelpfulLibraries.SourceGenerators.csproj +++ b/Lombiq.HelpfulLibraries.SourceGenerators/Lombiq.HelpfulLibraries.SourceGenerators/Lombiq.HelpfulLibraries.SourceGenerators.csproj @@ -20,7 +20,7 @@ - + From b7259b2921c0a5c9f687ac0618906d61d459da9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Mon, 18 Mar 2024 01:16:36 +0100 Subject: [PATCH 18/31] Fix warning SA1311. --- .../Mvc/MvcActionContextExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/MvcActionContextExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/MvcActionContextExtensions.cs index 048585e7..48169d9f 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/MvcActionContextExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/MvcActionContextExtensions.cs @@ -7,7 +7,7 @@ namespace Microsoft.AspNetCore.Mvc; public static class MvcActionContextExtensions { - private static readonly string settingsAdminControllerName = typeof(SettingsAdminController).ControllerName(); + private static readonly string _settingsAdminControllerName = typeof(SettingsAdminController).ControllerName(); /// /// Returns a value indicating whether the requested page matches the provided non-empty route values. @@ -34,7 +34,7 @@ public static bool IsMvcRoute( public static bool IsSiteSettingsPage(this ActionContext context, string groupId) => context.IsMvcRoute( nameof(SettingsAdminController.Index), - settingsAdminControllerName, + _settingsAdminControllerName, $"{nameof(OrchardCore)}.{nameof(OrchardCore.Settings)}") && context.RouteData.Values.GetMaybe("GroupId")?.ToString() == groupId; } From 09ff84a3ef6459819d8d8bc7eed4b76c3fdc3e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Mon, 18 Mar 2024 01:28:20 +0100 Subject: [PATCH 19/31] Replace AdminRouteAttribute with AdminAttribute. --- Lombiq.HelpfulLibraries.OrchardCore/Docs/Mvc.md | 4 ---- .../Mvc/AdminRouteAttribute.cs | 1 + .../Mvc/AdminRouteAttributeRouteMapper.cs | 2 ++ Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Docs/Mvc.md b/Lombiq.HelpfulLibraries.OrchardCore/Docs/Mvc.md index a4d54c77..8070ee90 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Docs/Mvc.md +++ b/Lombiq.HelpfulLibraries.OrchardCore/Docs/Mvc.md @@ -19,7 +19,3 @@ If you also use our [UI Testing Toolbox](https://github.com/Lombiq/UI-Testing-To ## `WidgetFilterBase` A base class for creating filters that insert a content as widget in a specified zone with permission check. - -## `[AdminRoute]` - -`[AdminRoute("My/Path/{id}")]` can be applied to any MVC action method. It work the same way as if you used [Route("Admin/My/Path/{id}")] except the admin prefix is no longer hard coded. This functionality is enabled by registering its mapper as a DI service using the `AdminRouteAttributeRouteMapper.AddToServices(services)` static method. diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/AdminRouteAttribute.cs b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/AdminRouteAttribute.cs index 664cd9e4..339d3c8c 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/AdminRouteAttribute.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/AdminRouteAttribute.cs @@ -2,6 +2,7 @@ namespace Lombiq.HelpfulLibraries.OrchardCore.Mvc; +[Obsolete("Use the [Admin(route)] attribute instead.")] [AttributeUsage(AttributeTargets.Method)] public sealed class AdminRouteAttribute : Attribute { diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/AdminRouteAttributeRouteMapper.cs b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/AdminRouteAttributeRouteMapper.cs index 3af98164..6cc61afc 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/AdminRouteAttributeRouteMapper.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/AdminRouteAttributeRouteMapper.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Options; using OrchardCore.Admin; using OrchardCore.Mvc.Routing; +using System; using System.Reflection; namespace Lombiq.HelpfulLibraries.OrchardCore.Mvc; @@ -25,6 +26,7 @@ namespace Lombiq.HelpfulLibraries.OrchardCore.Mvc; /// It can be added to the DI service collection using the static method. /// /// +[Obsolete("Use the [Admin(route)] attribute instead of [AdminRoute(route)].")] public class AdminRouteAttributeRouteMapper : IAreaControllerRouteMapper { private readonly string _adminUrlPrefix; diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs index 0e362ff3..2e10490f 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/TypedRoute.cs @@ -93,7 +93,7 @@ public string ToString(HttpContext httpContext) public override string ToString() { var routeTemplate = _action.GetCustomAttribute()?.Template ?? - _action.GetCustomAttribute()?.Template; + _action.GetCustomAttribute()?.Template; var (route, arguments) = routeTemplate != null && !string.IsNullOrWhiteSpace(routeTemplate) ? GetRouteFromTemplate(routeTemplate, _arguments) : ($"{_area}/{_controller.ControllerName()}/{_action.GetCustomAttribute()?.Name ?? _action.Name}", _arguments); From cefd8cb1e91fad7c39bd06a7e7856cd42cac063d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Tue, 2 Apr 2024 14:53:21 +0200 Subject: [PATCH 20/31] Use JObject.FromObject instead of JsonSerializer.SerializeToNode where applicable. --- .../Contents/ContentExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs index 79106062..bd31ca97 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Contents/ContentExtensions.cs @@ -183,7 +183,7 @@ public static T GetProperty(this ContentElement contentElement, string path) where T : class { // Re-serializing ensures that the SelectNode will query from the current root. - var data = JsonSerializer.SerializeToNode((JsonObject)contentElement.Content); + var data = JObject.FromObject((JsonObject)contentElement.Content); return data.SelectNode(path)?.Deserialize(); } From 4d43c0c8222d9ba2869353c7b96410cb588040bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Tue, 23 Apr 2024 00:11:32 +0200 Subject: [PATCH 21/31] Remove leftover Newtonseft JsonConvert. --- .../Mvc/OrchardControllerExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/OrchardControllerExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/OrchardControllerExtensions.cs index 72e154b5..0431e6f2 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/OrchardControllerExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/OrchardControllerExtensions.cs @@ -79,6 +79,6 @@ private static void LogJsonError(Controller controller, Exception exception) logger.LogError( exception, "An error has occurred while generating a JSON result. (Request Route Values: {RouteValues})", - JsonConvert.SerializeObject(context.Request.RouteValues)); + JsonSerializer.Serialize(context.Request.RouteValues)); } } From efceb5d0eb5b405567bd454af344a77d24810a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Tue, 23 Apr 2024 17:17:09 +0200 Subject: [PATCH 22/31] Update OC preview version. --- .../Lombiq.HelpfulLibraries.LinqToDb.csproj | 2 +- ...Lombiq.HelpfulLibraries.OrchardCore.csproj | 40 +++++++++---------- .../Lombiq.HelpfulLibraries.Samples.csproj | 12 +++--- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj index fc7a3aab..2c6bdd11 100644 --- a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj +++ b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj @@ -25,6 +25,6 @@ - + diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj index aea08587..df9ff77b 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj +++ b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj @@ -24,28 +24,28 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - - + + + + + diff --git a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj index 91297b99..03f9ac7a 100644 --- a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj +++ b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj @@ -12,12 +12,12 @@ - - - - - - + + + + + + From 1d3f7fd118ef7ce24626e4ac5e908de372b2d2cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Tue, 23 Apr 2024 17:26:11 +0200 Subject: [PATCH 23/31] post update fixuppost update fixup --- .../Contents/JsonSectionDisplayDriver.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Contents/JsonSectionDisplayDriver.cs b/Lombiq.HelpfulLibraries.OrchardCore/Contents/JsonSectionDisplayDriver.cs index f6ed2002..810fb7d4 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Contents/JsonSectionDisplayDriver.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Contents/JsonSectionDisplayDriver.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc.ModelBinding; using OrchardCore.DisplayManagement.Entities; using OrchardCore.DisplayManagement.Handlers; +using OrchardCore.DisplayManagement.ModelBinding; using OrchardCore.DisplayManagement.Views; using OrchardCore.Security.Permissions; using OrchardCore.Settings; @@ -43,7 +44,7 @@ await AuthorizeAsync() .OnGroup(GroupId) : null; - public override async Task UpdateAsync(TSection section, BuildEditorContext context) + public override async Task UpdateAsync(TSection section, UpdateEditorContext context) { var viewModel = new JsonViewModel(); From 92c53d5ead42f04226f01518b532193b50de0529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Tue, 23 Apr 2024 22:44:45 +0200 Subject: [PATCH 24/31] unusing --- .../Contents/JsonSectionDisplayDriver.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Contents/JsonSectionDisplayDriver.cs b/Lombiq.HelpfulLibraries.OrchardCore/Contents/JsonSectionDisplayDriver.cs index 810fb7d4..1e81d4c1 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Contents/JsonSectionDisplayDriver.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Contents/JsonSectionDisplayDriver.cs @@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Mvc.ModelBinding; using OrchardCore.DisplayManagement.Entities; using OrchardCore.DisplayManagement.Handlers; -using OrchardCore.DisplayManagement.ModelBinding; using OrchardCore.DisplayManagement.Views; using OrchardCore.Security.Permissions; using OrchardCore.Settings; From df7967c96f0bae6ea71fc3aff47c7433b113c230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sat, 27 Apr 2024 19:31:43 +0200 Subject: [PATCH 25/31] Update OC package version. --- .../Lombiq.HelpfulLibraries.LinqToDb.csproj | 2 +- ...Lombiq.HelpfulLibraries.OrchardCore.csproj | 40 +++++++++---------- .../Lombiq.HelpfulLibraries.Samples.csproj | 12 +++--- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj index 2c6bdd11..68aa8c85 100644 --- a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj +++ b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj @@ -25,6 +25,6 @@ - + diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj index df9ff77b..4dffd3e2 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj +++ b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj @@ -24,28 +24,28 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - - + + + + + diff --git a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj index 03f9ac7a..95eef385 100644 --- a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj +++ b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj @@ -12,12 +12,12 @@ - - - - - - + + + + + + From fefbeed948fa6f76ce881eb246b7f92798514db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sat, 4 May 2024 12:57:04 +0200 Subject: [PATCH 26/31] Update OC versions --- .../Lombiq.HelpfulLibraries.LinqToDb.csproj | 2 +- ...Lombiq.HelpfulLibraries.OrchardCore.csproj | 40 +++++++++---------- .../Lombiq.HelpfulLibraries.Samples.csproj | 12 +++--- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj index 68aa8c85..84054891 100644 --- a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj +++ b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj @@ -25,6 +25,6 @@ - + diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj index 4dffd3e2..0e8d001b 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj +++ b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj @@ -24,28 +24,28 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - - + + + + + diff --git a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj index 95eef385..9ec0478f 100644 --- a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj +++ b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj @@ -12,12 +12,12 @@ - - - - - - + + + + + + From b610869de6d047671fa32a5417feb5b3095b228e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sat, 4 May 2024 13:55:11 +0200 Subject: [PATCH 27/31] Post merge and OC update fixup --- .../Fields/NoneShapeTableProvider.cs | 2 +- ...Lombiq.HelpfulLibraries.OrchardCore.csproj | 30 +++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Fields/NoneShapeTableProvider.cs b/Lombiq.HelpfulLibraries.OrchardCore/Fields/NoneShapeTableProvider.cs index e50d3304..0ec7186b 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Fields/NoneShapeTableProvider.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Fields/NoneShapeTableProvider.cs @@ -32,7 +32,7 @@ public void Discover(ShapeTableBuilder builder) /// /// Adds a "None" option to every field's display and editor and renders an empty shape. /// - public async Task DiscoverAsync(ShapeTableBuilder builder) + public async ValueTask DiscoverAsync(ShapeTableBuilder builder) { var allFieldNames = (await _contentDefinitionManager.ListPartDefinitionsAsync()) .SelectMany(part => part.Fields) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj index 0e8d001b..bb895afe 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj +++ b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj @@ -24,28 +24,32 @@ + - - - - + + + + + + - - + + + - - - - + + + + + + + - - - From 50522e73c3fdd12e605268a2d8e7d636126c9ecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Tue, 7 May 2024 21:08:58 +0200 Subject: [PATCH 28/31] Update OC to the latest preview. --- .../Lombiq.HelpfulLibraries.LinqToDb.csproj | 2 +- ...Lombiq.HelpfulLibraries.OrchardCore.csproj | 42 +++++++++---------- .../Lombiq.HelpfulLibraries.Samples.csproj | 12 +++--- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj index 84054891..27434260 100644 --- a/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj +++ b/Lombiq.HelpfulLibraries.LinqToDb/Lombiq.HelpfulLibraries.LinqToDb.csproj @@ -25,6 +25,6 @@ - + diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj index bb895afe..e956d857 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj +++ b/Lombiq.HelpfulLibraries.OrchardCore/Lombiq.HelpfulLibraries.OrchardCore.csproj @@ -25,31 +25,31 @@ - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - - + + diff --git a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj index 9ec0478f..25f1b628 100644 --- a/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj +++ b/Lombiq.HelpfulLibraries.Samples/Lombiq.HelpfulLibraries.Samples.csproj @@ -12,12 +12,12 @@ - - - - - - + + + + + + From aa51b0a1803afbca244f604f28276f7290276eee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Fri, 10 May 2024 11:32:26 +0200 Subject: [PATCH 29/31] Update readme and references to OC 1.9. --- Lombiq.HelpfulLibraries.Refit/Helpers/RefitHelper.cs | 10 +++++----- Lombiq.HelpfulLibraries.Refit/Readme.md | 6 +++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Lombiq.HelpfulLibraries.Refit/Helpers/RefitHelper.cs b/Lombiq.HelpfulLibraries.Refit/Helpers/RefitHelper.cs index e49d362d..cca336e2 100644 --- a/Lombiq.HelpfulLibraries.Refit/Helpers/RefitHelper.cs +++ b/Lombiq.HelpfulLibraries.Refit/Helpers/RefitHelper.cs @@ -14,12 +14,12 @@ public static class RefitHelper /// Optional action for configuring other settings. /// Interface to create the implementation for. /// An instance that implements . - [Obsolete("As of Orchard Core 1.9 Newtonsoft.Json is no longer supported.")] + [Obsolete("As of Orchard Core 2.0 Newtonsoft.Json is no longer supported.")] public static T WithNewtonsoftJson(string hostUrl, Action configure = null) => WithNewtonsoftJson(new Uri(hostUrl), configure); /// - [Obsolete("As of Orchard Core 1.9 Newtonsoft.Json is no longer supported.")] + [Obsolete("As of Orchard Core 2.0 Newtonsoft.Json is no longer supported.")] public static T WithNewtonsoftJson(Uri hostUrl, Action configure = null) => RestService.For(hostUrl.AbsoluteUri, CreateSettingsWithNewtonsoftJson(configure)); @@ -31,11 +31,11 @@ public static T WithNewtonsoftJson(Uri hostUrl, Action configu /// Optional action for configuring other settings. /// Interface to create the implementation for. /// An instance that implements . - [Obsolete("As of Orchard Core 1.9 Newtonsoft.Json is no longer supported.")] + [Obsolete("As of Orchard Core 2.0 Newtonsoft.Json is no longer supported.")] public static T WithNewtonsoftJson(HttpClient httpClient, Action configure = null) => RestService.For(httpClient, CreateSettingsWithNewtonsoftJson(configure)); - [Obsolete("As of Orchard Core 1.9 Newtonsoft.Json is no longer supported.")] + [Obsolete("As of Orchard Core 2.0 Newtonsoft.Json is no longer supported.")] private static RefitSettings CreateSettingsWithNewtonsoftJson(Action configure) => - throw new NotSupportedException("As of Orchard Core 1.9 Newtonsoft.Json is no longer supported."); + throw new NotSupportedException("As of Orchard Core 2.0 Newtonsoft.Json is no longer supported."); } diff --git a/Lombiq.HelpfulLibraries.Refit/Readme.md b/Lombiq.HelpfulLibraries.Refit/Readme.md index 2db9424f..87da3055 100644 --- a/Lombiq.HelpfulLibraries.Refit/Readme.md +++ b/Lombiq.HelpfulLibraries.Refit/Readme.md @@ -8,4 +8,8 @@ For general details about and on using the Helpful Libraries see the [root Readm ## Helpers -- `RefitHelper`: Adds shortcuts for creating Refit API clients from interfaces. (e.g. `RefitHelper.WithNewtonsoftJson()`) +- `RefitHelper`: Adds shortcuts for creating Refit API clients from interfaces. (Note: as of OC 2.0 `Newtonsoft.Json` has been deprecated so methods like `RefitHelper.WithNewtonsoftJson()` are marked obsolete.) + +## Models + +- `SimpleTextResponse`: An simplified container for the content, headers and some other metadata from `ApiResponse`. This way the `ApiResponse` can be disposed early and doesn't have to be carried around which would be a potential memory leak risk. From 52c1ea13084a353133b578b34e5b6cfea7861c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Fri, 10 May 2024 12:46:04 +0200 Subject: [PATCH 30/31] Add LocalizedHtmlStringConverter unit test. --- .../LocalizedHtmlStringConverter.cs | 7 +-- .../LocalizedHtmlStringConverterTests.cs | 50 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 Lombiq.HelpfulLibraries.Tests/UnitTests/Converters/LocalizedHtmlStringConverterTests.cs diff --git a/Lombiq.HelpfulLibraries.AspNetCore/Localization/LocalizedHtmlStringConverter.cs b/Lombiq.HelpfulLibraries.AspNetCore/Localization/LocalizedHtmlStringConverter.cs index 1cd4d740..382db97f 100644 --- a/Lombiq.HelpfulLibraries.AspNetCore/Localization/LocalizedHtmlStringConverter.cs +++ b/Lombiq.HelpfulLibraries.AspNetCore/Localization/LocalizedHtmlStringConverter.cs @@ -32,9 +32,10 @@ public override LocalizedHtmlString Read(ref Utf8JsonReader reader, Type typeToC if (token is JsonObject jsonObject) { - var name = jsonObject[nameof(LocalizedHtmlString.Name)]?.Deserialize(); - var value = jsonObject.GetMaybe(nameof(LocalizedHtmlString.Value))?.Deserialize() ?? name; - var isResourceNotFound = jsonObject.GetMaybe(nameof(LocalizedHtmlString.IsResourceNotFound))?.Deserialize(); + var dictionary = jsonObject.ToDictionaryIgnoreCase(); + var name = dictionary.GetMaybe(nameof(LocalizedHtmlString.Name))?.Deserialize(); + var value = dictionary.GetMaybe(nameof(LocalizedHtmlString.Value))?.Deserialize() ?? name; + var isResourceNotFound = dictionary.GetMaybe(nameof(LocalizedHtmlString.IsResourceNotFound))?.Deserialize(); name ??= value; if (string.IsNullOrEmpty(name)) throw new InvalidOperationException("Missing name."); diff --git a/Lombiq.HelpfulLibraries.Tests/UnitTests/Converters/LocalizedHtmlStringConverterTests.cs b/Lombiq.HelpfulLibraries.Tests/UnitTests/Converters/LocalizedHtmlStringConverterTests.cs new file mode 100644 index 00000000..73c5079e --- /dev/null +++ b/Lombiq.HelpfulLibraries.Tests/UnitTests/Converters/LocalizedHtmlStringConverterTests.cs @@ -0,0 +1,50 @@ +using Lombiq.HelpfulLibraries.AspNetCore.Localization; +using Microsoft.AspNetCore.Mvc.Localization; +using Shouldly; +using System.Text.Json; +using Xunit; + +namespace Lombiq.HelpfulLibraries.Tests.UnitTests.Converters; + +public class LocalizedHtmlStringConverterTests +{ + private const string Name = "my text"; + + private static readonly JsonSerializerOptions _options = new() + { + Converters = { new LocalizedHtmlStringConverter() }, + }; + + [Theory] + [InlineData(Name, Name, null, "{\"Name\":\"my text\",\"Value\":\"my text\",\"IsResourceNotFound\":false}")] + [InlineData(Name, Name, false, "{\"Name\":\"my text\",\"Value\":\"my text\",\"IsResourceNotFound\":false}")] + [InlineData(Name, Name, true, "{\"Name\":\"my text\",\"Value\":\"my text\",\"IsResourceNotFound\":true}")] + [InlineData( + Name, + "az én szövegem", // #spell-check-ignore-line + null, + "{\"Name\":\"my text\",\"Value\":\"az \\u00E9n sz\\u00F6vegem\",\"IsResourceNotFound\":false}")] + public void LocalizedHtmlStringShouldBeSerializedCorrectly(string name, string value, bool? notFound, string expected) + { + var localized = notFound == null + ? new LocalizedHtmlString(name, value) + : new LocalizedHtmlString(name, value, notFound.Value); + + JsonSerializer.Serialize(localized, _options).ShouldBe(expected); + } + + [Theory] + [InlineData("\"my text\"", Name, Name, false)] + [InlineData("{ \"name\": \"my text\", \"value\": \"my text\", \"isResourceNotFound\": true }", Name, Name, true)] + [InlineData("{ \"name\": \"my text\", \"value\": \"some other text\" }", Name, "some other text", false)] + [InlineData("{ \"value\": \"my text\" }", Name, Name, false)] + [InlineData("{ \"NAME\": \"my text\" }", Name, Name, false)] + public void LocalizedHtmlStringShouldBeDeserializedCorrectly(string json, string name, string value, bool notFound) + { + var localized = JsonSerializer.Deserialize(json, _options); + + localized.Name.ShouldBe(name); + localized.Value.ShouldBe(value); + localized.IsResourceNotFound.ShouldBe(notFound); + } +} From 79a8f359930e0df6918d632dd052225f4e278da5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Fri, 10 May 2024 12:57:03 +0200 Subject: [PATCH 31/31] spell-check-ignore-line again --- .../UnitTests/Converters/LocalizedHtmlStringConverterTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.HelpfulLibraries.Tests/UnitTests/Converters/LocalizedHtmlStringConverterTests.cs b/Lombiq.HelpfulLibraries.Tests/UnitTests/Converters/LocalizedHtmlStringConverterTests.cs index 73c5079e..6cbd5f45 100644 --- a/Lombiq.HelpfulLibraries.Tests/UnitTests/Converters/LocalizedHtmlStringConverterTests.cs +++ b/Lombiq.HelpfulLibraries.Tests/UnitTests/Converters/LocalizedHtmlStringConverterTests.cs @@ -23,7 +23,7 @@ public class LocalizedHtmlStringConverterTests Name, "az én szövegem", // #spell-check-ignore-line null, - "{\"Name\":\"my text\",\"Value\":\"az \\u00E9n sz\\u00F6vegem\",\"IsResourceNotFound\":false}")] + "{\"Name\":\"my text\",\"Value\":\"az \\u00E9n sz\\u00F6vegem\",\"IsResourceNotFound\":false}")] // #spell-check-ignore-line public void LocalizedHtmlStringShouldBeSerializedCorrectly(string name, string value, bool? notFound, string expected) { var localized = notFound == null