Skip to content

Commit d961e5b

Browse files
committed
add fix for transforming array data
1 parent fcde214 commit d961e5b

2 files changed

Lines changed: 134 additions & 17 deletions

File tree

android/src/main/java/com/onesignal/rnonesignalandroid/RNUtils.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.facebook.react.bridge.ReadableMap;
66
import com.facebook.react.bridge.ReadableMapKeySetIterator;
77
import com.facebook.react.bridge.ReadableType;
8+
import com.facebook.react.bridge.WritableArray;
89
import com.facebook.react.bridge.WritableMap;
910
import com.onesignal.inAppMessages.IInAppMessage;
1011
import com.onesignal.inAppMessages.IInAppMessageClickEvent;
@@ -51,6 +52,8 @@ public static WritableMap convertHashMapToWritableMap(HashMap<String, Object> ha
5152
writableMap.putDouble(key, ((Long) value).doubleValue());
5253
} else if (value instanceof HashMap) {
5354
writableMap.putMap(key, convertHashMapToWritableMap((HashMap<String, Object>) value));
55+
} else if (value instanceof List) {
56+
writableMap.putArray(key, convertListToWritableArray((List<Object>) value));
5457
} else {
5558
writableMap.putNull(key);
5659
}
@@ -287,4 +290,30 @@ private static List<Object> convertJSONArrayToList(JSONArray array) throws JSONE
287290

288291
return list;
289292
}
293+
294+
private static WritableArray convertListToWritableArray(List<Object> list) throws JSONException {
295+
WritableArray writableArray = Arguments.createArray();
296+
for (Object item : list) {
297+
if (item instanceof String) {
298+
writableArray.pushString((String) item);
299+
} else if (item instanceof Boolean) {
300+
writableArray.pushBoolean((Boolean) item);
301+
} else if (item instanceof Integer) {
302+
writableArray.pushInt((Integer) item);
303+
} else if (item instanceof Double) {
304+
writableArray.pushDouble((Double) item);
305+
} else if (item instanceof Float) {
306+
writableArray.pushDouble(((Float) item).doubleValue());
307+
} else if (item instanceof Long) {
308+
writableArray.pushDouble(((Long) item).doubleValue());
309+
} else if (item instanceof HashMap) {
310+
writableArray.pushMap(convertHashMapToWritableMap((HashMap<String, Object>) item));
311+
} else if (item instanceof List) {
312+
writableArray.pushArray(convertListToWritableArray((List<Object>) item));
313+
} else {
314+
writableArray.pushNull();
315+
}
316+
}
317+
return writableArray;
318+
}
290319
}

test.sh

Lines changed: 105 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,110 @@ set -a
55
[ -f .env ] && . .env
66
set +a
77

8+
# Test data
9+
read -r -d '' TEST_DATA_1 << 'EOF'
10+
{
11+
"route": "Search",
12+
"basket": [
13+
{
14+
"sku": "12qw3er56y",
15+
"itemName": "Hand-crafted Peppermint Candy Canes",
16+
"qty": 3,
17+
"cost": "9.99"
18+
},
19+
{
20+
"sku": "90ujr5383",
21+
"itemName": "Fat Squirrel",
22+
"qty": 1,
23+
"cost": "29.99"
24+
}
25+
]
26+
}
27+
EOF
28+
29+
read -r -d '' TEST_DATA_2 << 'EOF'
30+
{
31+
"route": "ComplexTest",
32+
"stringValue": "test string",
33+
"booleanValue": true,
34+
"integerValue": 42,
35+
"doubleValue": 3.14159,
36+
"nullValue": null,
37+
"stringArray": ["apple", "banana", "cherry"],
38+
"numberArray": [1, 2, 3, 4, 5],
39+
"booleanArray": [true, false, true],
40+
"mixedArray": [
41+
"string item",
42+
123,
43+
45.67,
44+
true,
45+
false,
46+
null,
47+
{
48+
"nestedString": "value",
49+
"nestedNumber": 999,
50+
"nestedBoolean": false
51+
},
52+
[
53+
"nested array item 1",
54+
"nested array item 2",
55+
789
56+
]
57+
],
58+
"nestedObjects": {
59+
"level1": {
60+
"level2": {
61+
"level3": {
62+
"deepValue": "deeply nested",
63+
"deepNumber": 1000,
64+
"deepArray": [1, 2, 3]
65+
}
66+
}
67+
}
68+
},
69+
"arrayOfObjects": [
70+
{
71+
"id": 1,
72+
"name": "Item One",
73+
"active": true,
74+
"price": 19.99,
75+
"tags": ["tag1", "tag2"]
76+
},
77+
{
78+
"id": 2,
79+
"name": "Item Two",
80+
"active": false,
81+
"price": 29.99,
82+
"tags": ["tag3"],
83+
"metadata": {
84+
"created": "2024-01-01",
85+
"updated": null
86+
}
87+
},
88+
{
89+
"id": 3,
90+
"name": null,
91+
"active": true,
92+
"price": 0,
93+
"tags": []
94+
}
95+
],
96+
"nestedArrays": [
97+
[1, 2, 3],
98+
["a", "b", "c"],
99+
[true, false],
100+
[
101+
{
102+
"item": "nested in array in array",
103+
"value": 42
104+
}
105+
]
106+
],
107+
"emptyArray": [],
108+
"emptyObject": {}
109+
}
110+
EOF
111+
8112
curl --request POST \
9113
--url 'https://api.onesignal.com/notifications?c=push' \
10114
--header "Authorization: Key $API_KEY" \
@@ -27,21 +131,5 @@ curl --request POST \
27131
\"en\": \"Test Subtitle\"
28132
},
29133
\"name\": \"abc123\",
30-
\"data\": {
31-
\"route\": \"Search\",
32-
\"basket\": [
33-
{
34-
\"sku\": \"12qw3er56y\",
35-
\"itemName\": \"Hand-crafted Peppermint Candy Canes\",
36-
\"qty\": 3,
37-
\"cost\": \"9.99\"
38-
},
39-
{
40-
\"sku\": \"90ujr5383\",
41-
\"itemName\": \"Fat Squirrel\",
42-
\"qty\": 1,
43-
\"cost\": \"29.99\"
44-
}
45-
]
46-
}
134+
\"data\": $TEST_DATA_2
47135
}"

0 commit comments

Comments
 (0)