From 954992e425934318cda309a27456db8d16e3cdb4 Mon Sep 17 00:00:00 2001 From: shacharPash <93581407+shacharPash@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:12:03 +0300 Subject: [PATCH] Add null check to Json.Get (#199) add null check to Json.Get --- src/NRedisStack/Json/JsonCommands.cs | 2 +- src/NRedisStack/Json/JsonCommandsAsync.cs | 2 +- tests/NRedisStack.Tests/Json/JsonTests.cs | 34 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/NRedisStack/Json/JsonCommands.cs b/src/NRedisStack/Json/JsonCommands.cs index 98d39dfa..00c780fe 100644 --- a/src/NRedisStack/Json/JsonCommands.cs +++ b/src/NRedisStack/Json/JsonCommands.cs @@ -225,7 +225,7 @@ public RedisResult Get(RedisKey key, string[] paths, RedisValue? indent = null, public T? Get(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = default) { var res = _db.Execute(JsonCommandBuilder.Get(key, path)); - if (res.Type == ResultType.BulkString) + if (res.Type == ResultType.BulkString && !res.IsNull) { var arr = JsonSerializer.Deserialize(res.ToString()!); if (arr?.Count > 0) diff --git a/src/NRedisStack/Json/JsonCommandsAsync.cs b/src/NRedisStack/Json/JsonCommandsAsync.cs index 1e8b48b6..3382236b 100644 --- a/src/NRedisStack/Json/JsonCommandsAsync.cs +++ b/src/NRedisStack/Json/JsonCommandsAsync.cs @@ -81,7 +81,7 @@ public async Task GetAsync(RedisKey key, string[] paths, RedisValue public async Task GetAsync(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = default) { var res = await _db.ExecuteAsync(JsonCommandBuilder.Get(key, path)); - if (res.Type == ResultType.BulkString) + if (res.Type == ResultType.BulkString && !res.IsNull) { var arr = JsonSerializer.Deserialize(res.ToString()!); if (arr?.Count > 0) diff --git a/tests/NRedisStack.Tests/Json/JsonTests.cs b/tests/NRedisStack.Tests/Json/JsonTests.cs index 4a67cff8..694cc472 100644 --- a/tests/NRedisStack.Tests/Json/JsonTests.cs +++ b/tests/NRedisStack.Tests/Json/JsonTests.cs @@ -1118,4 +1118,38 @@ public void TestJsonCommandBuilder() } Assert.Equal("JSON.GET", getBuild2.Command); } + + [Fact] + public void TestGetIssue198() + { + var commands = new JsonCommands(redisFixture.Redis.GetDatabase()); + var keys = CreateKeyNames(1); + var key = keys[0]; + commands.Set(key, "$", new Person() { Age = 35, Name = "Alice" }); + + // Path not found: + var result = commands.Get(key, "$.a"); // returns "[]" because the path is not found (but the key is) + // Key not found: + var result2 = commands.Get("notExistKey", "$.a"); // returns (nil) because the key is not found + + Assert.Null(result); + Assert.Null(result2); + } + + [Fact] + public async Task TestGetIssue198_Async() + { + var commands = new JsonCommandsAsync(redisFixture.Redis.GetDatabase()); + var keys = CreateKeyNames(1); + var key = keys[0]; + await commands.SetAsync(key, "$", new Person() { Age = 35, Name = "Alice" }); + + // Path not found: + var result = await commands.GetAsync(key, "$.a"); // returns "[]" because the path is not found (but the key is) + // Key not found: + var result2 = await commands.GetAsync("notExistKey", "$.a"); // returns (nil) because the key is not found + + Assert.Null(result); + Assert.Null(result2); + } } \ No newline at end of file