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

Fix Json Merge object #16589

Merged
merged 6 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,16 @@ public static bool TryParse(string json, out JsonObject? jsonObject, JsonNodeOpt
{
if (item.Value is null)
{
if (settings!.MergeNullValueHandling == MergeNullValueHandling.Merge)
{
jsonObject[item.Key] = null;
}

continue;
}

var existingProperty = jsonObject[item.Key];

if (existingProperty is null)
{
jsonObject[item.Key] = item.Value.Clone();
Expand All @@ -123,7 +129,7 @@ public static bool TryParse(string json, out JsonObject? jsonObject, JsonNodeOpt
if (existingProperty is JsonValue || existingProperty.GetValueKind() != item.Value.GetValueKind())
{
if (item.Value.GetValueKind() != JsonValueKind.Null ||
settings?.MergeNullValueHandling == MergeNullValueHandling.Merge)
settings!.MergeNullValueHandling == MergeNullValueHandling.Merge)
{
jsonObject[item.Key] = item.Value.Clone();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.Json.Settings;

namespace OrchardCore.ContentManagement;
Expand All @@ -17,6 +18,11 @@ public static class ContentExtensions
MergeNullValueHandling = MergeNullValueHandling.Merge
};

private static readonly JsonSerializerOptions _jsonReadExistingObjectSettings = new(JOptions.Default)
{
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
};

/// <summary>
/// Gets a content element by its name.
/// </summary>
Expand Down Expand Up @@ -186,7 +192,7 @@ public static ContentElement Apply(this ContentElement contentElement, string na
var elementData = contentElement.Data[name] as JsonObject;
if (elementData is not null)
{
elementData.Merge(JObject.FromObject(element), _jsonMergeSettings);
elementData.Merge(JObject.FromObject(element, _jsonReadExistingObjectSettings), _jsonMergeSettings);
}
else
{
Expand Down
26 changes: 26 additions & 0 deletions test/OrchardCore.Tests/ContentManagement/ContentElementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,30 @@ public void Get_WhenCastingConcreteTypeThenBaseType_ReturnNewInstance()
// contentPart should be returned from cache, so it must be same as actualPart.
Assert.Same(contentPart, actualPart);
}

[Theory]
[InlineData(15)]
[InlineData(-15)]
[InlineData(null)]
public void Apply_WhenCalledWithNullProperty_SetThePropertyToNull(int? minutes)
{
// Arrange
var contentItem = new ContentItem();

// Act
contentItem.Apply(new TestContentPart()
{
Minutes = minutes,
});

var part = contentItem.As<TestContentPart>();

// Assert
Assert.Equal(minutes, part.Minutes);
}
}

public class TestContentPart : ContentPart
{
public int? Minutes { get; set; }
}