Skip to content

Commit

Permalink
Add SelectNode and Remove method to JsonDynamicObject class (OrchardC…
Browse files Browse the repository at this point in the history
…MS#15509)

Co-authored-by: Hisham Bin Ateya <[email protected]>
Co-authored-by: hyzx86 <[email protected]>
Co-authored-by: Sébastien Ros <[email protected]>
Co-authored-by: Mike Alhayek <[email protected]>
Co-authored-by: Sára El-Saig <[email protected]>
  • Loading branch information
6 people authored Mar 24, 2024
1 parent d524386 commit ba718ab
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
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);
}

public JsonNode? SelectNode(string path) => _jsonObject.SelectNode(path);

[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;
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

0 comments on commit ba718ab

Please sign in to comment.