-
Notifications
You must be signed in to change notification settings - Fork 223
Fix #1426 IllegalStateException when deserializing message using conversations.history #1429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
slack-api-model/src/main/java/com/slack/api/util/json/GsonFileFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.slack.api.util.json; | ||
|
||
import com.google.gson.*; | ||
import com.slack.api.model.File; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
public class GsonFileFactory implements JsonDeserializer<File>, JsonSerializer<File> { | ||
|
||
// This is just a workaround to customize Gson library behavior | ||
// You don't need to edit this class at all | ||
static class NormalizedFile extends File { | ||
} | ||
|
||
private boolean failOnUnknownProperties; | ||
|
||
public GsonFileFactory() { | ||
this(false); | ||
} | ||
|
||
public GsonFileFactory(boolean failOnUnknownProperties) { | ||
this.failOnUnknownProperties = failOnUnknownProperties; | ||
} | ||
|
||
@Override | ||
public File deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
throws JsonParseException { | ||
final JsonObject jsonObject = json.getAsJsonObject(); | ||
// Remove unusual data structure form Slack API server | ||
// As the starting point in Jan 2025, we just ignore these non-array properties, | ||
// but we may want to assign to a different field if it's necessary for some use cases | ||
// See https://github.com/slackapi/java-slack-sdk/issues/1426 for more details | ||
if (jsonObject.has("favorites") && !jsonObject.get("favorites").isJsonArray()) { | ||
jsonObject.remove("favorites"); | ||
} | ||
if (jsonObject.has("channels") && !jsonObject.get("channels").isJsonArray()) { | ||
jsonObject.remove("channels"); | ||
} | ||
if (jsonObject.has("ims") && !jsonObject.get("ims").isJsonArray()) { | ||
jsonObject.remove("ims"); | ||
} | ||
if (jsonObject.has("groups") && !jsonObject.get("groups").isJsonArray()) { | ||
jsonObject.remove("groups"); | ||
} | ||
if (jsonObject.has("shares")) { | ||
JsonObject shares = jsonObject.get("shares").getAsJsonObject(); | ||
if (shares.has("public")) { | ||
adjustSharesObjects(shares.get("public").getAsJsonObject()); | ||
} | ||
if (shares.has("private")) { | ||
adjustSharesObjects(shares.get("private").getAsJsonObject()); | ||
} | ||
} | ||
// To prevent StackOverflowError here, run the deserialize method for File's subclass. | ||
// If we want to attach the above unusual data, you can add it to File class | ||
return context.deserialize(jsonObject, NormalizedFile.class); | ||
} | ||
|
||
private void adjustSharesObjects(JsonObject shares) { | ||
for (String channelId : shares.keySet()) { | ||
for (JsonElement elem : shares.get(channelId).getAsJsonArray()) { | ||
JsonObject e = elem.getAsJsonObject(); | ||
if (e.has("reply_users") && !e.get("reply_users").isJsonArray()) { | ||
// As the starting point in Jan 2025, we just ignore this property, | ||
// but we may want to assign to a different field if it's necessary for some use cases | ||
e.remove("reply_users"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public JsonElement serialize(File src, Type typeOfSrc, JsonSerializationContext context) { | ||
return context.serialize(src); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
slack-api-model/src/test/java/test_locally/api/model/FileTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package test_locally.api.model; | ||
|
||
import com.slack.api.model.File; | ||
import org.junit.Test; | ||
import test_locally.unit.GsonFactory; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class FileTest { | ||
|
||
String ISSUE_1426_JSON = "{\n" + | ||
" \"id\": \"F08AF7HUWQL\",\n" + | ||
" \"created\": 1737949586,\n" + | ||
" \"timestamp\": 1737949586,\n" + | ||
" \"name\": \"sample.txt\",\n" + | ||
" \"title\": \"sample.txt\",\n" + | ||
" \"mimetype\": \"text/plain\",\n" + | ||
" \"filetype\": \"text\",\n" + | ||
" \"pretty_type\": \"Plain Text\",\n" + | ||
" \"user\": \"U8P5K48E6\",\n" + | ||
" \"user_team\": \"T03E94MJU\",\n" + | ||
" \"editable\": true,\n" + | ||
" \"size\": 57,\n" + | ||
" \"mode\": \"snippet\",\n" + | ||
" \"is_external\": false,\n" + | ||
" \"external_type\": \"\",\n" + | ||
" \"is_public\": true,\n" + | ||
" \"public_url_shared\": false,\n" + | ||
" \"display_as_bot\": false,\n" + | ||
" \"username\": \"\",\n" + | ||
" \"url_private\": \"https://files.slack.com/files-pri/T03E94MJU-F08AF7HUWQL/sample.txt\",\n" + | ||
" \"url_private_download\": \"https://files.slack.com/files-pri/T03E94MJU-F08AF7HUWQL/download/sample.txt\",\n" + | ||
" \"permalink\": \"https://seratch.slack.com/files/U8P5K48E6/F08AF7HUWQL/sample.txt\",\n" + | ||
" \"permalink_public\": \"https://slack-files.com/T03E94MJU-F08AF7HUWQL-9d27bd3319\",\n" + | ||
" \"edit_link\": \"https://seratch.slack.com/files/U8P5K48E6/F08AF7HUWQL/sample.txt/edit\",\n" + | ||
" \"preview\": \"Hello, World!!!!!\\n\\nThis is a sample text file.\\n\\n日本語\",\n" + | ||
" \"preview_highlight\": \"\\u003cdiv class\\u003d\\\"CodeMirror cm-s-default CodeMirrorServer\\\"\\u003e\\n\\u003cdiv class\\u003d\\\"CodeMirror-code\\\"\\u003e\\n\\u003cdiv\\u003e\\u003cpre\\u003eHello, World!!!!!\\u003c/pre\\u003e\\u003c/div\\u003e\\n\\u003cdiv\\u003e\\u003cpre\\u003e\\u003c/pre\\u003e\\u003c/div\\u003e\\n\\u003cdiv\\u003e\\u003cpre\\u003eThis is a sample text file.\\u003c/pre\\u003e\\u003c/div\\u003e\\n\\u003cdiv\\u003e\\u003cpre\\u003e\\u003c/pre\\u003e\\u003c/div\\u003e\\n\\u003cdiv\\u003e\\u003cpre\\u003e日本語\\u003c/pre\\u003e\\u003c/div\\u003e\\n\\u003c/div\\u003e\\n\\u003c/div\\u003e\\n\",\n" + | ||
" \"lines\": 5,\n" + | ||
" \"lines_more\": 0,\n" + | ||
" \"preview_is_truncated\": false,\n" + | ||
" \"favorites\": {},\n" + // unusual data structure here | ||
" \"is_starred\": false,\n" + | ||
" \"shares\": {\n" + | ||
" \"public\": {\n" + | ||
" \"C03E94MKU\": [\n" + | ||
" {\n" + | ||
" \"reply_users\": {},\n" + // unusual data structure here | ||
" \"reply_users_count\": 0,\n" + | ||
" \"reply_count\": 0,\n" + | ||
" \"ts\": \"1737949587.842889\",\n" + | ||
" \"channel_name\": \"random\",\n" + | ||
" \"team_id\": \"T03E94MJU\",\n" + | ||
" \"share_user_id\": \"U8P5K48E6\",\n" + | ||
" \"source\": \"UNKNOWN\"\n" + | ||
" }\n" + | ||
" ],\n" + | ||
" \"C03E94MKS\": [\n" + | ||
" {\n" + | ||
" \"reply_users\": [],\n" + | ||
" \"reply_users_count\": 0,\n" + | ||
" \"reply_count\": 0,\n" + | ||
" \"ts\": \"1737949587.678069\",\n" + | ||
" \"channel_name\": \"general\",\n" + | ||
" \"team_id\": \"T03E94MJU\",\n" + | ||
" \"share_user_id\": \"U8P5K48E6\",\n" + | ||
" \"source\": \"UNKNOWN\"\n" + | ||
" }\n" + | ||
" ]\n" + | ||
" }\n" + | ||
" },\n" + | ||
" \"channels\": {},\n" + // unusual data structure here | ||
" \"groups\": {},\n" + // unusual data structure here | ||
" \"ims\": {},\n" + // unusual data structure here | ||
" \"has_more_shares\": false,\n" + | ||
" \"has_rich_preview\": false,\n" + | ||
" \"file_access\": \"visible\",\n" + | ||
" \"comments_count\": 0\n" + | ||
"}\n"; | ||
|
||
@Test | ||
public void issue1426_parse() { | ||
// com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 65 column 14 path $.groups | ||
File file = GsonFactory.createSnakeCase().fromJson(ISSUE_1426_JSON, File.class); | ||
assertThat(file.getShares(), is(notNullValue())); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand this comment, do we still need it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned below, if you run
context.deserialize(jsonObject, File.class);
within the deserialize method, it results in StackOverflowError because the same method is associated with the File.class. To let gson mechansim to run a different auto-binding process without stack overflow, I've added this sub class. This is internal hidden class, so you can rename it whenever you want to do so. Also, if you find a better solution to achive the same, please feel free to change this part. It won't be a breaking change because this class and the the code using it are hidden from users.