diff --git a/src/OrchardCore/OrchardCore.Abstractions/Json/Nodes/JArray.cs b/src/OrchardCore/OrchardCore.Abstractions/Json/Nodes/JArray.cs index 828b9503892..b354a393311 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Json/Nodes/JArray.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Json/Nodes/JArray.cs @@ -118,7 +118,7 @@ public static bool ContainsValue(this JsonArray? jsonArray, JsonValue? value) settings ??= new JsonMergeSettings(); - switch (settings?.MergeArrayHandling ?? MergeArrayHandling.Concat) + switch (settings.MergeArrayHandling) { case MergeArrayHandling.Concat: diff --git a/src/OrchardCore/OrchardCore.Abstractions/OrchardCore.Abstractions.csproj b/src/OrchardCore/OrchardCore.Abstractions/OrchardCore.Abstractions.csproj index c39dcada174..3f580b270c1 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/OrchardCore.Abstractions.csproj +++ b/src/OrchardCore/OrchardCore.Abstractions/OrchardCore.Abstractions.csproj @@ -12,6 +12,7 @@ + diff --git a/test/OrchardCore.Tests/Abstractions/Json/Nodes/JArrayTests.cs b/test/OrchardCore.Tests/Abstractions/Json/Nodes/JArrayTests.cs new file mode 100644 index 00000000000..740d12441bc --- /dev/null +++ b/test/OrchardCore.Tests/Abstractions/Json/Nodes/JArrayTests.cs @@ -0,0 +1,30 @@ +using System.Text.Json.Nodes; +using System.Text.Json.Settings; + +namespace OrchardCore.Json.Nodes.Test; + +public class JArrayTests +{ + public static IEnumerable MergeArrayEntries => [ + ["[1, 2, 3, 4]", "[4, 5, 6]", null, "[1,2,3,4,4,5,6]"], + ["[1, 2, 3, 4]", "[4, 5, 6]", new JsonMergeSettings() { MergeArrayHandling = MergeArrayHandling.Concat }, "[1,2,3,4,4,5,6]"], + ["[1, 2, 3, 4]", "[4, 5, 6]", new JsonMergeSettings() { MergeArrayHandling = MergeArrayHandling.Union }, "[1,2,3,4,5,6]"], + ["[1, 2, 3, 4]", "[4, 5, 6]", new JsonMergeSettings() { MergeArrayHandling = MergeArrayHandling.Replace }, "[4,5,6]"] + ]; + + [Theory] + [MemberData(nameof(MergeArrayEntries))] + public void MergeArrayShouldRespectJsonMergeSettings(string jsonArrayContent1, string jsonArrayContent2, JsonMergeSettings mergeSettings, string expectedJsonString) + { + // Arrange + var array = JsonNode.Parse(jsonArrayContent1) as JsonArray; + var content = JsonNode.Parse(jsonArrayContent2); + + // Act + var result = array.Merge(content, mergeSettings); + + // Assert + Assert.NotNull(result); + Assert.Equal(expectedJsonString, result.ToJsonString()); + } +}