Skip to content

Commit 4814163

Browse files
shaljamhifaizsk
andauthored
fix: don't reset webhook events when updating other fields (#188)
Co-authored-by: Faiz Shaikh <[email protected]>
1 parent 2cfe3f6 commit 4814163

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

src/main/java/io/getstream/chat/java/models/App.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,7 @@ public static class AppUpdateRequestData {
798798

799799
@NotNull
800800
@JsonProperty("reminders_interval")
801+
@JsonInclude(Include.NON_DEFAULT)
801802
private int remindersInterval;
802803

803804
@Nullable
@@ -887,17 +888,21 @@ public static class AppUpdateRequestData {
887888

888889
@Nullable
889890
@JsonProperty("webhook_events")
891+
@JsonInclude(Include.NON_NULL)
890892
private List<String> webhookEvents;
891893

892894
@Nullable
893895
@JsonProperty("multi_tenant_enabled")
894896
@JsonInclude(Include.NON_NULL)
895897
private Boolean multiTenantEnabled;
896898

899+
static final Date defaultDate = new Date(Long.MIN_VALUE);
900+
897901
@Nullable
898902
@JsonProperty("revoke_tokens_issued_before")
899-
// This field can be sent as null
900-
private Date revokeTokensIssuedBefore;
903+
@JsonInclude(value = Include.CUSTOM, valueFilter = NonDefaultDateFilter.class)
904+
@Builder.Default
905+
private Date revokeTokensIssuedBefore = defaultDate;
901906

902907
@Nullable
903908
@JsonProperty("channel_hide_members_only")
@@ -911,6 +916,7 @@ public static class AppUpdateRequestData {
911916

912917
@Nullable
913918
@JsonProperty("grants")
919+
@JsonInclude(Include.NON_NULL)
914920
private Map<String, List<String>> grants;
915921

916922
@Nullable
@@ -924,6 +930,17 @@ protected Call<StreamResponseObject> generateCall(Client client) {
924930
return client.create(AppService.class).update(this.internalBuild());
925931
}
926932
}
933+
934+
// Filter that excludes default date and included null (used to reset)
935+
static class NonDefaultDateFilter {
936+
// Return true if filtering out (excluding), false to include
937+
@Override
938+
public boolean equals(Object o) {
939+
// only ever called with String value
940+
Date other = (Date) o;
941+
return defaultDate.equals(other);
942+
}
943+
}
927944
}
928945

929946
public static class AppGetRateLimitsRequest extends StreamRequest<AppGetRateLimitsResponse> {

src/test/java/io/getstream/chat/java/AppTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.getstream.chat.java;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import io.getstream.chat.java.exceptions.StreamException;
45
import io.getstream.chat.java.models.App;
56
import io.getstream.chat.java.models.App.AppCheckSnsResponse;
@@ -11,11 +12,13 @@
1112
import io.getstream.chat.java.models.Message;
1213
import io.getstream.chat.java.models.Message.MessageRequestObject;
1314
import io.getstream.chat.java.services.framework.DefaultClient;
15+
import java.util.ArrayList;
1416
import java.util.Arrays;
1517
import java.util.Calendar;
1618
import java.util.Collections;
1719
import java.util.Date;
1820
import java.util.GregorianCalendar;
21+
import java.util.HashMap;
1922
import java.util.Properties;
2023
import java.util.Random;
2124
import org.junit.jupiter.api.Assertions;
@@ -250,4 +253,56 @@ void whenUpdatingAppSettingsWithSNSEventHook_thenNoException() throws StreamExce
250253
throw e;
251254
}
252255
}
256+
257+
@DisplayName("App Settings update webhook events")
258+
@Test
259+
void whenUpdatingAppSettings_thenDoesntAlwaysChangeWebhookEvents() {
260+
var messageNewList = Arrays.asList("message.new");
261+
Assertions.assertDoesNotThrow(() -> App.update().webhookEvents(messageNewList).request());
262+
263+
var appConfig = Assertions.assertDoesNotThrow(() -> App.get().request()).getApp();
264+
Assertions.assertEquals(messageNewList, appConfig.getWebhookEvents());
265+
266+
// Updating another field should not change (reset) webhook events
267+
Assertions.assertDoesNotThrow(() -> App.update().remindersInterval(60).request());
268+
269+
appConfig = Assertions.assertDoesNotThrow(() -> App.get().request()).getApp();
270+
Assertions.assertEquals(messageNewList, appConfig.getWebhookEvents());
271+
272+
// Reset webhook events to defaults using an empty list
273+
Assertions.assertDoesNotThrow(() -> App.update().webhookEvents(new ArrayList<>()).request());
274+
appConfig = Assertions.assertDoesNotThrow(() -> App.get().request()).getApp();
275+
Assertions.assertTrue(appConfig.getWebhookEvents().size() > 1);
276+
}
277+
278+
@DisplayName("AppConfig encoding should not include null fields")
279+
@Test
280+
void whenEncodingAppConfig_thenNoNullFields() {
281+
var appConfig = App.update().internalBuild();
282+
final ObjectMapper mapper = new ObjectMapper();
283+
284+
String json = Assertions.assertDoesNotThrow(() -> mapper.writeValueAsString(appConfig));
285+
286+
// When we didn't set any fields, the JSON should be empty
287+
Assertions.assertEquals("{}", json);
288+
289+
// We set some fields where we mean to reset them
290+
var appConfigNull =
291+
App.update()
292+
.webhookEvents(new ArrayList<>())
293+
.revokeTokensIssuedBefore(null)
294+
.grants(new HashMap<>())
295+
.internalBuild();
296+
297+
json = Assertions.assertDoesNotThrow(() -> mapper.writeValueAsString(appConfigNull));
298+
299+
Assertions.assertTrue(
300+
json.contains("\"webhook_events\":[]"), "JSON should contain null webhook_events field");
301+
302+
Assertions.assertTrue(
303+
json.contains("\"revoke_tokens_issued_before\":null"),
304+
"JSON should contain null revoke_tokens_issued_before field");
305+
306+
Assertions.assertTrue(json.contains("\"grants\":{}"), "JSON should contain null grants field");
307+
}
253308
}

0 commit comments

Comments
 (0)