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

Add SelectNode and Remove method to JsonDynamicObject class #15509

Merged
merged 21 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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 @@ -41,6 +41,20 @@ public object? this[int index]
}
}

public bool Remove(JsonNode? item)
{
var index = _jsonArray.IndexOf(item);
_dictionary.Remove(index);

return _jsonArray.Remove(item);
}

public void RemoveAt(int index)
{
_dictionary.Remove(index);
_jsonArray.RemoveAt(index);
}

public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object? result)
{
var value = GetValue((int)indexes[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object?[]? args,
return true;
}

public bool Remove(string key)
{
_dictionary.Remove(key);

return _jsonObject.Remove(key);
hyzx86 marked this conversation as resolved.
Show resolved Hide resolved
}

public JsonNode? SelectNode(string path) => _jsonObject.SelectNode(path);
hyzx86 marked this conversation as resolved.
Show resolved Hide resolved

[Obsolete("Please use the SelectNode method", error: true)]
public JsonNode? SelectToken(string path) => _jsonObject.SelectNode(path);

public object? GetValue(string key)
{
if (_dictionary.TryGetValue(key, out var value))
Expand Down
46 changes: 35 additions & 11 deletions test/OrchardCore.Tests/Data/ContentItemTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text.Json;
using OrchardCore.ContentManagement;
using System.Text.Json.Dynamic;
using System.Text.Json.Nodes;
using OrchardCore.ContentManagement;

namespace OrchardCore.Tests.Data
{
Expand Down Expand Up @@ -117,27 +118,34 @@ public void ContentShouldBeJsonPathQueryable()
AssertJsonEqual(textPropertyNode, contentItemJson.SelectNode("$..Text"));
}

private static ContentItem CreateContentItemWithMyPart(string text = "test")
[Fact]
public void RemovingPropertyShouldWork()
{
var contentItem = new ContentItem();
contentItem.GetOrCreate<MyPart>();
contentItem.Alter<MyPart>(x => x.Text = text);

return contentItem;
contentItem.Alter<MyPart>(x => x.Text = "test");

JsonDynamicObject content = contentItem.Content;
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
Assert.Null(content.GetValue("not real property")); // Properties that don't exist return null.
Assert.NotNull(content.GetValue(nameof(MyPart))); // Right now this property exists.

content.Remove(nameof(MyPart));
Assert.Null(content.GetValue(nameof(MyPart)));
}

private static void AssertJsonEqual(JsonNode expected, JsonNode actual)
[Fact]
public void ContentShouldCanCallRemoveMethod()
{
Assert.NotNull(expected);
Assert.NotNull(actual);
Assert.Equal(expected.ToJsonString(), actual.ToJsonString());
var contentItem = CreateContentItemWithMyPart();
contentItem.Alter<MyPart>(x => x.Text = "test");
Assert.Equal("test", contentItem.As<MyPart>().Text);
Assert.True(contentItem.Content.Remove("MyPart"));
}

[Fact]
public void ShouldDeserializeListContentPart()
{
var contentItem = new ContentItem();
contentItem.GetOrCreate<GetOnlyListPart>();
var contentItem = CreateContentItemWithMyPart();
contentItem.Alter<MyPart>(x => x.Text = "test");
contentItem.Alter<MyPart>(x =>
{
Expand All @@ -149,6 +157,22 @@ public void ShouldDeserializeListContentPart()

Assert.Contains(@"""MyPart"":{""Text"":""test"",""myField"":{""Value"":123}}", json);
}

private static ContentItem CreateContentItemWithMyPart(string text = "test")
{
var contentItem = new ContentItem();
contentItem.GetOrCreate<MyPart>();
contentItem.Alter<MyPart>(x => x.Text = text);

return contentItem;
}

private static void AssertJsonEqual(JsonNode expected, JsonNode actual)
{
Assert.NotNull(expected);
Assert.NotNull(actual);
Assert.Equal(expected.ToJsonString(), actual.ToJsonString());
}
}

public class MyPart : ContentPart
Expand Down
Loading