Skip to content

DX | 09-06-2025 | Release #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## v2.1.3

### Date: 06-Jun-2025

- Fixed SyncStack to handle ArrayList

## v2.1.2

### Date: 26-May-2025
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.contentstack.sdk</groupId>
<artifactId>java</artifactId>
<version>2.1.2</version>
<version>2.1.3</version>
<packaging>jar</packaging>
<name>contentstack-java</name>
<description>Java SDK for Contentstack Content Delivery API</description>
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/com/contentstack/sdk/SyncStack.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.contentstack.sdk;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;


Expand Down Expand Up @@ -68,6 +70,7 @@ protected synchronized void setJSON(@NotNull JSONObject jsonobject) {

if (receiveJson.has("items")) {
Object itemsObj = receiveJson.opt("items");

if (itemsObj instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) itemsObj;
syncItems = new ArrayList<>();
Expand All @@ -77,14 +80,26 @@ protected synchronized void setJSON(@NotNull JSONObject jsonobject) {
syncItems.add(sanitizeJson(jsonItem));
}
}
} else {
if (itemsObj instanceof JSONObject) {
syncItems = new ArrayList<>();
syncItems.add(sanitizeJson((JSONObject) itemsObj));
} else {
logger.warning("'items' is not a valid list. Skipping processing.");
syncItems = new ArrayList<>();
} else if (itemsObj instanceof JSONObject) {
syncItems = new ArrayList<>();
syncItems.add(sanitizeJson((JSONObject) itemsObj));
} else if (itemsObj instanceof List) {
List<?> itemsList = (List<?>) itemsObj;
syncItems = new ArrayList<>();
for (Object item : itemsList) {
if (item instanceof JSONObject) {
syncItems.add(sanitizeJson((JSONObject) item));
} else if (item instanceof Map) {
JSONObject jsonItem = new JSONObject((Map<?, ?>) item);
syncItems.add(sanitizeJson(jsonItem));
} else {
logger.warning("Item in ArrayList is not a JSONObject or LinkedHashMap. Skipping. Type: " + item.getClass().getName());
}
}
} else {
logger.warning("'items' is not a valid JSONArray, JSONObject, or ArrayList. Type: " +
(itemsObj != null ? itemsObj.getClass().getName() : "null"));
syncItems = new ArrayList<>();
}
} else {
syncItems = new ArrayList<>();
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/com/contentstack/sdk/TestSyncStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class TestSyncStack {
private SyncStack syncStack;
private final Stack stack = Credentials.getStack();
private final String host = Credentials.HOST;

@BeforeEach
void setUp() {
Expand Down Expand Up @@ -176,4 +182,37 @@ void testSetJSON_ThreadSafety() throws InterruptedException {

assertFalse(syncStack.getItems().isEmpty()); // No race conditions
}

/**
* ✅ Test: Real API call to syncContentType
*/
@Test
void testRealSyncContentType() throws IllegalAccessException {
// Create a CountDownLatch to wait for the async call to complete
CountDownLatch latch = new CountDownLatch(1);
// Make the actual API call
stack.syncContentType("product", new SyncResultCallBack() {
@Override
public void onCompletion(SyncStack syncStack, Error error) {
if (error != null) {
fail("Sync failed with error: " + error.getErrorMessage());
}
// Verify the response
assertNotNull(syncStack.getJSONResponse());
assertNull(syncStack.getUrl());
assertNotNull(syncStack.getItems());
assertFalse(syncStack.getItems().isEmpty());
assertTrue(syncStack.getCount() > 0);

latch.countDown();
}
});

try {
// Wait for the async call to complete (with timeout)
assertTrue(latch.await(10, TimeUnit.SECONDS), "Sync operation timed out");
} catch (InterruptedException e) {
fail("Test was interrupted: " + e.getMessage());
}
}
}
Loading