From 590163588bce5cd58725d0ba4f2fcc9571e5900d Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Tue, 28 Jan 2025 10:25:46 +0900 Subject: [PATCH] Handle more properties --- .../com/slack/api/util/json/GsonFileFactory.java | 13 +++++++++++-- .../test/java/test_locally/api/model/FileTest.java | 13 +++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/slack-api-model/src/main/java/com/slack/api/util/json/GsonFileFactory.java b/slack-api-model/src/main/java/com/slack/api/util/json/GsonFileFactory.java index 732fe167c..75f244c06 100644 --- a/slack-api-model/src/main/java/com/slack/api/util/json/GsonFileFactory.java +++ b/slack-api-model/src/main/java/com/slack/api/util/json/GsonFileFactory.java @@ -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")) { diff --git a/slack-api-model/src/test/java/test_locally/api/model/FileTest.java b/slack-api-model/src/test/java/test_locally/api/model/FileTest.java index 7267409b8..dad4fca85 100644 --- a/slack-api-model/src/test/java/test_locally/api/model/FileTest.java +++ b/slack-api-model/src/test/java/test_locally/api/model/FileTest.java @@ -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" + @@ -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" + @@ -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())); } }