forked from slackapi/java-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix slackapi#1426 IllegalStateException when deserializing message us…
…ing conversation.history
- Loading branch information
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
67 changes: 67 additions & 0 deletions
67
slack-api-model/src/main/java/com/slack/api/util/json/GsonFileFactory.java
This file contains 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,67 @@ | ||
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 | ||
// See https://github.com/slackapi/java-slack-sdk/issues/1426 for more details | ||
if (jsonObject.has("groups") && !jsonObject.get("groups").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 | ||
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); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
slack-api-model/src/test/java/test_locally/api/model/FileTest.java
This file contains 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,91 @@ | ||
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" + | ||
" \"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" + | ||
" \"C03E94MKU\",\n" + | ||
" \"C03E94MKS\"\n" + | ||
" ],\n" + | ||
" \"groups\": {},\n" + // unusual data structure here | ||
" \"ims\": [],\n" + | ||
" \"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 message = GsonFactory.createSnakeCase().fromJson(ISSUE_1426_JSON, File.class); | ||
assertThat(message.getShares(), is(notNullValue())); | ||
} | ||
} |
This file contains 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