Skip to content

Commit 4fb787c

Browse files
authored
Fix for #1564: Return empty array instead of single element nil array in value array processor (#1993)
This changes the return of a nil result through the `RedisValueArrayProcessor` so that it's a `[]` instead of a single element `[ nil ]` in our handling. This affects the following commands, which are multibulk when a count is provided (even if it's 1), otherwise they are bulkstring. This change affects the non-count case when it's null. Instead of a single element array with a nil value, we'd return an empty array as the Redis surface area intends: - `LPOP`/`RPOP` - `SRANDMEMBER` - `SPOP` The other usages of `RedisValueArrayProcessor` are _always_ multibulk and are not affected: - `HMGET` - `HKEYS` - `HVALS` - `LRANGE` - `MGET` - `SDIFF` - `SINTER` - `SUNION` - `SMEMBERS` - `SORT` - `XCLAIM` - `Z(REV)RANGE` - `Z(REV)RANGEBYLEX` - `Z(REV)RANGEBYSCORE`
1 parent 34ba699 commit 4fb787c

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

docs/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- More correctly reconnects subscriptions on connection failures, including to other endpoints
1717
- Adds "(vX.X.X)" version suffix to the default client ID so server-side `CLIENT LIST` can more easily see what's connected (#1985 via NickCraver)
1818
- Fix for including (or not including) key names on some message failures (#1990 via NickCraver)
19+
- Fixed return of nil results in `LPOP`, `RPOP`, `SRANDMEMBER`, and `SPOP` (#1993 via NickCraver)
1920

2021
## 2.2.88
2122

src/StackExchange.Redis/ResultProcessor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,10 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
12491249
{
12501250
// allow a single item to pass explicitly pretending to be an array; example: SPOP {key} 1
12511251
case ResultType.BulkString:
1252-
var arr = new[] { result.AsRedisValue() };
1252+
// If the result is nil, the result should be an empty array
1253+
var arr = result.IsNull
1254+
? Array.Empty<RedisValue>()
1255+
: new[] { result.AsRedisValue() };
12531256
SetResult(message, arr);
12541257
return true;
12551258
case ResultType.MultiBulk:

tests/StackExchange.Redis.Tests/Sets.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public void SetPopMulti_Multi()
8484
Assert.Equal(7, db.SetLength(key));
8585
}
8686
}
87+
8788
[Fact]
8889
public void SetPopMulti_Single()
8990
{
@@ -225,5 +226,22 @@ public async Task SetAdd_Zero_Async()
225226
Assert.Equal(0, db.SetLength(key));
226227
}
227228
}
229+
230+
[Fact]
231+
public void SetPopMulti_Nil()
232+
{
233+
using (var conn = Create())
234+
{
235+
Skip.IfMissingFeature(conn, nameof(RedisFeatures.SetPopMultiple), r => r.SetPopMultiple);
236+
237+
var db = conn.GetDatabase();
238+
var key = Me();
239+
240+
db.KeyDelete(key, CommandFlags.FireAndForget);
241+
242+
var arr = db.SetPop(key, 1);
243+
Assert.Empty(arr);
244+
}
245+
}
228246
}
229247
}

0 commit comments

Comments
 (0)