Title: ClassCastExceptions on several different Graph API endpoints · Issue #641 · microsoftgraph/msgraph-sdk-java · GitHub
Open Graph Title: ClassCastExceptions on several different Graph API endpoints · Issue #641 · microsoftgraph/msgraph-sdk-java
X Title: ClassCastExceptions on several different Graph API endpoints · Issue #641 · microsoftgraph/msgraph-sdk-java
Description: Summary When executing a variety of different Graph API requests, we've noticed the following ClassCastExceptions occurring: java.lang.ClassCastException: java.io.BufferedInputStream cannot be cast to com.microsoft.graph.requests.extensi...
Open Graph Description: Summary When executing a variety of different Graph API requests, we've noticed the following ClassCastExceptions occurring: java.lang.ClassCastException: java.io.BufferedInputStream cannot be cast...
X Description: Summary When executing a variety of different Graph API requests, we've noticed the following ClassCastExceptions occurring: java.lang.ClassCastException: java.io.BufferedInputStream cannot be ...
Opengraph URL: https://github.com/microsoftgraph/msgraph-sdk-java/issues/641
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ClassCastExceptions on several different Graph API endpoints","articleBody":"## Summary\r\nWhen executing a variety of different Graph API requests, we've noticed the following ClassCastExceptions occurring:\r\n```\r\njava.lang.ClassCastException: java.io.BufferedInputStream cannot be cast to com.microsoft.graph.requests.extensions.DriveItemInviteCollectionResponse\r\n at com.microsoft.graph.requests.extensions.DriveItemInviteCollectionRequest.post(DriveItemInviteCollectionRequest.java:58)\r\n```\r\n\r\nOne example call:\r\n```\r\nIDriveItemInviteCollectionPage permissionsPage = graphClient.drives(driveId)\r\n .items(itemId)\r\n .invite(requireSignIn, roles, sendInvitation, message, driveRecipients)\r\n .buildRequest()\r\n .select(buildSelectClause(properties))\r\n .post();\r\n```\r\n\r\n### Why I filed a new issue\r\nThere was a similar issue filed at https://github.com/microsoftgraph/msgraph-sdk-java/issues/635. Noting that we are also using version 1.6.0. The reason why I filed this new issue is because there's some slight variance in the calls that are affected and I had additional information to share on why they are being affected from debugging through the issue. With this additional information, I wanted to confirm whether updating our library version would solve the issue. We have updated our library to 2.3.2 for newer versions of our application, and I'd like to verify that this would be sufficient for solving the issue since updating the library is non-trivial for our process.\r\n\r\n### Root Cause\r\nThe issue appears to be intermittent and is entirely dependent on what the SDK gets back from making an HTTP Request to the Graph API. The relevant code in the API is in CoreHttpProvider#sendRequestInternal seen below (https://github.com/microsoftgraph/msgraph-sdk-java/blob/1.6.0/src/main/java/com/microsoft/graph/http/CoreHttpProvider.java)\r\n```\r\n@SuppressWarnings(\"unchecked\")\r\nprivate \u003cResult, Body, DeserializeType> Result sendRequestInternal(final IHttpRequest request,\r\n\t\tfinal Class\u003cResult> resultClass,\r\n\t\tfinal Body serializable,\r\n\t\tfinal IProgressCallback\u003cResult> progress,\r\n\t\tfinal IStatefulResponseHandler\u003cResult, DeserializeType> handler)\r\n\t\t\t\tthrows ClientException {\r\n\t\t...\r\n\t\tin = new BufferedInputStream(response.body().byteStream());\r\n\r\n\t\tfinal Map\u003cString, String> headers = CoreHttpProvider.getResponseHeadersAsMapStringString(response);\r\n\r\n\t\tfinal String contentType = headers.get(Constants.CONTENT_TYPE_HEADER_NAME);\r\n\t\tif (contentType != null && contentType.contains(Constants.JSON_CONTENT_TYPE)) {\r\n\t\t\tlogger.logDebug(\"Response json\");\r\n\t\t\treturn handleJsonResponse(in, CoreHttpProvider.getResponseHeadersAsMapOfStringList(response), resultClass);\r\n\t\t} else {\r\n\t\t\tlogger.logDebug(\"Response binary\");\r\n\t\t\tisBinaryStreamInput = true;\r\n\t\t\t//no inspection unchecked\r\n\t\t\treturn (Result) handleBinaryStream(in);\r\n\t\t}\r\n\t\t...\r\n}\r\n```\r\n\r\nHere's the generic code that's used in the SDK for every Graph API request on 1.6.0 (previous version we're using). This code is deciding what kind of format to return the response in based on the \"Content-Type\" header and the caller of the method. It's deciding between whether to return a JSON object with information about the call made (for most calls), or a BufferedInputStream (for calls that deal with transferring data). \r\n\r\nThe root cause of the issue is that the \"Content-Type\" header can come in varying cases (some caps or all lowercase). The Graph API in 1.6.0 is looking for \"Content-Type\" case-sensitive to determine the content type. When the request returns \"content-type\", the code cannot find the correct header since it is case-sensitive and defaults to returning the BufferedInputStream. This leads to the exception we've been seeing.\r\n\r\nThe questions I have for the SDK team are:\r\n- Do we know whether or not this particular \"Content-Type\" header issue has been resolved in a later version? Based on what I was looking at in the 2.3.2 version of the SDK, it appears the issue would still exist unless the HTTP Response from the Graph API has been sanitized in some way to always guarantee the right case of \"Content-Type\"\r\n- What has changed in the Graph API to cause this to occur more frequently? As of this week, we've seen all of our calls run into this issue several times more often than in the past (where we've maybe encountered the issue once in our application)\r\n\r\n### Expected behavior\r\nWhen performing any Graph API call, we expect the proper contract object to be returned. For the following example:\r\n```\r\nIDriveItemInviteCollectionPage permissionsPage = graphClient.drives(driveId)\r\n .items(itemId)\r\n .invite(requireSignIn, roles, sendInvitation, message, driveRecipients)\r\n .buildRequest()\r\n .select(buildSelectClause(properties))\r\n .post();\r\n```\r\n\r\nWe expect a `IDriveItemInviteCollectionPage` to be returned.\r\n\r\n### Actual behavior\r\nThe following ClassCastException is returned:\r\n```\r\njava.lang.ClassCastException: java.io.BufferedInputStream cannot be cast to com.microsoft.graph.requests.extensions.DriveItemInviteCollectionResponse\r\n at com.microsoft.graph.requests.extensions.DriveItemInviteCollectionRequest.post(DriveItemInviteCollectionRequest.java:58)\r\n````\r\n\r\n### Steps to reproduce the behavior\r\nThe issue is intermittent and cannot be reliably reproduced each call. I see the issue occur 1 in 10 calls that I make using the SDK. \r\n[AB#7967](https://microsoftgraph.visualstudio.com/0985d294-5762-4bc2-a565-161ef349ca3e/_workitems/edit/7967)","author":{"url":"https://github.com/veevagoon","@type":"Person","name":"veevagoon"},"datePublished":"2021-02-02T22:54:05Z","url":"https://github.com/microsoftgraph/msgraph-sdk-java/issues/641"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:23e4097a-626d-88f6-6710-f7cf8c5aabf6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9FA4:F8826:1F91864:293CCB2:6B053A8C |
| html-safe-nonce | d44d9150e37c62c75c8fc7b9384701765ca29ec47c944115b363115bbd54e67a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RkE0OkY4ODI2OjFGOTE4NjQ6MjkzQ0NCMjo2QjA1M0E4QyIsInZpc2l0b3JfaWQiOiIzNTQ1MzU4MzQ5MTU3ODc0MzE2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7a54dd4be7723bbae592b8c5ec638acf4b10d87818babf0134087fff82e4badc |
| hovercard-subject-tag | issue:799788938 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoftgraph/msgraph-sdk-java/641/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7e14e5b5d8f31aa99eb0ad0c2d66b14d908fa51d40b7aeda9272f45d8291dd11/microsoftgraph/msgraph-sdk-java/issues/641 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7e14e5b5d8f31aa99eb0ad0c2d66b14d908fa51d40b7aeda9272f45d8291dd11/microsoftgraph/msgraph-sdk-java/issues/641 |
| og:image:alt | Summary When executing a variety of different Graph API requests, we've noticed the following ClassCastExceptions occurring: java.lang.ClassCastException: java.io.BufferedInputStream cannot be cast... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | veevagoon |
| hostname | github.com |
| expected-hostname | github.com |
| None | 854d880890b5e847b3fce9d1b31f032107baeddeebad2aa379c48aa4f1451f6b |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoftgraph/msgraph-sdk-java git https://github.com/microsoftgraph/msgraph-sdk-java.git |
| octolytics-dimension-user_id | 17304259 |
| octolytics-dimension-user_login | microsoftgraph |
| octolytics-dimension-repository_id | 103300409 |
| octolytics-dimension-repository_nwo | microsoftgraph/msgraph-sdk-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 103300409 |
| octolytics-dimension-repository_network_root_nwo | microsoftgraph/msgraph-sdk-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 220e980097d94ce616d5d11fbd5316d208e3fc93 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width