Skip to content

Commit 954992e

Browse files
authored
Add null check to Json.Get<T> (#199)
add null check to Json.Get<T>
1 parent e32b6b6 commit 954992e

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/NRedisStack/Json/JsonCommands.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public RedisResult Get(RedisKey key, string[] paths, RedisValue? indent = null,
225225
public T? Get<T>(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = default)
226226
{
227227
var res = _db.Execute(JsonCommandBuilder.Get<T>(key, path));
228-
if (res.Type == ResultType.BulkString)
228+
if (res.Type == ResultType.BulkString && !res.IsNull)
229229
{
230230
var arr = JsonSerializer.Deserialize<JsonArray>(res.ToString()!);
231231
if (arr?.Count > 0)

src/NRedisStack/Json/JsonCommandsAsync.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public async Task<RedisResult> GetAsync(RedisKey key, string[] paths, RedisValue
8181
public async Task<T?> GetAsync<T>(RedisKey key, string path = "$", JsonSerializerOptions? serializerOptions = default)
8282
{
8383
var res = await _db.ExecuteAsync(JsonCommandBuilder.Get<T>(key, path));
84-
if (res.Type == ResultType.BulkString)
84+
if (res.Type == ResultType.BulkString && !res.IsNull)
8585
{
8686
var arr = JsonSerializer.Deserialize<JsonArray>(res.ToString()!);
8787
if (arr?.Count > 0)

tests/NRedisStack.Tests/Json/JsonTests.cs

+34
Original file line numberDiff line numberDiff line change
@@ -1118,4 +1118,38 @@ public void TestJsonCommandBuilder()
11181118
}
11191119
Assert.Equal("JSON.GET", getBuild2.Command);
11201120
}
1121+
1122+
[Fact]
1123+
public void TestGetIssue198()
1124+
{
1125+
var commands = new JsonCommands(redisFixture.Redis.GetDatabase());
1126+
var keys = CreateKeyNames(1);
1127+
var key = keys[0];
1128+
commands.Set(key, "$", new Person() { Age = 35, Name = "Alice" });
1129+
1130+
// Path not found:
1131+
var result = commands.Get<Person>(key, "$.a"); // returns "[]" because the path is not found (but the key is)
1132+
// Key not found:
1133+
var result2 = commands.Get<Person>("notExistKey", "$.a"); // returns (nil) because the key is not found
1134+
1135+
Assert.Null(result);
1136+
Assert.Null(result2);
1137+
}
1138+
1139+
[Fact]
1140+
public async Task TestGetIssue198_Async()
1141+
{
1142+
var commands = new JsonCommandsAsync(redisFixture.Redis.GetDatabase());
1143+
var keys = CreateKeyNames(1);
1144+
var key = keys[0];
1145+
await commands.SetAsync(key, "$", new Person() { Age = 35, Name = "Alice" });
1146+
1147+
// Path not found:
1148+
var result = await commands.GetAsync<Person>(key, "$.a"); // returns "[]" because the path is not found (but the key is)
1149+
// Key not found:
1150+
var result2 = await commands.GetAsync<Person>("notExistKey", "$.a"); // returns (nil) because the key is not found
1151+
1152+
Assert.Null(result);
1153+
Assert.Null(result2);
1154+
}
11211155
}

0 commit comments

Comments
 (0)