Skip to content

Commit

Permalink
Fix JSONPathTester not working with root level arrays (#66)
Browse files Browse the repository at this point in the history
* Fix JSONPathTester not working with root arrays

* Update unit tests, update TestJsonPathAsync

---------

Co-authored-by: Sebastian Siewien <[email protected]>
  • Loading branch information
wiewiur667 and sebastiansiewien authored Feb 20, 2025
1 parent 7807e7e commit ab94a84
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"foo": 1
},
{
"foo": 2
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public JsonPathTesterCommandLineToolTests()
}

[Fact]
public async Task TestJsonPathUi()
public async Task TestJsonPathUiObject()
{
string inputJson = await TestDataProvider.GetEmbeddedFileContent("DevToys.Tools.UnitTests.Tools.TestData.JsonPathTester.sample.json");
string inputJson = await TestDataProvider.GetEmbeddedFileContent("DevToys.Tools.UnitTests.Tools.TestData.JsonPathTester.sample-object.json");

_tool.InputJson = inputJson;
_tool.InputJsonPath = "$.phoneNumbers[:1].type";
Expand All @@ -31,9 +31,9 @@ public async Task TestJsonPathUi()
}

[Fact]
public async Task TestJsonPathUiFailed()
public async Task TestJsonPathUiObjectFailed()
{
string inputJson = await TestDataProvider.GetEmbeddedFileContent("DevToys.Tools.UnitTests.Tools.TestData.JsonPathTester.sample.json");
string inputJson = await TestDataProvider.GetEmbeddedFileContent("DevToys.Tools.UnitTests.Tools.TestData.JsonPathTester.sample-object.json");

_tool.InputJson = inputJson;
_tool.InputJsonPath = "$.TEST";
Expand All @@ -44,4 +44,36 @@ public async Task TestJsonPathUiFailed()
string result = consoleOutput.GetOutput().Trim();
result.Should().Be("[]");
}

[Fact]
public async Task TestJsonPathUiArray()
{
string inputJson = await TestDataProvider.GetEmbeddedFileContent("DevToys.Tools.UnitTests.Tools.TestData.JsonPathTester.sample-array.json");

_tool.InputJson = inputJson;
_tool.InputJsonPath = "$[0].foo";

using var consoleOutput = new ConsoleOutputMonitor();
await _tool.InvokeAsync(new MockILogger(), CancellationToken.None);

string result = consoleOutput.GetOutput().Trim();
result.Should().Be(@"[
1
]");
}

