Skip to content

Commit

Permalink
Fix parsing negative JSON values for uint32 fields (#839)
Browse files Browse the repository at this point in the history
Sync of cl/538436650
  • Loading branch information
osa1 committed Jun 9, 2023
1 parent 2931b2e commit edf8e92
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions protobuf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
(`mergeFromBuffer`, `mergeFromProto3Json`, ...). ([#489], [#727])
* Fix handling `null` values in proto3 JSON deserializer. ([#751], [#760],
[#763])
* Fix handling negative JSON values when parsing uint32 fields. ([#839])

[#183]: https://github.com/google/protobuf.dart/issues/183
[#644]: https://github.com/google/protobuf.dart/pull/644
Expand All @@ -43,6 +44,7 @@
[#760]: https://github.com/google/protobuf.dart/issues/760
[#763]: https://github.com/google/protobuf.dart/pull/763
[#754]: https://github.com/google/protobuf.dart/pull/754
[#839]: https://github.com/google/protobuf.dart/pull/839

## 2.1.0

Expand Down
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ dynamic _convertJsonValue(BuilderInfo meta, _FieldSet fs, value, int tagNumber,
break;
case PbFieldType._INT32_BIT:
case PbFieldType._SINT32_BIT:
case PbFieldType._UINT32_BIT:
case PbFieldType._SFIXED32_BIT:
if (value is int) return value;
if (value is String) return int.parse(value);
expectedType = 'int or stringified int';
break;
case PbFieldType._UINT32_BIT:
case PbFieldType._FIXED32_BIT:
int? validatedValue;
if (value is int) validatedValue = value;
Expand Down
7 changes: 7 additions & 0 deletions protoc_plugin/test/json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ void main() {
expect(message.count, 2214672939);
});

test('testUint32Negative', () {
var message = foo.Inner.fromJson('{"6": -1}');
expect(message.countUint32, 4294967295);
message = foo.Inner.fromJson('{"6": -2080294357}');
expect(message.countUint32, 2214672939);
});

test('testParse', () {
expect(TestAllTypes.fromJson(testAllJsonTypes), getAllSet());
});
Expand Down
1 change: 1 addition & 0 deletions protoc_plugin/test/protos/foo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ message Inner {
repeated string strings = 3;
optional Inner inner = 4;
optional fixed32 count = 5;
optional uint32 count_uint32 = 6;
}

extend Outer {
Expand Down

0 comments on commit edf8e92

Please sign in to comment.