Skip to content

Commit 9954343

Browse files
authored
Merge pull request #91 from contentstack/fix/DX-1282-addParam-method-enh
DX-1282: enhanced the implementation of addParam
2 parents b0e25f6 + 447c3d9 commit 9954343

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
run: mvn jacoco:prepare-agent jacoco:report
2323

2424
- name: Upload Code Coverage Report
25-
uses: actions/upload-artifact@v2
25+
uses: actions/upload-artifact@v3
2626
with:
2727
name: code-coverage-report
2828
path: target/site/jacoco

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import retrofit2.Call;
88
import retrofit2.Retrofit;
99

10+
import java.util.ArrayList;
11+
import java.util.Arrays;
1012
import java.util.HashMap;
13+
import java.util.List;
1114
import java.util.Map;
1215
import java.util.Objects;
1316

@@ -35,6 +38,7 @@ public class Entry implements BaseImplementation<Entry> {
3538
protected final EntryService service;
3639
protected final String contentTypeUid;
3740
protected final String entryUid;
41+
private int includeCounter = 1;
3842

3943
protected Entry(Retrofit instance, Map<String, Object> headers, String contentTypeUid) {
4044
this.contentTypeUid = contentTypeUid;
@@ -82,6 +86,7 @@ public Entry addHeader(@NotNull String key, @NotNull String value) {
8286
* @param value query param value for the request
8387
* @return instance of {@link Entry}
8488
*/
89+
@Override
8590
public Entry addParam(@NotNull String key, @NotNull Object value) {
8691
this.params.put(key, value);
8792
return this;
@@ -121,6 +126,30 @@ protected Entry clearParams() {
121126
return this;
122127
}
123128

129+
protected Entry addToParams(@NotNull String key, @NotNull Object value){
130+
if (key.equals("include[]")) {
131+
if (value instanceof String[]) {
132+
for (String item : (String[]) value) {
133+
this.params.put(key + includeCounter++, item);
134+
}
135+
} else if (value instanceof String) {
136+
this.params.put(key, value);
137+
}
138+
} else {
139+
this.params.put(key, value);
140+
}
141+
return this;
142+
}
143+
144+
public Call<ResponseBody> includeReference(@NotNull Object referenceField){
145+
if (referenceField instanceof String || referenceField instanceof String[]) {
146+
addToParams("include[]", referenceField);
147+
} else {
148+
throw new IllegalArgumentException("Reference fields must be a String or an array of Strings");
149+
}
150+
validateCT();
151+
return this.service.fetch(this.headers, this.contentTypeUid, this.params);
152+
}
124153
/**
125154
* <b>Fetches the list of all the entries of a particular content type.</b>
126155
* It also returns the content of each entry in JSON format. You can also

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import retrofit2.Call;
66
import retrofit2.http.*;
77

8+
import java.util.List;
89
import java.util.Map;
910

1011
public interface EntryService {
@@ -13,7 +14,7 @@ public interface EntryService {
1314
Call<ResponseBody> fetch(
1415
@HeaderMap Map<String, Object> headers,
1516
@Path("content_type_uid") String contentTypeUid,
16-
@QueryMap(encoded = true) Map<String, Object> queryParameter);
17+
@QueryMap(encoded = true) Map<String, Object> queryParameter);
1718

1819
@Headers("Content-Type: application/json")
1920
@GET("content_types/{content_type_uid}/entries/{entry_uid}")

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

Lines changed: 30 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,29 @@ 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+
@Test
104+
void testIncludeReferenceSingleReference(){
105+
Request request = entryInstance.includeReference("reference").request();
106+
Assertions.assertEquals("include[]=reference", request.url().query());
107+
}
108+
@Test
109+
void testIncludeReferenceMultipleReferences() {
110+
String[] array = {"reference","navigation_menu.page_reference"};
111+
Request req = entryInstance.includeReference(array).request();
112+
Assertions.assertEquals("include[]1=reference&include[]2=navigation_menu.page_reference", req.url().query());
113+
}
114+
115+
@Test
116+
void testIncludeReferenceMultipleReferencesWithParams() throws IOException {
117+
entryInstance.clearParams();
118+
entryInstance.addParam("locale", "en-us");
119+
String[] array = {"reference","navigation_menu.page_reference"};
120+
entryInstance.addParam("include_workflow", false);
121+
entryInstance.addParam("include_publish_details", true);
122+
Request req = entryInstance.includeReference(array).request();
123+
Assertions.assertEquals("include[]1=reference&include_publish_details=true&include[]2=navigation_menu.page_reference&locale=en-us&include_workflow=false", req.url().query());
124+
125+
}
96126

97127
@Test
98128
void testSingleEntryEncodedPath() {

0 commit comments

Comments
 (0)