Skip to content

Commit

Permalink
Add restrictions while setting search field attribute (#3124)
Browse files Browse the repository at this point in the history
- Attribute cannot be null
- Attribute cannot be set multiple times
  • Loading branch information
sazzad16 authored Aug 28, 2022
1 parent e41904e commit ff95d38
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/main/java/redis/clients/jedis/search/FieldName.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/redis/clients/jedis/modules/search/SchemaTest.java
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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"));
}
}

0 comments on commit ff95d38

Please sign in to comment.