diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 7c648c1355..dcc73c6c26 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -7,6 +7,7 @@ 🐞 Fixed - Fixed an unhandled `WebSocketChannelException` surfacing when a reconnect attempt failed (e.g. DNS lookup failed in background); the duplicate signal on `sink.done` is now ignored since the stream's `onError` already handles it. +- Fixed failing to parse `null` values in `UpsertPushPreferencesResponse.userPreferences` JSON. ## 9.25.0 diff --git a/packages/stream_chat/lib/src/core/api/responses.dart b/packages/stream_chat/lib/src/core/api/responses.dart index cbd4505678..e1d0537eec 100644 --- a/packages/stream_chat/lib/src/core/api/responses.dart +++ b/packages/stream_chat/lib/src/core/api/responses.dart @@ -834,8 +834,11 @@ class GetUnreadCountResponse extends _BaseResponse { /// Model response for [StreamChatClient.setPushPreferences] api call @JsonSerializable(createToJson: false) class UpsertPushPreferencesResponse extends _BaseResponse { - /// Mapping of user IDs to their push preferences - @JsonKey(defaultValue: {}) + /// Mapping of user IDs to their push preferences. + /// + /// Users whose user-global preferences were not touched by the upsert call + /// (the server returns `null` for them) are omitted from this map. + @JsonKey(fromJson: _userPreferencesFromJson) late Map userPreferences; /// Mapping of user IDs to their channel-specific push preferences @@ -846,3 +849,14 @@ class UpsertPushPreferencesResponse extends _BaseResponse { static UpsertPushPreferencesResponse fromJson(Map json) => _$UpsertPushPreferencesResponseFromJson(json); } + +Map _userPreferencesFromJson( + Map? json, +) { + if (json == null) return {}; + return { + for (final MapEntry(:key, :value) in json.entries) + if (value != null) + key: PushPreference.fromJson(value as Map), + }; +} diff --git a/packages/stream_chat/lib/src/core/api/responses.g.dart b/packages/stream_chat/lib/src/core/api/responses.g.dart index 30dd445960..58b5619e0a 100644 --- a/packages/stream_chat/lib/src/core/api/responses.g.dart +++ b/packages/stream_chat/lib/src/core/api/responses.g.dart @@ -494,12 +494,8 @@ UpsertPushPreferencesResponse _$UpsertPushPreferencesResponseFromJson( Map json) => UpsertPushPreferencesResponse() ..duration = json['duration'] as String? - ..userPreferences = (json['user_preferences'] as Map?) - ?.map( - (k, e) => - MapEntry(k, PushPreference.fromJson(e as Map)), - ) ?? - {} + ..userPreferences = _userPreferencesFromJson( + json['user_preferences'] as Map?) ..userChannelPreferences = (json['user_channel_preferences'] as Map?)?.map( (k, e) => MapEntry( diff --git a/packages/stream_chat/test/src/core/api/responses_test.dart b/packages/stream_chat/test/src/core/api/responses_test.dart index 4f9d45c740..292dd5a127 100644 --- a/packages/stream_chat/test/src/core/api/responses_test.dart +++ b/packages/stream_chat/test/src/core/api/responses_test.dart @@ -4457,7 +4457,8 @@ void main() { }, "user2": { "chat_level": "none" - } + }, + "user3": null }, "user_channel_preferences": { "user1": { @@ -4482,7 +4483,10 @@ void main() { ); expect(response.userPreferences, isA>()); + // user3 had a `null` value in the response (no user-global prefs were + // touched by the upsert) — should be filtered outs. expect(response.userPreferences, hasLength(2)); + expect(response.userPreferences.containsKey('user3'), isFalse); // Test user1 preferences final user1Prefs = response.userPreferences['user1']!;