Skip to content

Commit 5a8ae8d

Browse files
committed
Added: objectID parameter validity check before calling deleteObject method
1 parent d6b0e83 commit 5a8ae8d

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

algoliasearch-common/src/main/java/com/algolia/search/APIClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,12 @@ <T> TaskSingleIndex saveObjects(String indexName, List<T> objects, RequestOption
807807

808808
Task deleteObject(String indexName, String objectID, RequestOptions requestOptions)
809809
throws AlgoliaException {
810+
811+
if (objectID.trim().length() == 0)
812+
{
813+
throw new AlgoliaException("ObjectID must not be empty");
814+
}
815+
810816
Task result =
811817
httpClient.requestWithRetry(
812818
new AlgoliaRequest<>(

algoliasearch-common/src/main/java/com/algolia/search/AsyncAPIClient.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,14 @@ <T> CompletableFuture<AsyncTaskSingleIndex> saveObjects(
788788
.thenApply(s -> s.setIndex(indexName));
789789
}
790790

791-
CompletableFuture<AsyncTask> deleteObject(
792-
String indexName, String objectID, RequestOptions requestOptions) {
791+
CompletableFuture<AsyncTask> deleteObject (
792+
String indexName, String objectID, RequestOptions requestOptions) throws AlgoliaException {
793+
794+
if (objectID.trim().length() == 0)
795+
{
796+
throw new AlgoliaException("ObjectID must not be empty");
797+
}
798+
793799
return httpClient
794800
.requestWithRetry(
795801
new AlgoliaRequest<>(

algoliasearch-common/src/main/java/com/algolia/search/AsyncObjects.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.algolia.search;
22

3+
import com.algolia.search.exceptions.AlgoliaException;
34
import com.algolia.search.objects.RequestOptions;
45
import com.algolia.search.objects.tasks.async.AsyncTask;
56
import com.algolia.search.objects.tasks.async.AsyncTaskIndexing;
@@ -206,7 +207,7 @@ default CompletableFuture<AsyncTaskSingleIndex> saveObjects(
206207
* @param objectID the unique identifier of the object to retrieve
207208
* @return the associated AsyncTask
208209
*/
209-
default CompletableFuture<AsyncTask> deleteObject(@Nonnull String objectID) {
210+
default CompletableFuture<AsyncTask> deleteObject(@Nonnull String objectID) throws AlgoliaException {
210211
return deleteObject(objectID, RequestOptions.empty);
211212
}
212213

@@ -217,8 +218,8 @@ default CompletableFuture<AsyncTask> deleteObject(@Nonnull String objectID) {
217218
* @param requestOptions Options to pass to this request
218219
* @return the associated AsyncTask
219220
*/
220-
default CompletableFuture<AsyncTask> deleteObject(
221-
@Nonnull String objectID, @Nonnull RequestOptions requestOptions) {
221+
default CompletableFuture<AsyncTask> deleteObject (
222+
@Nonnull String objectID, @Nonnull RequestOptions requestOptions) throws AlgoliaException {
222223
return getApiClient().deleteObject(getName(), objectID, requestOptions);
223224
}
224225

algoliasearch-tests/src/test/java/com/algolia/search/integration/common/async/AsyncObjectsTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.List;
1212
import java.util.Optional;
1313
import java.util.concurrent.CompletableFuture;
14+
15+
import com.algolia.search.exceptions.AlgoliaException;
1416
import org.junit.Test;
1517

1618
@SuppressWarnings("ConstantConditions")
@@ -132,4 +134,22 @@ public void getObjectsWithAttributesToRetrieve() throws Exception {
132134
futureAssertThat(result).hasSize(1);
133135
futureAssertThat(result).extracting("name").containsNull();
134136
}
137+
138+
@Test(expected = AlgoliaException.class)
139+
public void deleteObjectEmptyObjectIdShouldFail() throws AlgoliaException {
140+
AsyncIndex<AlgoliaObject> index = createIndex(AlgoliaObject.class);
141+
index.deleteObject("");
142+
}
143+
144+
@Test(expected = AlgoliaException.class)
145+
public void deleteObjectWhiteSpaceObjectIdShouldFail() throws AlgoliaException {
146+
AsyncIndex<AlgoliaObject> index = createIndex(AlgoliaObject.class);
147+
index.deleteObject(" ");
148+
}
149+
150+
@Test(expected = NullPointerException.class)
151+
public void deleteObjectNullObjectIdShouldFail() throws AlgoliaException {
152+
AsyncIndex<AlgoliaObject> index = createIndex(AlgoliaObject.class);
153+
index.deleteObject(null);
154+
}
135155
}

algoliasearch-tests/src/test/java/com/algolia/search/integration/common/sync/SyncObjectsTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,22 @@ public void getObjectsWithAttributesToRetrieve() throws AlgoliaException {
130130
assertThat(objects.get(0))
131131
.isEqualToComparingFieldByField(new AlgoliaObjectWithID("1", null, 5));
132132
}
133+
134+
@Test(expected = AlgoliaException.class)
135+
public void deleteObjectEmptyObjectIdShouldFail() throws AlgoliaException {
136+
Index<AlgoliaObject> index = createIndex(AlgoliaObject.class);
137+
index.deleteObject("");
138+
}
139+
140+
@Test(expected = AlgoliaException.class)
141+
public void deleteObjectWhiteSpaceObjectIdShouldFail() throws AlgoliaException {
142+
Index<AlgoliaObject> index = createIndex(AlgoliaObject.class);
143+
index.deleteObject(" ");
144+
}
145+
146+
@Test(expected = NullPointerException.class)
147+
public void deleteObjectNullObjectIdShouldFail() throws AlgoliaException {
148+
Index<AlgoliaObject> index = createIndex(AlgoliaObject.class);
149+
index.deleteObject(null);
150+
}
133151
}

0 commit comments

Comments
 (0)