-
-
Notifications
You must be signed in to change notification settings - Fork 257
Normalize data
properties of SentryUser
and Breadcrumb
before sending over method channel
#1591
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
10 commits
Select commit
Hold shift + click to select a range
d758c20
Normalize `SentryUser.data` and `Breadcrumb.data` before sending over…
denrase d639fae
Merge branch 'main' into fix/logging-breadcrumb-object-tostring
denrase df3606f
add changelog entry
denrase 73d5675
call to string in else path
denrase 9709c72
final instead of var
denrase a905fd1
Normalize setContexts & setExtra
denrase 24c0b88
Merge branch 'main' into fix/logging-breadcrumb-object-tostring
denrase 49a3147
Merge branch 'main' into fix/logging-breadcrumb-object-tostring
denrase 9eeecbf
Merge branch 'main' into fix/logging-breadcrumb-object-tostring
denrase 3198df7
Merge branch 'main' into fix/logging-breadcrumb-object-tostring
denrase 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
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
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,38 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
/// Makes sure no invalid data is sent over method channels. | ||
@internal | ||
class MethodChannelHelper { | ||
static dynamic normalize(dynamic data) { | ||
if (data == null) { | ||
return null; | ||
} | ||
if (_isPrimitive(data)) { | ||
return data; | ||
} else if (data is List<dynamic>) { | ||
return _normalizeList(data); | ||
} else if (data is Map<String, dynamic>) { | ||
return normalizeMap(data); | ||
} else { | ||
return data.toString(); | ||
} | ||
} | ||
|
||
static Map<String, dynamic>? normalizeMap(Map<String, dynamic>? data) { | ||
if (data == null) { | ||
return null; | ||
} | ||
return data.map((key, value) => MapEntry(key, normalize(value))); | ||
} | ||
|
||
static List<dynamic>? _normalizeList(List<dynamic>? data) { | ||
if (data == null) { | ||
return null; | ||
} | ||
return data.map((e) => normalize(e)).toList(); | ||
} | ||
|
||
static bool _isPrimitive(dynamic value) { | ||
return value == null || value is String || value is num || value is bool; | ||
} | ||
} |
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
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
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,159 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:sentry_flutter/src/method_channel_helper.dart'; | ||
import 'package:collection/collection.dart'; | ||
|
||
void main() { | ||
group('normalize', () { | ||
test('primitives', () { | ||
var expected = <String, dynamic>{ | ||
'null': null, | ||
'int': 1, | ||
'float': 1.1, | ||
'bool': true, | ||
'string': 'Foo', | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(expected); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
|
||
expect(MethodChannelHelper.normalize(null), null); | ||
expect(MethodChannelHelper.normalize(1), 1); | ||
expect(MethodChannelHelper.normalize(1.1), 1.1); | ||
expect(MethodChannelHelper.normalize(true), true); | ||
expect(MethodChannelHelper.normalize('Foo'), 'Foo'); | ||
}); | ||
|
||
test('object', () { | ||
expect(MethodChannelHelper.normalize(_CustomObject()), 'CustomObject()'); | ||
}); | ||
|
||
test('object in list', () { | ||
var input = <String, dynamic>{ | ||
'object': [_CustomObject()] | ||
}; | ||
var expected = <String, dynamic>{ | ||
'object': ['CustomObject()'] | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalize(input); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
|
||
test('object in map', () { | ||
var input = <String, dynamic>{ | ||
'object': <String, dynamic>{'object': _CustomObject()} | ||
}; | ||
var expected = <String, dynamic>{ | ||
'object': <String, dynamic>{'object': 'CustomObject()'} | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalize(input); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
}); | ||
|
||
group('normalizeMap', () { | ||
test('primitives', () { | ||
var expected = <String, dynamic>{ | ||
'null': null, | ||
'int': 1, | ||
'float': 1.1, | ||
'bool': true, | ||
'string': 'Foo', | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(expected); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
|
||
test('list with primitives', () { | ||
var expected = <String, dynamic>{ | ||
'list': [null, 1, 1.1, true, 'Foo'], | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(expected); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
|
||
test('map with primitives', () { | ||
var expected = <String, dynamic>{ | ||
'map': <String, dynamic>{ | ||
'null': null, | ||
'int': 1, | ||
'float': 1.1, | ||
'bool': true, | ||
'string': 'Foo', | ||
}, | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(expected); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
|
||
test('object', () { | ||
var input = <String, dynamic>{'object': _CustomObject()}; | ||
var expected = <String, dynamic>{'object': 'CustomObject()'}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(input); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
|
||
test('object in list', () { | ||
var input = <String, dynamic>{ | ||
'object': [_CustomObject()] | ||
}; | ||
var expected = <String, dynamic>{ | ||
'object': ['CustomObject()'] | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(input); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
|
||
test('object in map', () { | ||
var input = <String, dynamic>{ | ||
'object': <String, dynamic>{'object': _CustomObject()} | ||
}; | ||
var expected = <String, dynamic>{ | ||
'object': <String, dynamic>{'object': 'CustomObject()'} | ||
}; | ||
|
||
var actual = MethodChannelHelper.normalizeMap(input); | ||
expect( | ||
DeepCollectionEquality().equals(actual, expected), | ||
true, | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
class _CustomObject { | ||
@override | ||
String toString() { | ||
return 'CustomObject()'; | ||
} | ||
} |
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
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.