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 JSONPathTester not working with root level arrays #66

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -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
Loading