Fix TypeRegistry passing when unpacking nested Any messages from JSON (#568)
diff --git a/protobuf/lib/src/protobuf/mixins/well_known.dart b/protobuf/lib/src/protobuf/mixins/well_known.dart
index e7ac9fe..f644284 100644
--- a/protobuf/lib/src/protobuf/mixins/well_known.dart
+++ b/protobuf/lib/src/protobuf/mixins/well_known.dart
@@ -85,7 +85,7 @@
'The type of the Any message (${any.typeUrl}) is not in the given typeRegistry.');
}
var unpacked = info.createEmptyInstance!()..mergeFromBuffer(any.value);
- var proto3Json = unpacked.toProto3Json();
+ var proto3Json = unpacked.toProto3Json(typeRegistry: typeRegistry);
if (info.toProto3Json == null) {
var map = proto3Json as Map<String, dynamic>;
map['@type'] = any.typeUrl;
diff --git a/protoc_plugin/Makefile b/protoc_plugin/Makefile
index a7556e3..aef823f 100644
--- a/protoc_plugin/Makefile
+++ b/protoc_plugin/Makefile
@@ -21,6 +21,7 @@
google/protobuf/timestamp \
google/protobuf/type \
google/protobuf/unittest_import \
+ google/protobuf/unittest_nested_any \
google/protobuf/unittest_optimize_for \
google/protobuf/unittest_well_known_types \
google/protobuf/unittest \
diff --git a/protoc_plugin/test/proto3_json_test.dart b/protoc_plugin/test/proto3_json_test.dart
index 02fb71a..791ae39 100644
--- a/protoc_plugin/test/proto3_json_test.dart
+++ b/protoc_plugin/test/proto3_json_test.dart
@@ -17,6 +17,7 @@
import '../out/protos/google/protobuf/struct.pb.dart';
import '../out/protos/google/protobuf/timestamp.pb.dart';
import '../out/protos/google/protobuf/unittest.pb.dart';
+import '../out/protos/google/protobuf/unittest_nested_any.pb.dart';
import '../out/protos/google/protobuf/unittest_well_known_types.pb.dart';
import '../out/protos/google/protobuf/wrappers.pb.dart';
import '../out/protos/map_field.pb.dart';
@@ -270,6 +271,26 @@
throwsA(TypeMatcher<ArgumentError>()));
});
+ test('Nested Any', () {
+ final packedOne = Any.pack(AnyMessage1()..value = '1');
+ final packedTwo = Any.pack(AnyMessage2()
+ ..value = '2'
+ ..anyField2 = packedOne);
+ expect(
+ packedTwo.toProto3Json(
+ typeRegistry: TypeRegistry([AnyMessage1(), AnyMessage2()])),
+ {
+ 'anyField2': {
+ 'value': '1',
+ '@type':
+ 'type.googleapis.com/protobuf_unittest_nested_any.AnyMessage1'
+ },
+ 'value': '2',
+ '@type':
+ 'type.googleapis.com/protobuf_unittest_nested_any.AnyMessage2'
+ });
+ });
+
test('struct', () {
final s = Struct()
..fields['null'] = (Value()..nullValue = NullValue.NULL_VALUE)
@@ -852,6 +873,7 @@
expect(t, TestAllTypes());
expect(t.unknownFields.isEmpty, isTrue);
});
+
test('Any', () {
final m1 = Any()
..mergeFromProto3Json({
@@ -929,6 +951,28 @@
parseFailure([]));
});
+ test('Nested Any', () {
+ final m1 = Any()
+ ..mergeFromProto3Json({
+ 'anyField2': {
+ 'value': '1',
+ '@type':
+ 'type.googleapis.com/protobuf_unittest_nested_any.AnyMessage1'
+ },
+ 'value': '2',
+ '@type':
+ 'type.googleapis.com/protobuf_unittest_nested_any.AnyMessage2'
+ }, typeRegistry: TypeRegistry([AnyMessage1(), AnyMessage2()]));
+
+ expect(
+ m1
+ .unpackInto(AnyMessage2())
+ .anyField2
+ .unpackInto(AnyMessage1())
+ .value,
+ '1');
+ });
+
test('Duration', () {
expect(
Duration()..mergeFromProto3Json('0s'),
diff --git a/protoc_plugin/test/protos/google/protobuf/unittest_nested_any.proto b/protoc_plugin/test/protos/google/protobuf/unittest_nested_any.proto
new file mode 100644
index 0000000..d929b54
--- /dev/null
+++ b/protoc_plugin/test/protos/google/protobuf/unittest_nested_any.proto
@@ -0,0 +1,15 @@
+syntax = "proto3";
+
+package protobuf_unittest_nested_any;
+
+import "google/protobuf/any.proto";
+
+message AnyMessage1 {
+ google.protobuf.Any any_field1 = 1;
+ string value = 2;
+}
+
+message AnyMessage2 {
+ google.protobuf.Any any_field2 = 1;
+ string value = 2;
+}