[Fact]
public async Task TestJsonPathUiArrayFailed()
{
string inputJson = await TestDataProvider.GetEmbeddedFileContent("DevToys.Tools.UnitTests.Tools.TestData.JsonPathTester.sample-array.json");

_tool.InputJson = inputJson;
_tool.InputJsonPath = "$[0].TEST";

using var consoleOutput = new ConsoleOutputMonitor();
await _tool.InvokeAsync(new MockILogger(), CancellationToken.None);

string result = consoleOutput.GetOutput().Trim();
result.Should().Be("[]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public JsonPathTesterGuiToolTests()
}

[Fact]
public async Task TestJsonPathUi()
public async Task TestJsonPathUiObject()
{
string inputJson = await TestDataProvider.GetEmbeddedFileContent("DevToys.Tools.UnitTests.Tools.TestData.JsonPathTester.sample.json");
string inputJson = await TestDataProvider.GetEmbeddedFileContent("DevToys.Tools.UnitTests.Tools.TestData.JsonPathTester.sample-object.json");

_inputBox.Text(inputJson);
_jsonPathInputBox.Text("$.phoneNumbers[:1].type");
Expand All @@ -38,14 +38,40 @@ public async Task TestJsonPathUi()
}

[Fact]
public async Task TestJsonPathUiFailed()
public async Task TestJsonPathUiObjectFailed()
{
string inputJson = await TestDataProvider.GetEmbeddedFileContent("DevToys.Tools.UnitTests.Tools.TestData.JsonPathTester.sample.json");
string inputJson = await TestDataProvider.GetEmbeddedFileContent("DevToys.Tools.UnitTests.Tools.TestData.JsonPathTester.sample-object.json");

_inputBox.Text(inputJson);
_jsonPathInputBox.Text("$.TEST");
await _tool.WorkTask;

_outputBox.Text.Should().Be("[]");
}

[Fact]
public async Task TestJsonPathUiArray()
{
string inputJson = await TestDataProvider.GetEmbeddedFileContent("DevToys.Tools.UnitTests.Tools.TestData.JsonPathTester.sample-array.json");

_inputBox.Text(inputJson);
_jsonPathInputBox.Text("$[0].foo");
await _tool.WorkTask;

_outputBox.Text.Should().Be(@"[
1
]");
}

[Fact]
public async Task TestJsonPathUiArrayFailed()
{
string inputJson = await TestDataProvider.GetEmbeddedFileContent("DevToys.Tools.UnitTests.Tools.TestData.JsonPathTester.sample-array.json");

_inputBox.Text(inputJson);
_jsonPathInputBox.Text("$[0].TEST");
await _tool.WorkTask;

_outputBox.Text.Should().Be("[]");
}
}
4 changes: 2 additions & 2 deletions src/DevToys.Tools/Helpers/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ internal static async Task<ResultInfo<string>> TestJsonPathAsync(string json, st
try
{
using JsonReader reader = new JsonTextReader(new StringReader(json));
JObject? jsonObject = await JObject.LoadAsync(reader, settings: null, cancellationToken);
JToken? jsonObject = await JToken.LoadAsync(reader, settings: null, cancellationToken);
return TestJsonPath(jsonObject, jsonPath, logger, cancellationToken);
}
catch (Exception ex) when (ex is OperationCanceledException or JsonReaderException)
Expand All @@ -134,7 +134,7 @@ internal static async Task<ResultInfo<string>> TestJsonPathAsync(string json, st
/// <summary>
/// Get the data of a JSON object in the given JSONPath.
/// </summary>
internal static ResultInfo<string> TestJsonPath(JObject jsonObject, string jsonPath, ILogger logger, CancellationToken cancellationToken)
internal static ResultInfo<string> TestJsonPath(JToken jsonObject, string jsonPath, ILogger logger, CancellationToken cancellationToken)
{
Guard.IsNotNull(logger);
if (jsonObject is null || string.IsNullOrWhiteSpace(jsonPath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private enum GridColumns

private CancellationTokenSource? _cancellationTokenSource;
private string _lastTreatedInputJson = string.Empty;
private JObject? _inputJsonObjectBackup = null;
private JToken? _inputJsonObjectBackup = null;

[ImportingConstructor]
public JsonPathTesterGuiTool(ISettingsProvider settingsProvider)
Expand Down Expand Up @@ -172,18 +172,18 @@ private async Task TestJsonPathAsync(string json, string jsonPath, CancellationT
if (string.Equals(_lastTreatedInputJson, json, StringComparison.Ordinal))
{
// We can reuse the previous JObject. Let's avoid parsing it too often to improve performance.
_inputJsonObjectBackup ??= JObject.Parse(json);
_inputJsonObjectBackup ??= JToken.Parse(json);
}
else
{
// We need to parse the new JObject.
_inputJsonObjectBackup = JObject.Parse(json);
_inputJsonObjectBackup = JToken.Parse(json);
_lastTreatedInputJson = json;
}

cancellationToken.ThrowIfCancellationRequested();

JObject jsonObject = _inputJsonObjectBackup;
JToken jsonObject = _inputJsonObjectBackup;

ResultInfo<string> resultInfo = JsonHelper.TestJsonPath(jsonObject, jsonPath, _logger, cancellationToken);
if (resultInfo.HasSucceeded)
Expand Down

0 comments on commit ab94a84

Please sign in to comment.