diff --git a/src/main/java/redis/clients/jedis/search/FieldName.java b/src/main/java/redis/clients/jedis/search/FieldName.java index d75251a812..ba8a7e10f6 100644 --- a/src/main/java/redis/clients/jedis/search/FieldName.java +++ b/src/main/java/redis/clients/jedis/search/FieldName.java @@ -25,6 +25,12 @@ public FieldName(String name, String attribute) { } public FieldName as(String attribute) { + if (attribute == null) { + throw new IllegalArgumentException("Setting null as field attribute is not allowed."); + } + if (this.attribute != null) { + throw new IllegalStateException("Attribute for this field is already set."); + } this.attribute = attribute; return this; } diff --git a/src/test/java/redis/clients/jedis/modules/search/SchemaTest.java b/src/test/java/redis/clients/jedis/modules/search/SchemaTest.java index d8409ad0b7..e7917e3f48 100644 --- a/src/test/java/redis/clients/jedis/modules/search/SchemaTest.java +++ b/src/test/java/redis/clients/jedis/modules/search/SchemaTest.java @@ -1,12 +1,14 @@ package redis.clients.jedis.modules.search; +import static org.junit.Assert.assertThrows; + +import java.util.Collections; import org.hamcrest.CoreMatchers; import org.hamcrest.MatcherAssert; import org.junit.Test; +import redis.clients.jedis.search.FieldName; import redis.clients.jedis.search.Schema; -import java.util.Collections; - public class SchemaTest { private final static String TITLE = "title"; @@ -33,4 +35,15 @@ public void printSchemaTest() throws Exception { MatcherAssert.assertThat(schemaPrint, CoreMatchers.containsString("{name='release_year', type=NUMERIC, sortable=true, noindex=false}")); MatcherAssert.assertThat(schemaPrint, CoreMatchers.containsString("VectorField{name='vector', type=VECTOR, algorithm=HNSW")); } + + @Test + public void fieldAttributeNull() { + assertThrows(IllegalArgumentException.class, () -> FieldName.of("identifier").as(null)); + } + + @Test + public void fieldAttributeMultiple() { + assertThrows(IllegalStateException.class, () -> FieldName.of("identifier").as("attribute").as("attribute")); + assertThrows(IllegalStateException.class, () -> new FieldName("identifier", "attribute").as("attribute")); + } }