From 1d08f95fec8de6caba958d2c199728e181ae5b06 Mon Sep 17 00:00:00 2001 From: Artem Sheremet Date: Tue, 9 Oct 2018 10:12:07 +0200 Subject: [PATCH 1/2] Bump upper bound of 'mockito' dependency --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 98f1cd5348..3c51d7311b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,4 +17,4 @@ dev_dependencies: args: ">=0.13.0 <2.0.0" test: ">=0.12.0 <2.0.0" yaml: ">=2.1.0 <3.0.0" - mockito: ">=2.0.0 <3.0.0" + mockito: ">=2.0.0 <4.0.0" From 4d2123b9115d7000211d439667f1c806dbcd28c2 Mon Sep 17 00:00:00 2001 From: Artem Sheremet Date: Mon, 8 Oct 2018 21:28:21 +0200 Subject: [PATCH 2/2] mergeAttributes: deepcopy RHS Map if LHS isn't Map --- lib/src/utils.dart | 9 +++++++-- test/utils_test.dart | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/src/utils.dart b/lib/src/utils.dart index bcbccf167c..0e5e93e181 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -12,8 +12,13 @@ void mergeAttributes(Map attributes, {@required Map into}) { assert(attributes != null && into != null); attributes.forEach((String name, dynamic value) { - final dynamic targetValue = into[name]; - if (value is Map && targetValue is Map) { + dynamic targetValue = into[name]; + if (value is Map) { + if (targetValue is! Map) { + // Let mergeAttributes make a deep copy, because assigning a reference + // of 'value' will expose 'value' to be mutated by further merges. + into[name] = targetValue = {}; + } mergeAttributes(value, into: targetValue); } else { into[name] = value; diff --git a/test/utils_test.dart b/test/utils_test.dart index 3ee15130dc..e86a57a2aa 100644 --- a/test/utils_test.dart +++ b/test/utils_test.dart @@ -35,6 +35,25 @@ void main() { }, }); }); + + test('does not allow overriding original maps', () { + final environment = { + 'extra': { + 'device': 'Pixel 2', + }, + }; + + final event = { + 'extra': { + 'widget': 'Scaffold', + }, + }; + + final target = {}; + mergeAttributes(environment, into: target); + mergeAttributes(event, into: target); + expect(environment['extra'], {'device': 'Pixel 2'}); + }); }); group('formatDateAsIso8601WithSecondPrecision', () {