diff --git a/protobuf/CHANGELOG.md b/protobuf/CHANGELOG.md index 3a41a329..9d5c038a 100644 --- a/protobuf/CHANGELOG.md +++ b/protobuf/CHANGELOG.md @@ -16,10 +16,17 @@ * Fix `PbList` methods `addAll`, `insertAll`, `replaceRange`, `setAll`, `setRange` iterating the `Iterable` argument twice. ([#730], [#1070]) +* Fix `GeneratedMessage.==` throwing a type error when comparing `map` fields in + some cases. ([#1075], [#1077]) + + This bug was introduced with protobuf-5.0.0. + [#1060]: https://github.com/google/protobuf.dart/pull/1060 [#1062]: https://github.com/google/protobuf.dart/pull/1062 [#730]: https://github.com/google/protobuf.dart/issues/730 [#1070]: https://github.com/google/protobuf.dart/pull/1070 +[#1075]: https://github.com/google/protobuf.dart/issues/1075 +[#1077]: https://github.com/google/protobuf.dart/pull/1077 ## 5.0.0 diff --git a/protobuf/lib/src/protobuf/utils.dart b/protobuf/lib/src/protobuf/utils.dart index 7de5c1e7..3a4db970 100644 --- a/protobuf/lib/src/protobuf/utils.dart +++ b/protobuf/lib/src/protobuf/utils.dart @@ -8,7 +8,7 @@ import 'internal.dart'; import 'json_parsing_context.dart'; // TODO(antonm): reconsider later if PbList should take care of equality. -bool deepEquals(Object lhs, Object rhs) { +bool deepEquals(Object? lhs, Object? rhs) { // Some GeneratedMessages implement Map, so test this first. if (lhs is GeneratedMessage) return lhs == rhs; if (rhs is GeneratedMessage) return false; @@ -17,7 +17,7 @@ bool deepEquals(Object lhs, Object rhs) { return lhs == rhs; } -bool areListsEqual(List lhs, List rhs) { +bool areListsEqual(List lhs, List rhs) { if (lhs.length != rhs.length) return false; for (var i = 0; i < lhs.length; i++) { if (!deepEquals(lhs[i], rhs[i])) return false; @@ -25,7 +25,7 @@ bool areListsEqual(List lhs, List rhs) { return true; } -bool areMapsEqual(Map lhs, Map rhs) { +bool areMapsEqual(Map lhs, Map rhs) { if (lhs.length != rhs.length) return false; return lhs.keys.every((key) => deepEquals(lhs[key], rhs[key])); } diff --git a/protoc_plugin/test/map_test.dart b/protoc_plugin/test/map_test.dart index 3002324b..e437948a 100644 --- a/protoc_plugin/test/map_test.dart +++ b/protoc_plugin/test/map_test.dart @@ -7,6 +7,7 @@ import 'package:test/test.dart' import 'gen/map_api.pb.dart' as pb; import 'gen/map_api2.pb.dart' as pb2; +import 'gen/map_field.pb.dart'; void main() { test("message doesn't implement Map when turned off", () { @@ -157,4 +158,23 @@ void main() { }); }, throwsArgumentError); }); + + test( + "Map equality check handles missing keys without type errors, bug #1075", + () { + final message1 = TestMap(); + final message2 = TestMap(); + expect(message1, message2); + + message1.int32ToInt32Field[1] = 2; + expect(message1 == message2, false); + + message2.int32ToInt32Field[3] = 4; + expect(message1 == message2, false); + + message1.int32ToInt32Field[3] = 4; + message2.int32ToInt32Field[1] = 2; + expect(message1, message2); + }, + ); }