Skip to content

Commit

Permalink
Merge changes from internal repo. (#68)
Browse files Browse the repository at this point in the history
Parsing protobuf from JSON now ignores unknown enum values instead of
throwing an exception.

The behavior is still different from parsing a message from a serialized
byte string, because we do not store the (tag, unknown enum value) pair
in the UnknownFieldSet. JSON parsing currently discards unknown field
tags, so it makes sense to also discard the unknown enum values.
  • Loading branch information
jakobr-google authored Apr 20, 2017
1 parent 8836e56 commit 8f06b65
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.5.4

* Unknown enum values are ignored when parsing JSON, instead of throwing an
exception.

## 0.5.3+2

* Resolved a strong-mode error.
Expand Down
21 changes: 18 additions & 3 deletions lib/src/protobuf/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,29 @@ void _appendJsonList(
_FieldSet fs, List json, FieldInfo fi, ExtensionRegistry registry) {
List repeated = fs._ensureRepeatedField(fi);
for (var value in json) {
repeated.add(_convertJsonValue(fs, value, fi.tagNumber, fi.type, registry));
var convertedValue =
_convertJsonValue(fs, value, fi.tagNumber, fi.type, registry);
if (convertedValue != null) {
repeated.add(convertedValue);
}
}
}

void _setJsonField(
_FieldSet fs, json, FieldInfo fi, ExtensionRegistry registry) {
var value = _convertJsonValue(fs, json, fi.tagNumber, fi.type, registry);
fs._validateField(fi, value);
fs._setFieldUnchecked(fi, value);
if (value != null) {
fs._validateField(fi, value);
fs._setFieldUnchecked(fi, value);
}
}

/// Converts [value] from the Json format to the Dart data type
/// suitable for inserting into the corresponding [GeneratedMessage] field.
///
/// Returns the converted value. This function returns [null] if the caller
/// should ignore the field value, because it is an unknown enum value.
/// This function throws [ArgumentError] if it cannot convert the value.
_convertJsonValue(_FieldSet fs, value, int tagNumber, int fieldType,
ExtensionRegistry registry) {
String expectedType; // for exception message
Expand Down Expand Up @@ -149,6 +161,9 @@ _convertJsonValue(_FieldSet fs, value, int tagNumber, int fieldType,
value = int.parse(value);
}
if (value is int) {
// The following call will return null if the enum value is unknown.
// In that case, we want the caller to ignore this value, so we return
// null from this method as well.
return fs._meta._decodeEnum(tagNumber, registry, value);
}
expectedType = 'int or stringified int';
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: protobuf
version: 0.5.3+2
version: 0.5.4
author: Dart Team <[email protected]>
description: Runtime library for protocol buffers support.
homepage: https://github.com/dart-lang/protobuf
Expand Down
2 changes: 1 addition & 1 deletion test/mock_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class MockMessage extends GeneratedMessage {

List<int> get int32s => $_get(3, 4, null);

Int64 get int64 => $_get(4, 5, 0);
Int64 get int64 => $_get(4, 5, new Int64(0));
set int64(x) => setField(5, x);

clone() {
Expand Down

0 comments on commit 8f06b65

Please sign in to comment.