Skip to content

Commit 5b16e3b

Browse files
committed
Fix: Unable to receive null response from Cloud Code
`res.success(null)` from Cloud Code caused `ParseCloud.callFunctionInBackground()` to return a JSONObject, which would result in `ClassCastException` in developer code since they would normally expect a `String` or something. This change fixes that.
1 parent 08b1f23 commit 5b16e3b

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

Parse/src/main/java/com/parse/ParseCloud.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
import java.util.List;
1212
import java.util.Map;
1313

14-
import org.json.JSONException;
15-
import org.json.JSONObject;
16-
1714
import bolts.Continuation;
1815
import bolts.Task;
1916

Parse/src/main/java/com/parse/ParseCloudCodeController.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99
package com.parse;
1010

11-
import org.json.JSONException;
1211
import org.json.JSONObject;
1312

1413
import java.util.Map;
@@ -46,11 +45,7 @@ public T then(Task<JSONObject> task) throws Exception {
4645
/* package for test */ Object convertCloudResponse(Object result) {
4746
if (result instanceof JSONObject) {
4847
JSONObject jsonResult = (JSONObject)result;
49-
try {
50-
result = jsonResult.get("result");
51-
} catch (JSONException e) {
52-
return result;
53-
}
48+
result = jsonResult.opt("result");
5449
}
5550

5651
ParseDecoder decoder = ParseDecoder.get();

Parse/src/test/java/com/parse/ParseCloudCodeControllerTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void testCallFunctionInBackgroundCommand() throws Exception {
116116
}
117117

118118
@Test
119-
public void testCallFunctionInBackgroundSuccess() throws Exception {
119+
public void testCallFunctionInBackgroundSuccessWithResult() throws Exception {
120120
JSONObject json = new JSONObject();
121121
json.put("result", "test");
122122
String content = json.toString();
@@ -137,6 +137,27 @@ public void testCallFunctionInBackgroundSuccess() throws Exception {
137137
assertEquals("test", cloudCodeTask.getResult());
138138
}
139139

140+
@Test
141+
public void testCallFunctionInBackgroundSuccessWithoutResult() throws Exception {
142+
JSONObject json = new JSONObject();
143+
String content = json.toString();
144+
145+
ParseHttpResponse mockResponse = mock(ParseHttpResponse.class);
146+
when(mockResponse.getStatusCode()).thenReturn(200);
147+
when(mockResponse.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes()));
148+
when(mockResponse.getTotalSize()).thenReturn(content.length());
149+
150+
ParseHttpClient restClient = mockParseHttpClientWithReponse(mockResponse);
151+
ParseCloudCodeController controller = new ParseCloudCodeController(restClient);
152+
153+
Task<String> cloudCodeTask = controller.callFunctionInBackground(
154+
"test", new HashMap<String, Object>(), "sessionToken");
155+
ParseTaskUtils.wait(cloudCodeTask);
156+
157+
verify(restClient, times(1)).execute(any(ParseHttpRequest.class));
158+
assertNull(cloudCodeTask.getResult());
159+
}
160+
140161
@Test
141162
public void testCallFunctionInBackgroundFailure() throws Exception {
142163
// TODO(mengyan): Remove once we no longer rely on retry logic.

0 commit comments

Comments
 (0)