From 92a2bd9d7eca67633de5d1ac7b299fc7b032f9ff Mon Sep 17 00:00:00 2001 From: Marcus Hoffmann Date: Thu, 1 Jul 2021 17:36:21 +0200 Subject: [PATCH] null safety test fixes --- lib/fake_matrix_api.dart | 4 +++- lib/src/model/auth/authentication_data.dart | 2 +- lib/src/model/events/room_encrypted_content.dart | 4 ++-- lib/src/utils/try_get_map_extension.dart | 16 ++++++++++++++-- test/matrix_api_test.dart | 6 ++---- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/fake_matrix_api.dart b/lib/fake_matrix_api.dart index f0f2d16d..1ca54f5b 100644 --- a/lib/fake_matrix_api.dart +++ b/lib/fake_matrix_api.dart @@ -1979,7 +1979,9 @@ class FakeMatrixApi extends MockClient { {'user_id': '@testuser:example.com'}, '/client/r0/register?kind=guest': (var req) => {'user_id': '@testuser:example.com'}, - '/client/r0/rooms/1234/upgrade': (var req) => {}, + '/client/r0/rooms/1234/upgrade': (var req) => { + 'replacement_room': '!1234:fakeServer.notExisting', + }, '/client/r0/user/1234/openid/request_token': (var req) => { 'access_token': 'SomeT0kenHere', 'token_type': 'Bearer', diff --git a/lib/src/model/auth/authentication_data.dart b/lib/src/model/auth/authentication_data.dart index 53ea3ecf..3a561798 100644 --- a/lib/src/model/auth/authentication_data.dart +++ b/lib/src/model/auth/authentication_data.dart @@ -23,7 +23,7 @@ class AuthenticationData { String type; - String? /*?*/ /*?*/ session; + String? session; AuthenticationData({required this.type, this.session}); diff --git a/lib/src/model/events/room_encrypted_content.dart b/lib/src/model/events/room_encrypted_content.dart index e124d3ca..911b755e 100644 --- a/lib/src/model/events/room_encrypted_content.dart +++ b/lib/src/model/events/room_encrypted_content.dart @@ -52,10 +52,10 @@ class RoomEncryptedContent { senderKey = json.tryGet('sender_key') ?? '', deviceId = json.tryGet('device_id'), sessionId = json.tryGet('session_id'), - ciphertextMegolm = json.tryGet('ciphertext'), + ciphertextMegolm = json.silentTryGet('ciphertext'), // filter out invalid/incomplete CiphertextInfos ciphertextOlm = json - .tryGet>('ciphertext') + .silentTryGet>('ciphertext') ?.entries .map((e) { try { diff --git a/lib/src/utils/try_get_map_extension.dart b/lib/src/utils/try_get_map_extension.dart index d768a24d..fe6a9d4d 100644 --- a/lib/src/utils/try_get_map_extension.dart +++ b/lib/src/utils/try_get_map_extension.dart @@ -28,7 +28,18 @@ extension TryGetMapExtension on Map { final value = this[key]; if (value != null && !(value is T)) { Logs().w( - 'Expected "${T.runtimeType}" in event content for the Key "$key" but got "${value.runtimeType}".'); + 'Expected "${T.runtimeType}" in event content for the Key "$key" but got "${value.runtimeType}".', + StackTrace.current); + return null; + } + return value; + } + + /// Same as tryGet but without logging any warnings. + /// This is helpful if you have a field that can mean multiple things on purpose. + T? silentTryGet(String key) { + final value = this[key]; + if (value != null && !(value is T)) { return null; } return value; @@ -38,7 +49,8 @@ extension TryGetMapExtension on Map { final value = this[key]; if (value != null && !(value is List)) { Logs().w( - 'Expected "List<${T.runtimeType}>" in event content for the key "$key" but got "${value.runtimeType}".'); + 'Expected "List<${T.runtimeType}>" in event content for the key "$key" but got "${value.runtimeType}".', + StackTrace.current); return null; } try { diff --git a/test/matrix_api_test.dart b/test/matrix_api_test.dart index 54a8f0a8..064cef97 100644 --- a/test/matrix_api_test.dart +++ b/test/matrix_api_test.dart @@ -1072,8 +1072,7 @@ void main() { matrixApi.homeserver = Uri.parse('https://fakeserver.notexisting'); matrixApi.accessToken = '1234'; - await matrixApi.deleteDevice('QBUAZIFURK', - auth: AuthenticationData.fromJson({})); + await matrixApi.deleteDevice('QBUAZIFURK'); matrixApi.homeserver = matrixApi.accessToken = null; }); @@ -1081,8 +1080,7 @@ void main() { matrixApi.homeserver = Uri.parse('https://fakeserver.notexisting'); matrixApi.accessToken = '1234'; - await matrixApi - .deleteDevices(['QBUAZIFURK'], auth: AuthenticationData.fromJson({})); + await matrixApi.deleteDevices(['QBUAZIFURK']); matrixApi.homeserver = matrixApi.accessToken = null; });