Skip to content

Commit c490412

Browse files
committed
- fixes a bug where downloading json files from onedrive would cause a cast exception
1 parent 6e7acc8 commit c490412

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/main/java/com/microsoft/graph/core/Constants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ private Constants() {
1717
* The content type for JSON responses
1818
*/
1919
public static final String JSON_CONTENT_TYPE = "application/json";
20+
/**
21+
* The content type for text responses
22+
*/
23+
public static final String TEXT_CONTENT_TYPE = "text/plain";
2024
/**
2125
* The binary content type header's value
2226
*/

src/main/java/com/microsoft/graph/http/CoreHttpProvider.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -417,20 +417,20 @@ public Request authenticateRequest(Request request) {
417417

418418
final Map<String, String> headers = CoreHttpProvider.getResponseHeadersAsMapStringString(response);
419419

420+
if(response.body() == null || response.body().contentLength() == 0)
421+
return (Result) null;
422+
420423
final String contentType = headers.get(Constants.CONTENT_TYPE_HEADER_NAME);
421-
if (contentType != null && contentType.contains(Constants.JSON_CONTENT_TYPE)) {
424+
if (contentType != null && resultClass != InputStream.class &&
425+
(contentType.contains(Constants.JSON_CONTENT_TYPE) || contentType.contains(Constants.TEXT_CONTENT_TYPE))) { // some services reply in text/plain with a JSON representation...
422426
logger.logDebug("Response json");
423427
return handleJsonResponse(in, CoreHttpProvider.getResponseHeadersAsMapOfStringList(response), resultClass);
424-
} else {
428+
} else if (resultClass == InputStream.class) {
425429
logger.logDebug("Response binary");
426430
isBinaryStreamInput = true;
427-
if (resultClass == InputStream.class) {
428-
return (Result) handleBinaryStream(in);
429-
} else if(response.body() != null && response.body().contentLength() > 0) { // some services reply in text/plain with a JSON representation...
430-
return handleJsonResponse(in, CoreHttpProvider.getResponseHeadersAsMapOfStringList(response), resultClass);
431-
} else {
432-
return (Result) null;
433-
}
431+
return (Result) handleBinaryStream(in);
432+
} else {
433+
return (Result) null;
434434
}
435435
} finally {
436436
if (!isBinaryStreamInput) {

src/test/java/com/microsoft/graph/functional/OneDriveTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.microsoft.graph.functional;
22

33
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
45

56
import java.io.IOException;
67
import java.io.InputStream;
@@ -13,6 +14,7 @@
1314
import com.microsoft.graph.concurrency.ChunkedUploadProvider;
1415
import com.microsoft.graph.concurrency.IProgressCallback;
1516
import com.microsoft.graph.core.ClientException;
17+
import com.microsoft.graph.http.CoreHttpProvider;
1618
import com.microsoft.graph.models.extensions.DriveItem;
1719
import com.microsoft.graph.models.extensions.DriveItemUploadableProperties;
1820
import com.microsoft.graph.models.extensions.UploadSession;
@@ -85,4 +87,18 @@ public void testDownloadWithCustomRequest() throws IOException {
8587
assertFalse("stream should not be empty", stream.read() == -1);
8688
}
8789
}
90+
@Test
91+
public void downloadJsonFileFromOneDrive() throws Exception {
92+
final InputStream stream = testBase.graphClient.me()
93+
.drive()
94+
.root()
95+
.itemWithPath("test.json")
96+
.content()
97+
.buildRequest()
98+
.get();
99+
100+
final String fileContent = CoreHttpProvider.streamToString(stream);
101+
102+
assertTrue(fileContent.length() > 0);
103+
}
88104
}

0 commit comments

Comments
 (0)