Repository navigation
fix(model): type the function input values in app activities #1718
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
7 commits
Select commit
Hold shift + click to select a range
4753585
fix(model): type the function input values in app activities
zimeg 4e8f263
docs: remove the comments that repeat the function input value fields
zimeg f4f2402
fix(model): keep string arrays and other function input values
zimeg 1695a8e
refactor: remove the unreachable branch and the raw value comment
zimeg a022128
fix(model): keep a function input value raw when a credential key isn…
zimeg d592b72
fix(model): keep the raw function input value as a JsonElement
zimeg 8f1c416
fix(model): keep a function input object raw unless it is only creden…
zimeg 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
129 changes: 129 additions & 0 deletions
129
...lient/src/main/java/com/slack/api/util/json/GsonAppActivityFunctionInputValueFactory.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,129 @@ | ||
| package com.slack.api.util.json; | ||
|
|
||
| import com.google.gson.*; | ||
| import com.slack.api.model.admin.AppActivity; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| @Slf4j | ||
| public class GsonAppActivityFunctionInputValueFactory implements JsonDeserializer<AppActivity.FunctionInputValue>, JsonSerializer<AppActivity.FunctionInputValue> { | ||
|
|
||
| private static final String REPORT_THIS = "Please report this issue at https://github.com/slackapi/java-slack-sdk/issues"; | ||
|
|
||
| private static final List<String> CREDENTIAL_PROPERTY_NAMES = Arrays.asList( | ||
| "credential_source", | ||
| "external_token_id", | ||
| "user_context_source" | ||
| ); | ||
|
|
||
| private final boolean failOnUnknownProperties; | ||
|
|
||
| public GsonAppActivityFunctionInputValueFactory() { | ||
| this(false); | ||
| } | ||
|
|
||
| public GsonAppActivityFunctionInputValueFactory(boolean failOnUnknownProperties) { | ||
| this.failOnUnknownProperties = failOnUnknownProperties; | ||
| } | ||
|
|
||
| @Override | ||
| public AppActivity.FunctionInputValue deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
| throws JsonParseException { | ||
| AppActivity.FunctionInputValue result = new AppActivity.FunctionInputValue(); | ||
| if (json.isJsonPrimitive()) { | ||
| JsonPrimitive primitive = json.getAsJsonPrimitive(); | ||
| if (primitive.isString()) { | ||
| result.setStringValue(primitive.getAsString()); | ||
| } else { | ||
| result.setRawValue(json); | ||
| } | ||
| return result; | ||
| } else if (json.isJsonArray()) { | ||
| List<String> values = new ArrayList<>(); | ||
| for (JsonElement elem : json.getAsJsonArray()) { | ||
| if (!elem.isJsonPrimitive() || !elem.getAsJsonPrimitive().isString()) { | ||
| result.setRawValue(json); | ||
| return result; | ||
| } | ||
| values.add(elem.getAsString()); | ||
| } | ||
| result.setStringValues(values); | ||
| return result; | ||
| } else if (json.isJsonObject()) { | ||
| JsonObject obj = json.getAsJsonObject(); | ||
| if (failOnUnknownProperties && obj.has("credential_source")) { | ||
| for (String name : obj.keySet()) { | ||
| if (!CREDENTIAL_PROPERTY_NAMES.contains(name)) { | ||
| String message = "An unknown property (" + name + ") is detected. " + REPORT_THIS; | ||
| throw new JsonParseException(message); | ||
| } | ||
| } | ||
| } | ||
| if (!isCredential(obj)) { | ||
| result.setRawValue(json); | ||
| return result; | ||
| } | ||
| result.setCredentialSource(getString(obj, "credential_source")); | ||
| result.setExternalTokenId(getString(obj, "external_token_id")); | ||
| result.setUserContextSource(getString(obj, "user_context_source")); | ||
| return result; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static boolean isCredential(JsonObject obj) { | ||
| if (obj.keySet().isEmpty()) { | ||
| return false; | ||
| } | ||
| for (String name : obj.keySet()) { | ||
| JsonElement value = obj.get(name); | ||
| if (!CREDENTIAL_PROPERTY_NAMES.contains(name)) { | ||
| return false; | ||
| } | ||
| if (!value.isJsonNull() && !(value.isJsonPrimitive() && value.getAsJsonPrimitive().isString())) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private static String getString(JsonObject obj, String name) { | ||
| JsonElement value = obj.get(name); | ||
| if (value == null || value.isJsonNull()) { | ||
| return null; | ||
| } | ||
| return value.getAsString(); | ||
|
zimeg marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public JsonElement serialize(AppActivity.FunctionInputValue src, Type typeOfSrc, JsonSerializationContext context) { | ||
| if (src.getStringValue() != null) { | ||
| return new JsonPrimitive(src.getStringValue()); | ||
| } else if (src.getStringValues() != null) { | ||
| return context.serialize(src.getStringValues()); | ||
| } else if (src.getRawValue() != null) { | ||
| return src.getRawValue(); | ||
| } else if (src.getCredentialSource() != null | ||
| || src.getExternalTokenId() != null | ||
| || src.getUserContextSource() != null) { | ||
| JsonObject json = new JsonObject(); | ||
| if (src.getCredentialSource() != null) { | ||
| json.addProperty("credential_source", src.getCredentialSource()); | ||
| } | ||
| if (src.getExternalTokenId() != null) { | ||
| json.addProperty("external_token_id", src.getExternalTokenId()); | ||
| } | ||
| if (src.getUserContextSource() != null) { | ||
| json.addProperty("user_context_source", src.getUserContextSource()); | ||
| } | ||
| return json; | ||
| } else { | ||
| log.warn("Unsupported field in AppActivity.FunctionInputValue is detected ({})", src); | ||
| return JsonNull.INSTANCE; | ||
| } | ||
| } | ||
| } | ||
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
169 changes: 169 additions & 0 deletions
169
...rc/test/java/test_locally/api/util/json/GsonAppActivityFunctionInputValueFactoryTest.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,169 @@ | ||
| package test_locally.api.util.json; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.JsonParseException; | ||
| import com.google.gson.JsonPrimitive; | ||
| import com.slack.api.SlackConfig; | ||
| import com.slack.api.model.admin.AppActivity; | ||
| import com.slack.api.util.json.GsonFactory; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.CoreMatchers.nullValue; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
|
|
||
| public class GsonAppActivityFunctionInputValueFactoryTest { | ||
|
|
||
| static Gson lenientGson() { | ||
| return GsonFactory.createSnakeCase(); | ||
| } | ||
|
|
||
| static Gson strictGson() { | ||
| SlackConfig config = new SlackConfig(); | ||
| config.setFailOnUnknownProperties(true); | ||
| return GsonFactory.createSnakeCase(config); | ||
| } | ||
|
|
||
| static AppActivity.FunctionInputValue parse(Gson gson, String value) { | ||
| String json = "{\"value\": " + value + ", \"hidden\": false, \"locked\": false}"; | ||
| return gson.fromJson(json, AppActivity.FunctionInput.class).getValue(); | ||
| } | ||
|
|
||
| String string_value = "\"https://www.example.com/\""; | ||
|
|
||
| String credential_value = "{\n" + | ||
| " \"credential_source\": \"END_USER\",\n" + | ||
| " \"external_token_id\": \"Et12345ABCDE\",\n" + | ||
| " \"user_context_source\": \"\"\n" + | ||
| "}"; | ||
|
|
||
| String unknown_key_value = "{\n" + | ||
| " \"credential_source\": \"DEVELOPER\",\n" + | ||
| " \"unknown\": \"value\"\n" + | ||
| "}"; | ||
|
|
||
| @Test | ||
| public void deserialize_string() { | ||
|
zimeg marked this conversation as resolved.
Resolved
|
||
| AppActivity.FunctionInputValue value = parse(strictGson(), string_value); | ||
| assertThat(value.toString(), is("AppActivity.FunctionInputValue(" + | ||
| "stringValue=https://www.example.com/, " + | ||
| "stringValues=null, " + | ||
| "credentialSource=null, " + | ||
| "externalTokenId=null, " + | ||
| "userContextSource=null, " + | ||
| "rawValue=null)")); | ||
| } | ||
|
|
||
| @Test | ||
| public void deserialize_credential() { | ||
| AppActivity.FunctionInputValue value = parse(strictGson(), credential_value); | ||
| assertThat(value.toString(), is("AppActivity.FunctionInputValue(" + | ||
| "stringValue=null, " + | ||
| "stringValues=null, " + | ||
| "credentialSource=END_USER, " + | ||
| "externalTokenId=Et12345ABCDE, " + | ||
| "userContextSource=, " + | ||
| "rawValue=null)")); | ||
| } | ||
|
|
||
| @Test(expected = JsonParseException.class) | ||
| public void deserialize_unknown_key_strict() { | ||
| parse(strictGson(), unknown_key_value); | ||
| } | ||
|
|
||
| @Test | ||
| public void deserialize_unknown_key_lenient() { | ||
| AppActivity.FunctionInputValue value = parse(lenientGson(), unknown_key_value); | ||
| assertThat(value.getCredentialSource(), is(nullValue())); | ||
| assertThat(value.getRawValue().toString(), is("{\"credential_source\":\"DEVELOPER\",\"unknown\":\"value\"}")); | ||
| } | ||
|
|
||
| @Test | ||
| public void round_trip_partial_credential() { | ||
| AppActivity.FunctionInputValue value = new AppActivity.FunctionInputValue(); | ||
| value.setExternalTokenId("Et12345ABCDE"); | ||
| AppActivity.FunctionInputValue parsed = parse(strictGson(), lenientGson().toJson(value)); | ||
| assertThat(parsed.getExternalTokenId(), is("Et12345ABCDE")); | ||
| assertThat(parsed.getRawValue(), is(nullValue())); | ||
| } | ||
|
|
||
| @Test | ||
| public void deserialize_array() { | ||
|
zimeg marked this conversation as resolved.
Resolved
|
||
| AppActivity.FunctionInputValue value = parse(lenientGson(), "[\"U12345\", \"U67890\"]"); | ||
| assertThat(value.getStringValues(), is(Arrays.asList("U12345", "U67890"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void deserialize_mixed_array() { | ||
| AppActivity.FunctionInputValue value = parse(lenientGson(), "[\"U12345\", 1]"); | ||
| assertThat(value.getStringValues(), is(nullValue())); | ||
| assertThat(value.getRawValue().toString(), is("[\"U12345\",1]")); | ||
| } | ||
|
|
||
| @Test | ||
| public void deserialize_credential_object_value() { | ||
| AppActivity.FunctionInputValue value = parse(lenientGson(), "{\"credential_source\": {\"type\": \"END_USER\"}}"); | ||
| assertThat(value.getCredentialSource(), is(nullValue())); | ||
| assertThat(value.getRawValue().toString(), is("{\"credential_source\":{\"type\":\"END_USER\"}}")); | ||
| } | ||
|
|
||
| @Test | ||
| public void deserialize_boolean() { | ||
| AppActivity.FunctionInputValue value = parse(lenientGson(), "true"); | ||
| assertThat(value.getStringValue(), is(nullValue())); | ||
| assertThat(value.getRawValue().getAsBoolean(), is(true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void deserialize_number() { | ||
| AppActivity.FunctionInputValue value = parse(lenientGson(), "9007199254740993"); | ||
| assertThat(value.getRawValue().getAsLong(), is(9007199254740993L)); | ||
| } | ||
|
|
||
| @Test | ||
| public void deserialize_object() { | ||
| AppActivity.FunctionInputValue value = parse(lenientGson(), "{\"user_id\": \"U12345\"}"); | ||
| assertThat(value.getCredentialSource(), is(nullValue())); | ||
| assertThat(value.getRawValue().toString(), is("{\"user_id\":\"U12345\"}")); | ||
| } | ||
|
|
||
| @Test | ||
| public void serialize_empty() { | ||
| assertThat(lenientGson().toJson(new AppActivity.FunctionInputValue()), is("null")); | ||
| } | ||
|
|
||
| @Test | ||
| public void serialize_string() { | ||
| AppActivity.FunctionInputValue value = new AppActivity.FunctionInputValue(); | ||
| value.setStringValue("https://www.example.com/"); | ||
| assertThat(lenientGson().toJson(value), is("\"https://www.example.com/\"")); | ||
| } | ||
|
|
||
| @Test | ||
| public void serialize_string_values() { | ||
| AppActivity.FunctionInputValue value = new AppActivity.FunctionInputValue(); | ||
| value.setStringValues(Arrays.asList("U12345", "U67890")); | ||
| assertThat(lenientGson().toJson(value), is("[\"U12345\",\"U67890\"]")); | ||
| } | ||
|
|
||
| @Test | ||
| public void serialize_raw_value() { | ||
| AppActivity.FunctionInputValue value = new AppActivity.FunctionInputValue(); | ||
| value.setRawValue(new JsonPrimitive(true)); | ||
| assertThat(lenientGson().toJson(value), is("true")); | ||
| } | ||
|
|
||
| @Test | ||
| public void serialize_credential() { | ||
| AppActivity.FunctionInputValue value = new AppActivity.FunctionInputValue(); | ||
| value.setCredentialSource("END_USER"); | ||
| value.setExternalTokenId("Et12345ABCDE"); | ||
| assertThat(lenientGson().toJson(value), is("{" + | ||
| "\"credential_source\":\"END_USER\"," + | ||
| "\"external_token_id\":\"Et12345ABCDE\"" + | ||
| "}")); | ||
| } | ||
|
|
||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.