Skip to content

Commit 5795828

Browse files
authored
Return null result properly for cloud function (#862)
1 parent 0cfc67e commit 5795828

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public T then(Task<JSONObject> task) {
4545
/* package for test */ Object convertCloudResponse(Object result) {
4646
if (result instanceof JSONObject) {
4747
JSONObject jsonResult = (JSONObject) result;
48+
// We want to make sure we pass back a null result as null, and not a JSONObject
49+
if (jsonResult.isNull("result")) {
50+
return null;
51+
}
4852
result = jsonResult.opt("result");
4953
}
5054

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,28 @@ public void testCallFunctionInBackgroundFailure() throws Exception {
185185

186186
//endregion
187187

188+
@Test
189+
public void testCallFunctionWithNullResult() throws Exception {
190+
String content = "{ result: null }";
191+
192+
ParseHttpResponse mockResponse = new ParseHttpResponse.Builder()
193+
.setStatusCode(200)
194+
.setTotalSize((long) content.length())
195+
.setContent(new ByteArrayInputStream(content.getBytes()))
196+
.build();
197+
198+
ParseHttpClient restClient = mockParseHttpClientWithReponse(mockResponse);
199+
ParseCloudCodeController controller = new ParseCloudCodeController(restClient);
200+
201+
Task<String> cloudCodeTask = controller.callFunctionInBackground(
202+
"test", new HashMap<String, Object>(), "sessionToken");
203+
ParseTaskUtils.wait(cloudCodeTask);
204+
205+
verify(restClient, times(1)).execute(any(ParseHttpRequest.class));
206+
String result = cloudCodeTask.getResult();
207+
assertEquals(null, result);
208+
}
209+
188210
private ParseHttpClient mockParseHttpClientWithReponse(ParseHttpResponse response)
189211
throws IOException {
190212
ParseHttpClient client = mock(ParseHttpClient.class);

0 commit comments

Comments
 (0)