From 51a11901fd6809eba919dfae40189e6195f2c6d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Mon, 18 Mar 2024 00:13:59 +0100 Subject: [PATCH 1/4] Update JOptions configuration. --- src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs b/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs index 7fdb2854208..b7fe56c2c1e 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs @@ -13,7 +13,8 @@ public static class JOptions public static readonly JsonSerializerOptions Base = new() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - ReferenceHandler = ReferenceHandler.IgnoreCycles, + PreferredObjectCreationHandling = JsonObjectCreationHandling.Populate, + ReferenceHandler = null, // Needed by JsonObjectCreationHandling.Populate. ReadCommentHandling = JsonCommentHandling.Skip, PropertyNameCaseInsensitive = true, AllowTrailingCommas = true, From 3d6354290f02c8a2a2693f7ce80e46cb085ce08f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Mon, 18 Mar 2024 22:49:30 +0100 Subject: [PATCH 2/4] Move settings into ContentSerializerJsonOptionsConfiguration. --- .../Extensions/ContentSerializerJsonOptionsConfiguration.cs | 4 +++- src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/OrchardCore/OrchardCore.Abstractions/Extensions/ContentSerializerJsonOptionsConfiguration.cs b/src/OrchardCore/OrchardCore.Abstractions/Extensions/ContentSerializerJsonOptionsConfiguration.cs index 6fab879b35d..2a5f874420d 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Extensions/ContentSerializerJsonOptionsConfiguration.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Extensions/ContentSerializerJsonOptionsConfiguration.cs @@ -17,8 +17,10 @@ public ContentSerializerJsonOptionsConfiguration(IOptions Date: Thu, 21 Mar 2024 14:54:15 +0100 Subject: [PATCH 3/4] Add unit test. --- .../Data/ContentItemTests.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/OrchardCore.Tests/Data/ContentItemTests.cs b/test/OrchardCore.Tests/Data/ContentItemTests.cs index c91f2ae3ea2..989e0603478 100644 --- a/test/OrchardCore.Tests/Data/ContentItemTests.cs +++ b/test/OrchardCore.Tests/Data/ContentItemTests.cs @@ -101,6 +101,23 @@ public void ContentShouldStoreFields() Assert.Contains(@"""MyPart"":{""Text"":""test"",""myField"":{""Value"":123}}", json); } + + [Fact] + public void ShouldDeserializeListContentPart() + { + var contentItem = new ContentItem(); + contentItem.GetOrCreate(); + contentItem.Alter(x => x.Text = "test"); + contentItem.Alter(x => + { + x.GetOrCreate("myField"); + x.Alter("myField", f => f.Value = 123); + }); + + var json = JConvert.SerializeObject(contentItem); + + Assert.Contains(@"""MyPart"":{""Text"":""test"",""myField"":{""Value"":123}}", json); + } } public class MyPart : ContentPart @@ -112,4 +129,9 @@ public class MyField : ContentField { public int Value { get; set; } } + + public class GetOnlyListPart : ContentPart + { + public IList Texts { get; } = new List(); + } } From 5734c8a142c62eae61596e01f8e9215c9f3e9a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Thu, 21 Mar 2024 14:54:43 +0100 Subject: [PATCH 4/4] Revert "Move settings into ContentSerializerJsonOptionsConfiguration." This reverts commit 3d6354290f02c8a2a2693f7ce80e46cb085ce08f. --- .../Extensions/ContentSerializerJsonOptionsConfiguration.cs | 4 +--- src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/OrchardCore/OrchardCore.Abstractions/Extensions/ContentSerializerJsonOptionsConfiguration.cs b/src/OrchardCore/OrchardCore.Abstractions/Extensions/ContentSerializerJsonOptionsConfiguration.cs index 2a5f874420d..6fab879b35d 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Extensions/ContentSerializerJsonOptionsConfiguration.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Extensions/ContentSerializerJsonOptionsConfiguration.cs @@ -17,10 +17,8 @@ public ContentSerializerJsonOptionsConfiguration(IOptions