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

Handle unknown fields part of a oneof #324

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions protobuf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 1.0.2

* Fixed generic handling of unknown fields.
* `GeneratedMessage.clearField(tagNumber)` will now also clear an unknown
field.
* `GeneratedMessage.hasField(tagNumber)` will now return true if there is an
unknown field.
* Handle cases where a field that is part of a oneof is removed by the
protobuf aware treeshaker.

Expand Down
16 changes: 15 additions & 1 deletion protobuf/lib/src/protobuf/extension_field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class _ExtensionFieldSet {
// I think this was originally here for repeated extensions.
_addInfoUnchecked(fi);
var value = _getFieldOrNull(fi);
if (value == null) return fi.makeDefault();
if (value == null) {
sigurdm marked this conversation as resolved.
Show resolved Hide resolved
_checkNotInUnknown(fi);
return fi.makeDefault();
}
return value;
}

Expand Down Expand Up @@ -50,6 +53,7 @@ class _ExtensionFieldSet {
List<T> _getList<T>(Extension<T> fi) {
var value = _values[fi.tagNumber];
if (value != null) return value as List<T>;
_checkNotInUnknown(fi);
if (_isReadOnly) return List<T>.unmodifiable(const []);
return _addInfoAndCreateList(fi);
}
Expand Down Expand Up @@ -184,4 +188,14 @@ class _ExtensionFieldSet {
}
}
}

void _checkNotInUnknown(Extension extension) {
if (_parent._hasUnknownFields &&
_parent._unknownFields.hasField(extension.tagNumber)) {
throw StateError(
'Trying to get $extension that is present as an unknown field. '
'Parse the message with this extension in the extension registry or use '
'`ExtensionRegistry.reparseMessage`.');
}
}
}
6 changes: 5 additions & 1 deletion protobuf/lib/src/protobuf/field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ class _FieldSet {
bool _hasField(int tagNumber) {
var fi = _nonExtensionInfo(tagNumber);
if (fi != null) return _$has(fi.index);
if (_hasUnknownFields && _unknownFields.hasField(tagNumber)) {
return true;
}
if (!_hasExtensions) return false;
return _extensions._hasField(tagNumber);
}
Expand Down Expand Up @@ -346,7 +349,8 @@ class _FieldSet {
void _updateOneOfCase(int newTagnumber) {
sigurdm marked this conversation as resolved.
Show resolved Hide resolved
int oneofIndex = _meta.oneofs[newTagnumber];
if (oneofIndex != null) {
_clearField(_oneofCases[oneofIndex]);
final currentTagnumber = _oneofCases[oneofIndex];
if (currentTagnumber != null) _clearField(currentTagnumber);
_oneofCases[oneofIndex] = newTagnumber;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe only call it conditionally

final currentTag = _oneofCases[oneofIndex];
if (currentTag != null) _clearField(currentTag);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - that's better

}
}
Expand Down
2 changes: 2 additions & 0 deletions protobuf/lib/src/protobuf/generated_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ abstract class GeneratedMessage {
_fieldSet._extensions._getFieldOrNull(extension) != null;

/// Returns [:true:] if this message has a field associated with [tagNumber].
///
/// Will return [:true:] even if that field is an unknown field.
bool hasField(int tagNumber) => _fieldSet._hasField(tagNumber);

/// Merges the contents of the [other] into this message.
Expand Down
8 changes: 8 additions & 0 deletions protoc_plugin/test/oneof_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ void main() {
expect(foo.hasIndex(), true);
expect(foo.index, Bar());
});

test('clone', () {
sigurdm marked this conversation as resolved.
Show resolved Hide resolved
Foo foo = Foo()..first = 'oneof';
expectFirstSet(foo);

final foo2 = Foo()..mergeFromMessage(foo);
expectFirstSet(foo2);
});
}

void expectSecondSet(Foo foo) {
Expand Down
5 changes: 5 additions & 0 deletions protoc_plugin/test/unknown_field_set_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,9 @@ void main() {

_checkNotEqual(f1, f2);
});

test('GeneratedMessage.hasField sees unknown fields', () {
expect(emptyMessage.hasField(testAllTypes.getTagNumber('optionalInt32')),
true);
});
}