Repository: microsoftgraph/msgraph-sdk-java
Issue: Error when trying to download .JSON files from OneDrive

I am trying to download a .json file from OneDrive. The following request patterns get the intended behavior (the file is downloaded) for other file types but none work for .json files.
InputStream driveItemInputStream = getGraphServiceClient().drives(driveId).items(driveItemId).content().buildRequest().get();
InputStream driveItemInputStream = getGraphServiceClient().drives().byId(driveId).items().byId(driveItemId).content().buildRequest().get();
InputStream driveItemInputStream = getGraphServiceClient().customRequest(("/drives/"+driveId+"/items/"+driveItemId+"/content"), InputStream.class).buildRequest().get();

Expected behavior
For the file to be downloaded.
Actual behavior
Error response:
com.microsoft.graph.core.ClientException: Error during http request
at com.microsoft.graph.http.DefaultHttpProvider.sendRequestInternal(DefaultHttpProvider.java:368)
	at com.microsoft.graph.http.DefaultHttpProvider.send(DefaultHttpProvider.java:204)
	at com.microsoft.graph.http.DefaultHttpProvider.send(DefaultHttpProvider.java:184)
	at com.microsoft.graph.http.BaseStreamRequest.send(BaseStreamRequest.java:81)
	at com.microsoft.graph.requests.extensions.DriveItemStreamRequest.get(DriveItemStreamRequest.java:55)
...
Caused by: java.lang.RuntimeException: Failed to invoke public java.io.InputStream() with no args
	at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:113)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)
	at com.google.gson.Gson.fromJson(Gson.java:927)
	at com.google.gson.Gson.fromJson(Gson.java:892)
	at com.google.gson.Gson.fromJson(Gson.java:841)
	at com.google.gson.Gson.fromJson(Gson.java:813)
	at com.microsoft.graph.serializer.DefaultSerializer.deserializeObject(DefaultSerializer.java:78)
	at com.microsoft.graph.http.DefaultHttpProvider.handleJsonResponse(DefaultHttpProvider.java:417)
	at com.microsoft.graph.http.DefaultHttpProvider.sendRequestInternal(DefaultHttpProvider.java:342)
	... 46 more


