Skip to content

Commit 18fb0ab

Browse files
committed
enhanced the implementation of addParam
1 parent b0e25f6 commit 18fb0ab

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/main/java/com/contentstack/cms/stack/Entry.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class Entry implements BaseImplementation<Entry> {
3535
protected final EntryService service;
3636
protected final String contentTypeUid;
3737
protected final String entryUid;
38+
private int includeCounter = 1;
3839

3940
protected Entry(Retrofit instance, Map<String, Object> headers, String contentTypeUid) {
4041
this.contentTypeUid = contentTypeUid;
@@ -82,15 +83,28 @@ public Entry addHeader(@NotNull String key, @NotNull String value) {
8283
* @param value query param value for the request
8384
* @return instance of {@link Entry}
8485
*/
86+
@Override
8587
public Entry addParam(@NotNull String key, @NotNull Object value) {
86-
this.params.put(key, value);
88+
if (key.equals("include[]")) {
89+
if (value instanceof String[]) {
90+
for (String item : (String[]) value) {
91+
this.params.put(key + includeCounter++, item);
92+
}
93+
} else if (value instanceof String) {
94+
this.params.put(key + includeCounter++, value);
95+
}
96+
} else {
97+
this.params.put(key, value);
98+
}
8799
return this;
88100
}
89101

90102

91103
@Override
92104
public Entry addParams(@NotNull HashMap<String, Object> params) {
93-
this.params.putAll(params);
105+
for (Map.Entry<String, Object> entry : params.entrySet()) {
106+
addParam(entry.getKey(), entry.getValue());
107+
}
94108
return this;
95109
}
96110

src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
import com.contentstack.cms.Contentstack;
44
import com.contentstack.cms.TestClient;
5+
import com.google.gson.JsonArray;
6+
57
import okhttp3.Request;
8+
69
import okhttp3.ResponseBody;
10+
import retrofit2.Response;
11+
712
import org.json.simple.JSONArray;
813
import org.json.simple.JSONObject;
914
import org.junit.jupiter.api.*;
1015

16+
import java.io.IOException;
1117
import java.util.ArrayList;
18+
import java.util.Arrays;
1219
import java.util.Collection;
1320
import java.util.HashMap;
1421
import java.util.Map;
@@ -93,6 +100,23 @@ void testSingleEntryQuery() {
93100
Request resp = entryInstance.fetch().request();
94101
Assertions.assertEquals("include_publish_details=true&locale=en-us&include_workflow=false", resp.url().query());
95102
}
103+
104+
@Test
105+
void testaddParam(){
106+
String[] array = {"reference","navigation_menu.page_reference"};
107+
entryInstance.addParam("include[]", array);
108+
Request request = entryInstance.find().request();
109+
Assertions.assertEquals("include[]1=reference&include[]2=navigation_menu.page_reference", request.url().query());
110+
}
111+
112+
@Test
113+
void testaddParams() throws IOException{
114+
HashMap<String, Object> paramslist = new HashMap<>();
115+
paramslist.put("include[]", new String[]{"reference", "navigation_menu.page_reference"});
116+
entryInstance.addParams(paramslist);
117+
Request request = entryInstance.find().request();
118+
Assertions.assertEquals("include[]1=reference&include[]2=navigation_menu.page_reference", request.url().query());
119+
}
96120

97121
@Test
98122
void testSingleEntryEncodedPath() {

0 commit comments

Comments
 (0)