Skip to content

Commit 0266d0a

Browse files
authored
Merge pull request #166 from contentstack/fix/DX-2174
fix: added fixes for response type changes
2 parents 9a07330 + a1ca843 commit 0266d0a

File tree

14 files changed

+96
-64
lines changed

14 files changed

+96
-64
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.contentstack.sdk</groupId>
77
<artifactId>java</artifactId>
8-
<version>2.0.2</version>
8+
<version>2.0.3</version>
99
<packaging>jar</packaging>
1010
<name>contentstack-java</name>
1111
<description>Java SDK for Contentstack Content Delivery API</description>

src/main/java/com/contentstack/sdk/AssetModel.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.contentstack.sdk;
22

3+
import java.util.LinkedHashMap;
34
import org.json.JSONArray;
45
import org.json.JSONObject;
56

7+
68
/**
79
* The type Asset model.
810
*/
@@ -25,11 +27,10 @@ class AssetModel {
2527
* @param isArray the is array
2628
*/
2729
public AssetModel(JSONObject response, boolean isArray) {
28-
2930
if (isArray) {
3031
json = response;
3132
} else {
32-
json = response.optJSONObject("asset");
33+
json = new JSONObject((LinkedHashMap<?, ?>) response.get("asset"));
3334
}
3435

3536
if (json != null) {

src/main/java/com/contentstack/sdk/AssetsModel.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.contentstack.sdk;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
35
import org.json.JSONArray;
46
import org.json.JSONObject;
57

6-
import java.util.ArrayList;
7-
import java.util.List;
88

99
/**
1010
* The type Assets model.
@@ -19,7 +19,12 @@ class AssetsModel {
1919
* @param response the response
2020
*/
2121
public AssetsModel(JSONObject response) {
22-
JSONArray listResponse = response != null && response.has("assets") ? response.optJSONArray("assets") : null;
22+
JSONArray listResponse = null;
23+
Object rawAssets = response.get("assets"); // Get assets
24+
if (rawAssets instanceof List) { // Check if it's an ArrayList
25+
List<?> assetsList = (List<?>) rawAssets;
26+
listResponse = new JSONArray(assetsList); // Convert to JSONArray
27+
}
2328
if (listResponse != null) {
2429
listResponse.forEach(model -> {
2530
JSONObject modelObj = (JSONObject) model;

src/main/java/com/contentstack/sdk/CSConnectionRequest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.contentstack.sdk;
22

3-
import org.json.JSONObject;
4-
53
import java.util.HashMap;
64
import java.util.LinkedHashMap;
75
import java.util.List;
6+
import org.json.JSONObject;
7+
88

99
import static com.contentstack.sdk.Constants.*;
1010

@@ -128,7 +128,8 @@ public void onRequestFinished(CSHttpConnection request) {
128128
EntriesModel model = new EntriesModel(jsonResponse);
129129
notifyClass.getResultObject(model.objectList, jsonResponse, true);
130130
} else if (request.getController().equalsIgnoreCase(Constants.FETCHENTRY)) {
131-
EntryModel model = new EntryModel(jsonResponse);
131+
JSONObject jsonModel = new JSONObject((LinkedHashMap<?, ?>) jsonResponse.get("entry"));
132+
EntryModel model = new EntryModel(jsonModel);
132133
entryInstance.resultJson = model.jsonObject;
133134
entryInstance.title = model.title;
134135
entryInstance.url = model.url;

src/main/java/com/contentstack/sdk/CSHttpConnection.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package com.contentstack.sdk;
22

3-
import okhttp3.Request;
4-
import okhttp3.ResponseBody;
5-
6-
import org.json.JSONArray;
7-
import org.json.JSONException;
8-
import org.json.JSONObject;
9-
10-
import retrofit2.Call;
11-
import retrofit2.Response;
12-
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.json.JsonMapper;
5+
import com.fasterxml.jackson.databind.type.MapType;
136
import java.io.IOException;
147
import java.io.UnsupportedEncodingException;
15-
import java.net.URLEncoder;
168
import java.net.SocketTimeoutException;
9+
import java.net.URLEncoder;
1710
import java.nio.charset.StandardCharsets;
1811
import java.util.HashMap;
1912
import java.util.Iterator;
@@ -22,10 +15,16 @@
2215
import java.util.logging.Level;
2316
import java.util.logging.Logger;
2417
import java.util.stream.IntStream;
25-
import com.fasterxml.jackson.databind.ObjectMapper; // Jackson for JSON parsing
26-
import com.fasterxml.jackson.databind.json.JsonMapper;
27-
import com.fasterxml.jackson.databind.node.ObjectNode;
28-
import com.fasterxml.jackson.databind.type.MapType;
18+
import okhttp3.Request;
19+
import okhttp3.ResponseBody;
20+
import org.json.JSONArray;
21+
import org.json.JSONException;
22+
import org.json.JSONObject;
23+
import retrofit2.Call;
24+
import retrofit2.Response;
25+
26+
27+
2928

3029
import static com.contentstack.sdk.Constants.*;
3130

@@ -230,7 +229,7 @@ private void getService(String requestUrl) throws IOException {
230229
MapType type = mapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class,
231230
Object.class);
232231
Map<String, Object> responseMap = mapper.readValue(response.body().string(), type);
233-
232+
234233
// Use the custom method to create an ordered JSONObject
235234
responseJSON = createOrderedJSONObject(responseMap);
236235
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {

src/main/java/com/contentstack/sdk/ContentTypesModel.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.contentstack.sdk;
22

3+
import java.util.ArrayList;
4+
import java.util.LinkedHashMap;
5+
import java.util.List;
36
import org.json.JSONArray;
47
import org.json.JSONObject;
58

9+
10+
611
/**
712
* The ContentTypesModel that contains content type response
813
*/
@@ -12,16 +17,26 @@ public class ContentTypesModel {
1217
private JSONArray responseJSONArray = new JSONArray();
1318

1419
public void setJSON(JSONObject responseJSON) {
15-
1620
if (responseJSON != null) {
1721
String ctKey = "content_type";
18-
if (responseJSON.has(ctKey) && responseJSON.opt(ctKey) instanceof JSONObject) {
19-
this.response = responseJSON.optJSONObject(ctKey);
22+
if (responseJSON.has(ctKey) && responseJSON.opt(ctKey) instanceof LinkedHashMap) {
23+
this.response = new JSONObject((LinkedHashMap<?, ?>) responseJSON.get(ctKey));
2024
}
2125
String ctListKey = "content_types";
22-
if (responseJSON.has(ctListKey) && responseJSON.opt(ctListKey) instanceof JSONArray) {
23-
this.response = responseJSON.optJSONArray(ctListKey);
24-
this.responseJSONArray = (JSONArray) this.response;
26+
if (responseJSON.has(ctListKey) && responseJSON.opt(ctListKey) instanceof ArrayList) {
27+
ArrayList<LinkedHashMap<?, ?>> contentTypes = (ArrayList) responseJSON.get(ctListKey);
28+
List<Object> objectList = new ArrayList<>();
29+
if (!contentTypes.isEmpty()) {
30+
contentTypes.forEach(model -> {
31+
if (model instanceof LinkedHashMap) {
32+
// Convert LinkedHashMap to JSONObject
33+
JSONObject jsonModel = new JSONObject((LinkedHashMap<?, ?>) model);
34+
objectList.add(jsonModel);
35+
}
36+
});
37+
}
38+
this.response = new JSONArray(objectList);
39+
this.responseJSONArray = new JSONArray(objectList);
2540
}
2641
}
2742
}

src/main/java/com/contentstack/sdk/EntryModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.contentstack.sdk;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
35
import org.json.JSONArray;
46
import org.json.JSONObject;
57

6-
import java.util.HashMap;
7-
import java.util.Map;
88

99
class EntryModel {
1010

@@ -40,6 +40,7 @@ class EntryModel {
4040

4141
public EntryModel(JSONObject response) {
4242
this.jsonObject = response;
43+
4344
if (this.jsonObject.has(ENTRY_KEY)) {
4445
this.jsonObject = jsonObject.optJSONObject(ENTRY_KEY);
4546
}
@@ -59,7 +60,6 @@ public EntryModel(JSONObject response) {
5960
if (this.jsonObject.has("description")) {
6061
this.description = this.jsonObject.opt("description");
6162
}
62-
6363
this.images = (JSONArray) this.jsonObject.opt("images");
6464
this.isDirectory = (Boolean) this.jsonObject.opt("is_dir");
6565
this.updatedAt = (String) this.jsonObject.opt("updated_at");

src/main/java/com/contentstack/sdk/Query.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.contentstack.sdk;
22

3+
import java.util.*;
4+
import java.util.logging.Level;
5+
import java.util.logging.Logger;
36
import org.jetbrains.annotations.NotNull;
47
import org.jetbrains.annotations.Nullable;
58
import org.json.JSONArray;
69
import org.json.JSONObject;
710

8-
import java.util.*;
9-
import java.util.logging.Level;
10-
import java.util.logging.Logger;
1111

1212
import static com.contentstack.sdk.Constants.*;
1313

@@ -1226,7 +1226,6 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean
12261226
entry.setTags(((EntryModel) object).tags);
12271227
objectList.add(entry);
12281228
}
1229-
12301229
if (isSingleEntry) {
12311230
Entry entry = contentTypeInstance.entry();
12321231
if (!objectList.isEmpty()) {

src/main/java/com/contentstack/sdk/QueryResult.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.contentstack.sdk;
22

3-
import org.json.JSONArray;
4-
import org.json.JSONObject;
5-
63
import java.util.List;
74
import java.util.Map;
85
import java.util.logging.Level;
96
import java.util.logging.Logger;
7+
import org.json.JSONArray;
8+
import org.json.JSONObject;
9+
1010

1111
/**
1212
* QueryResult works as the Query Response that works as getter as per the Json Key

src/main/java/com/contentstack/sdk/SyncStack.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.contentstack.sdk;
22

3+
import java.util.ArrayList;
4+
import java.util.LinkedHashMap;
5+
import java.util.List;
36
import org.jetbrains.annotations.NotNull;
47
import org.json.JSONArray;
58
import org.json.JSONObject;
69

7-
import java.util.ArrayList;
8-
import java.util.List;
910

1011
/**
1112
* Synchronization: The Sync API takes care of syncing your Contentstack data
@@ -59,7 +60,18 @@ public List<JSONObject> getItems() {
5960
protected void setJSON(@NotNull JSONObject jsonobject) {
6061
this.receiveJson = jsonobject;
6162
if (receiveJson.has("items")) {
62-
JSONArray jsonarray = receiveJson.getJSONArray("items");
63+
ArrayList<LinkedHashMap<?, ?>> items = (ArrayList) this.receiveJson.get("items");
64+
List<Object> objectList = new ArrayList<>();
65+
if (!items.isEmpty()) {
66+
items.forEach(model -> {
67+
if (model instanceof LinkedHashMap) {
68+
// Convert LinkedHashMap to JSONObject
69+
JSONObject jsonModel = new JSONObject((LinkedHashMap<?, ?>) model);
70+
objectList.add(jsonModel);
71+
}
72+
});
73+
}
74+
JSONArray jsonarray = new JSONArray(objectList);
6375
if (jsonarray != null) {
6476
syncItems = new ArrayList<>();
6577
for (int position = 0; position < jsonarray.length(); position++) {

0 commit comments

Comments
 (0)