Skip to content

Commit

Permalink
Fix $_getMap return value when mutability when message is read-only (#…
Browse files Browse the repository at this point in the history
…754)

This was broken in #741
  • Loading branch information
osa1 authored Sep 16, 2022
1 parent d331393 commit 1d175be
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
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 @@ -408,7 +408,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>()));
});
}

0 comments on commit 1d175be

Please sign in to comment.