Steps to reproduce the behavior
Use the SDK to make a GET request on a .json file in a user or group OneDrive using any of the request patterns above.
[AB#6032](https://microsoftgraph.visualstudio.com/0985d294-5762-4bc2-a565-161ef349ca3e/_workitems/edit/6032)

Changed files:
src/test/java/com/microsoft/graph/functional/OneDriveTests.java, src/main/java/com/microsoft/graph/http/CoreHttpProvider.java

diff --git a/src/main/java/com/microsoft/graph/http/CoreHttpProvider.java b/src/main/java/com/microsoft/graph/http/CoreHttpProvider.java
index 1cb2cf2db9..d10f8cb610 100644
--- a/src/main/java/com/microsoft/graph/http/CoreHttpProvider.java
+++ b/src/main/java/com/microsoft/graph/http/CoreHttpProvider.java
@@ -417,20 +417,20 @@ public class CoreHttpProvider implements IHttpProvider {
 
 				final Map<String, String> headers = CoreHttpProvider.getResponseHeadersAsMapStringString(response);
 
+				if(response.body() == null || response.body().contentLength() == 0)
+					return (Result) null;
+
 				final String contentType = headers.get(Constants.CONTENT_TYPE_HEADER_NAME);
-				if (contentType != null && contentType.contains(Constants.JSON_CONTENT_TYPE)) {
+				if (contentType != null && resultClass != InputStream.class && 
+							contentType.contains(Constants.JSON_CONTENT_TYPE)) {
 					logger.logDebug("Response json");
 					return handleJsonResponse(in, CoreHttpProvider.getResponseHeadersAsMapOfStringList(response), resultClass);
-				} else {
+				} else if (resultClass == InputStream.class) {
 					logger.logDebug("Response binary");
 					isBinaryStreamInput = true;
-					if (resultClass == InputStream.class) {
-						return (Result) handleBinaryStream(in);
-					} else if(response.body() != null && response.body().contentLength() > 0) { // some services reply in text/plain with a JSON representation...
-						return handleJsonResponse(in, CoreHttpProvider.getResponseHeadersAsMapOfStringList(response), resultClass);
-					} else {
-						return (Result) null;
-					}
+					return (Result) handleBinaryStream(in);
+				} else {
+					return (Result) null;
 				}
 			} finally {
 				if (!isBinaryStreamInput) {
diff --git a/src/test/java/com/microsoft/graph/functional/OneDriveTests.java b/src/test/java/com/microsoft/graph/functional/OneDriveTests.java
index e1d57aafb6..a0792f2e1a 100644
--- a/src/test/java/com/microsoft/graph/functional/OneDriveTests.java
+++ b/src/test/java/com/microsoft/graph/functional/OneDriveTests.java
@@ -1,18 +1,23 @@
 package com.microsoft.graph.functional;
 
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 
+import com.google.gson.JsonPrimitive;
 import com.microsoft.graph.concurrency.ChunkedUploadProvider;
 import com.microsoft.graph.concurrency.IProgressCallback;
 import com.microsoft.graph.core.ClientException;
+import com.microsoft.graph.http.CoreHttpProvider;
 import com.microsoft.graph.models.extensions.DriveItem;
 import com.microsoft.graph.models.extensions.DriveItemUploadableProperties;
 import com.microsoft.graph.models.extensions.UploadSession;
@@ -25,7 +30,25 @@ public class OneDriveTests {
     public void setUp() {
        testBase = new TestBase();
     }
-    
+	
+	IProgressCallback<DriveItem> callback = new IProgressCallback<DriveItem> () {
+		@Override
+		public void progress(final long current, final long max) {
+			//Check progress
+		}
+		@Override
+		public void success(final DriveItem result) {
+			//Handle the successful response
+			String finishedItemId = result.id;
+			Assert.assertNotNull(finishedItemId);
+		}
+		
+		@Override
+		public void failure(final ClientException ex) {
+			//Handle the failed upload
+			Assert.fail("Upload session failed");
+		}
+	};
     /**
      * Test large file upload. 
      * https://github.com/OneDrive/onedrive-sdk-csharp/blob/master/docs/chunked-uploads.md
@@ -41,25 +64,6 @@ public class OneDriveTests {
     	InputStream uploadFile = OneDriveTests.class.getClassLoader().getResourceAsStream("hamilton.jpg");
     	long fileSize = (long) uploadFile.available();
     	
-    	IProgressCallback<DriveItem> callback = new IProgressCallback<DriveItem> () {
-    		@Override
-        	public void progress(final long current, final long max) {
-        		//Check progress
-        	}
-    		@Override
-    		public void success(final DriveItem result) {
-    			//Handle the successful response
-    			String finishedItemId = result.id;
-    			Assert.assertNotNull(finishedItemId);
-    		}
-    		
-    		@Override
-    		public void failure(final ClientException ex) {
-    			//Handle the failed upload
-    			Assert.fail("Upload session failed");
-    		}
-    	};
-    	
     	UploadSession uploadSession = testBase
     			.graphClient
     			.me()
@@ -85,4 +89,43 @@ public class OneDriveTests {
 		   assertFalse("stream should not be empty", stream.read() == -1);
 		}
 	}
+	@Test
+	public void downloadJsonFileFromOneDrive() throws Exception {
+		final DriveItemUploadableProperties item = new DriveItemUploadableProperties();
+		item.name = "test.json";
+		item.additionalDataManager().put("@microsoft.graph.conflictBehavior", new JsonPrimitive("replace"));
+
+		final InputStream uploadFile = new ByteArrayInputStream("{\\"hehe\\":\\"haha\\"}".getBytes(StandardCharsets.UTF_8));
+
+    	final long fileSize = (long) uploadFile.available();
+
+		final UploadSession session = testBase.graphClient.me()
+			.drive()
+			.root()
+			.itemWithPath(item.name)
+			.createUploadSession(item)
+			.buildRequest()
+			.post();
+
+		ChunkedUploadProvider<DriveItem> chunkedUploadProvider = new ChunkedUploadProvider<DriveItem>(
+    			session, 
+    			testBase.graphClient, 
+    			uploadFile, 
+    			fileSize, 
+    			DriveItem.class);
+		
+		chunkedUploadProvider.upload(callback);
+		
+		final InputStream stream = testBase.graphClient.me()
+			.drive()
+			.root()
+			.itemWithPath(item.name)
+			.content()
+			.buildRequest()
+			.get();
+
+		final String fileContent = CoreHttpProvider.streamToString(stream);
+
+		assertTrue(fileContent.length() > 0);
+	}
 }