Skip to content
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

Fix $_getMap return value when mutability when message is read-only #754

Merged
merged 1 commit into from
Sep 16, 2022
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
1 change: 0 additions & 1 deletion protobuf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
getter `length` removed. ([#721])
* Update library documentation to hide internals, add documentation for public
types. ([#681])
* Avoid copying when reading map fields of read-only messages. ([#741])
* Fix `PbMap._isReadonly` field initialization in `PbMap.unmodifiable`.
([#741])
* Fix decoding map fields when key or value (or both) fields of a map entry is
Expand Down
3 changes: 2 additions & 1 deletion protobuf/lib/src/protobuf/field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ class _FieldSet {
assert(fi.isMapField);

if (_isReadOnly) {
return PbMap<K, V>(fi.keyFieldType, fi.valueFieldType);
return PbMap<K, V>.unmodifiable(
PbMap<K, V>(fi.keyFieldType, fi.valueFieldType));
}

var map = fi._createMapField(_message!);
Expand Down
7 changes: 7 additions & 0 deletions protoc_plugin/test/map_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,11 @@ void main() {
expect(message, TestMap()..int32ToEnumField[0] = TestMap_EnumValue.BAR);
}
});

test('Read-only message uninitialized map field value is read-only', () {
final msg = TestMap()..freeze();
expect(() {
msg.int32ToInt32Field[0] = 1;
}, throwsA(const TypeMatcher<UnsupportedError>()));
});
}