From ccb914a438ce10d488ccf69f1a88b0adea319359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20A=C4=9Facan?= Date: Thu, 6 Nov 2025 12:11:16 +0000 Subject: [PATCH 1/3] Fix message comparison throwing type error when comparing maps in some cases --- protobuf/CHANGELOG.md | 6 ++++++ protobuf/lib/src/protobuf/utils.dart | 2 +- protoc_plugin/test/map_test.dart | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/protobuf/CHANGELOG.md b/protobuf/CHANGELOG.md index 3a41a329..06e86d3f 100644 --- a/protobuf/CHANGELOG.md +++ b/protobuf/CHANGELOG.md @@ -16,10 +16,16 @@ * 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]) + + 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/pull/1075 ## 5.0.0 diff --git a/protobuf/lib/src/protobuf/utils.dart b/protobuf/lib/src/protobuf/utils.dart index 7de5c1e7..e6e178bd 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; 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); + }, + ); } From 66cbd5d17c191219b1e3c45a8b3fb4d9a4218d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20A=C4=9Facan?= Date: Thu, 6 Nov 2025 12:12:24 +0000 Subject: [PATCH 2/3] Update changelog --- protobuf/CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/protobuf/CHANGELOG.md b/protobuf/CHANGELOG.md index 06e86d3f..9d5c038a 100644 --- a/protobuf/CHANGELOG.md +++ b/protobuf/CHANGELOG.md @@ -17,7 +17,7 @@ `setRange` iterating the `Iterable` argument twice. ([#730], [#1070]) * Fix `GeneratedMessage.==` throwing a type error when comparing `map` fields in - some cases. ([#1075]) + some cases. ([#1075], [#1077]) This bug was introduced with protobuf-5.0.0. @@ -25,7 +25,8 @@ [#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/pull/1075 +[#1075]: https://github.com/google/protobuf.dart/issues/1075 +[#1077]: https://github.com/google/protobuf.dart/pull/1077 ## 5.0.0 From cad23f7f8ee0190f0c704fda2088f97bd2a2fe9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20A=C4=9Facan?= Date: Thu, 6 Nov 2025 13:06:18 +0000 Subject: [PATCH 3/3] Make type args Object? --- protobuf/lib/src/protobuf/utils.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protobuf/lib/src/protobuf/utils.dart b/protobuf/lib/src/protobuf/utils.dart index e6e178bd..3a4db970 100644 --- a/protobuf/lib/src/protobuf/utils.dart +++ b/protobuf/lib/src/protobuf/utils.dart @@ -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])); }