Skip to content

Commit ff4fd2e

Browse files
authored
Remove duplicates in Search Api fields
1 parent 9409278 commit ff4fd2e

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

cloudinary-core/src/main/java/com/cloudinary/Search.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,28 @@ public Search nextCursor(String value) {
4040
}
4141

4242
public Search aggregate(String field) {
43-
aggregateParam.add(field);
43+
if (!aggregateParam.contains(field)) {
44+
aggregateParam.add(field);
45+
}
4446
return this;
4547
}
4648

4749
public Search withField(String field) {
48-
withFieldParam.add(field);
50+
if (!withFieldParam.contains(field)) {
51+
withFieldParam.add(field);
52+
}
4953
return this;
5054
}
5155

5256
public Search sortBy(String field, String dir) {
5357
HashMap<String, Object> sortBucket = new HashMap<String, Object>();
5458
sortBucket.put(field, dir);
59+
for (int i = 0; i < sortByParam.size(); i++) {
60+
if (sortByParam.get(i).containsKey(field)){
61+
sortByParam.add(i, sortBucket);
62+
return this;
63+
}
64+
}
5565
sortByParam.add(sortBucket);
5666
return this;
5767
}
@@ -68,4 +78,4 @@ public ApiResponse execute() throws Exception {
6878
Map<String, String> options = ObjectUtils.asMap("content_type", "json");
6979
return this.api.callApi(Api.HttpMethod.POST, Arrays.asList("resources", "search"), this.toQuery(), options);
7080
}
71-
}
81+
}

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractSearchTest.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.cloudinary.test;
22

33
import com.cloudinary.Cloudinary;
4+
import com.cloudinary.Search;
45
import com.cloudinary.utils.ObjectUtils;
56
import org.junit.*;
67
import org.junit.rules.TestName;
78

8-
import java.util.List;
9-
import java.util.Map;
9+
import java.lang.reflect.Field;
10+
import java.util.*;
1011

11-
import static org.junit.Assert.assertEquals;
12-
import static org.junit.Assert.assertNull;
12+
import static org.junit.Assert.*;
1313
import static org.junit.Assume.assumeNotNull;
1414

1515
@SuppressWarnings({"rawtypes", "unchecked", "JavaDoc"})
@@ -74,12 +74,46 @@ public void shouldFindResourceByAssetId() throws Exception {
7474
assertEquals(1, resources.size());
7575
}
7676

77+
@Test
78+
public void testShouldNotDuplicateValues() throws Exception {
79+
Search request = cloudinary.search().maxResults(1).
80+
sortBy("created_at", "asc")
81+
.sortBy("created_at", "desc")
82+
.sortBy("public_id", "asc")
83+
.aggregate("format")
84+
.aggregate("format")
85+
.aggregate("resource_type")
86+
.withField("context")
87+
.withField("context")
88+
.withField("tags");
89+
Field[] fields = Search.class.getDeclaredFields();
90+
for(Field field : fields) {
91+
if(field.getName() == "aggregateParam") {
92+
field.setAccessible(true);
93+
ArrayList<String> aggregateList = (ArrayList<String>) field.get(request);
94+
Set<String> testSet = new HashSet<String>(aggregateList);
95+
assertTrue(aggregateList.size() == testSet.size());
96+
}
97+
if (field.getName() == "withFieldParam") {
98+
field.setAccessible(true);
99+
ArrayList<String> withFieldList = (ArrayList<String>) field.get(request);
100+
Set<String> testSet = new HashSet<String>(withFieldList);
101+
assertTrue(withFieldList.size() == testSet.size());
102+
}
103+
if (field.getName() == "sortByParam") {
104+
field.setAccessible(true);
105+
ArrayList<HashMap<String, Object>> sortByList = (ArrayList<HashMap<String, Object>>) field.get(request);
106+
Set<HashMap<String, Object>> testSet = new HashSet<HashMap<String, Object>>(sortByList);
107+
assertTrue(sortByList.size() == testSet.size());
108+
}
109+
}
110+
}
111+
77112
@Test
78113
public void shouldPaginateResourcesLimitedByTagAndOrderdByAscendingPublicId() throws Exception {
79114
List<Map> resources;
80115
Map result = cloudinary.search().maxResults(1).expression(String.format("tags:%s", SEARCH_TAG)).sortBy("public_id", "asc").execute();
81116
resources = (List<Map>) result.get("resources");
82-
83117
assertEquals(1, resources.size());
84118
assertEquals(3, result.get("total_count"));
85119
assertEquals(SEARCH_TEST, resources.get(0).get("public_id"));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy