Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions protobuf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions protobuf/lib/src/protobuf/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,15 +17,15 @@ bool deepEquals(Object lhs, Object rhs) {
return lhs == rhs;
}

bool areListsEqual(List lhs, List rhs) {
bool areListsEqual(List<Object?> lhs, List<Object?> rhs) {
if (lhs.length != rhs.length) return false;
for (var i = 0; i < lhs.length; i++) {
if (!deepEquals(lhs[i], rhs[i])) return false;
}
return true;
}

bool areMapsEqual(Map lhs, Map rhs) {
bool areMapsEqual(Map<Object?, Object?> lhs, Map<Object?, Object?> rhs) {
if (lhs.length != rhs.length) return false;
return lhs.keys.every((key) => deepEquals(lhs[key], rhs[key]));
}
Expand Down
20 changes: 20 additions & 0 deletions protoc_plugin/test/map_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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", () {
Expand Down Expand Up @@ -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);
},
);
}