Skip to content

Commit

Permalink
Redis - Improve parameter validation of the hmget command
Browse files Browse the repository at this point in the history
Fix #42131
  • Loading branch information
cescoffier committed Jul 25, 2024
1 parent 72297fb commit 641b11a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public interface HashCommands<K, F, V> extends RedisCommands {
* Group: hash
* Requires Redis 2.0.0
*
* @param key the key
* @param fields the fields
* @param key the key must not be {@code null}
* @param fields the fields, must not be empty, must not contain {@code null} values
* @return list of values associated with the given fields, in the same order as they are requested.
* If a requested field does not exist, the returned map contains a {@code null} value for that field.
**/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ Uni<Response> _hlen(K key) {

@SafeVarargs
final Uni<Response> _hmget(K key, F... fields) {
nonNull(key, "key");
doesNotContainNull(fields, "fields");
if (fields.length == 0) {
throw new IllegalArgumentException("`fields` must not be empty");
}
RedisCommand cmd = RedisCommand.of(Command.HMGET);
cmd.put(marshaller.encode(key));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.redis.datasource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.api.Assertions.offset;

Expand Down Expand Up @@ -392,4 +393,24 @@ void populateManyEntries(HashCommands<String, String, String> hash, Map<String,
hash.hset(key, expect);
}

/**
* Reproducer for <a href="https://github.com/quarkusio/quarkus/issues/42131">#42131</a>.
*/
@Test
void testInvalidHashMGet() {
HashCommands<String, String, String> cmd = ds.hash(String.class, String.class, String.class);
// Key must not be null
assertThatThrownBy(() -> cmd.hmget(null, "a", "b")).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("key");
// Fields must not be empty
assertThatThrownBy(() -> cmd.hmget("key")).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("fields");

// Fields must not contain `null`
assertThatThrownBy(() -> cmd.hmget("key", null, "b")).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("fields");
assertThatThrownBy(() -> cmd.hmget("key", "a", null, "b")).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("fields");
}

}

0 comments on commit 641b11a

Please sign in to comment.