Skip to content

Commit

Permalink
Handle more properties
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Jan 28, 2025
1 parent c79b3ea commit 5901635
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ public File deserialize(JsonElement json, Type typeOfT, JsonDeserializationConte
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()) {
// 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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class FileTest {
" \"lines\": 5,\n" +
" \"lines_more\": 0,\n" +
" \"preview_is_truncated\": false,\n" +
" \"favorites\": [],\n" +
" \"favorites\": {},\n" + // unusual data structure here
" \"is_starred\": false,\n" +
" \"shares\": {\n" +
" \"public\": {\n" +
Expand Down Expand Up @@ -70,12 +70,9 @@ public class FileTest {
" ]\n" +
" }\n" +
" },\n" +
" \"channels\": [\n" +
" \"C03E94MKU\",\n" +
" \"C03E94MKS\"\n" +
" ],\n" +
" \"channels\": {},\n" + // unusual data structure here
" \"groups\": {},\n" + // unusual data structure here
" \"ims\": [],\n" +
" \"ims\": {},\n" + // unusual data structure here
" \"has_more_shares\": false,\n" +
" \"has_rich_preview\": false,\n" +
" \"file_access\": \"visible\",\n" +
Expand All @@ -85,7 +82,7 @@ public class FileTest {
@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()));
File file = GsonFactory.createSnakeCase().fromJson(ISSUE_1426_JSON, File.class);
assertThat(file.getShares(), is(notNullValue()));
}
}

0 comments on commit 5901635

Please sign in to comment.