public ProtoKnownTypes(IEnumerable <Assembly> assemblies) { var messageTypes = ( from assembly in assemblies.Distinct() from type in assembly.GetTypes() where typeof(IMessage).IsAssignableFrom(type) where type.IsInterface is false select type).Distinct().ToList(); var descriptorAll = from type in messageTypes select type.GetProperty("Descriptor") .GetGetMethod()? .Invoke(null, null) as MessageDescriptor; Registry = TypeRegistry.FromMessages(descriptorAll); JsonParser = new JsonParser(JsonParser.Settings.Default.WithTypeRegistry(Registry)); JsonFormatter = new JsonFormatter(JsonFormatter.Settings.Default.WithTypeRegistry(Registry)); var anyUnpackers = from t in messageTypes select(Key : $"type.googleapis.com/{GetDescriptorFullName(t)}", Value : typeof(Any).GetMethod("Unpack").MakeGenericMethod(t)); var anyUnpackersDic = anyUnpackers.Select(x => (x.Key, Func: fun((Any a) => x.Value?.Invoke(a, null) as IMessage))) .ToDictionary(x => x.Key, x => x.Func); AnyUnpackersDic = anyUnpackersDic; }
public void AnyWellKnownType() { var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(Timestamp.Descriptor))); var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp(); var any = Any.Pack(timestamp); AssertJson("{ '@type': 'type.googleapis.com/google.protobuf.Timestamp', 'value': '1673-06-19T12:34:56Z' }", formatter.Format(any)); }
public async Task HandleCallAsync_Any_Success() { // Arrange HelloRequest?request = null; UnaryServerMethod <HttpApiGreeterService, HelloRequest, HelloReply> invoker = (s, r, c) => { request = r; return(Task.FromResult(new HelloReply { AnyMessage = Any.Pack(new StringValue { Value = "A value!" }) })); }; var typeRegistry = TypeRegistry.FromMessages(StringValue.Descriptor, Int32Value.Descriptor); var jsonFormatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true, typeRegistry)); var unaryServerCallHandler = CreateCallHandler( invoker, descriptorInfo: TestHelpers.CreateDescriptorInfo(bodyDescriptor: HelloRequest.Descriptor), httpApiOptions: new GrpcHttpApiOptions { JsonSettings = new JsonSettings { TypeRegistry = typeRegistry } }); var httpContext = TestHelpers.CreateHttpContext(); var requestJson = jsonFormatter.Format(new HelloRequest { Name = "Test", AnyMessage = Any.Pack(new Int32Value { Value = 123 }) }); httpContext.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(requestJson)); httpContext.Request.ContentType = "application/json"; // Act await unaryServerCallHandler.HandleCallAsync(httpContext); // Assert Assert.NotNull(request); Assert.Equal("Test", request !.Name); Assert.Equal("type.googleapis.com/google.protobuf.Int32Value", request !.AnyMessage.TypeUrl); httpContext.Response.Body.Seek(0, SeekOrigin.Begin); using var responseJson = JsonDocument.Parse(httpContext.Response.Body); var anyMessage = responseJson.RootElement.GetProperty("anyMessage"); Assert.Equal("type.googleapis.com/google.protobuf.StringValue", anyMessage.GetProperty("@type").GetString()); Assert.Equal("A value!", anyMessage.GetProperty("value").GetString()); }
public void AnyMessageType_CustomPrefix() { var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor))); var message = new TestAllTypes { SingleInt32 = 10 }; var any = Any.Pack(message, "foo.bar/baz"); AssertJson("{ '@type': 'foo.bar/baz/protobuf_unittest.TestAllTypes', 'singleInt32': 10 }", formatter.Format(any)); }
public void Any_CustomPrefix() { var registry = TypeRegistry.FromMessages(TestAllTypes.Descriptor); var message = new TestAllTypes { SingleInt32 = 10 }; var original = Any.Pack(message, "custom.prefix/middle-part"); var parser = new JsonParser(new JsonParser.Settings(10, registry)); string json = "{ \"@type\": \"custom.prefix/middle-part/protobuf_unittest.TestAllTypes\", \"singleInt32\": 10 }"; Assert.AreEqual(original, parser.Parse <Any>(json)); }
public void AnyMessageType() { var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor))); var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } }; var any = Any.Pack(message); AssertJson("{ '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 10, 'singleNestedMessage': { 'bb': 20 } }", formatter.Format(any)); }
public void Any_WellKnownType() { var registry = TypeRegistry.FromMessages(Timestamp.Descriptor); var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry)); var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.Utc).ToTimestamp(); var original = Any.Pack(timestamp); var json = formatter.Format(original); // This is tested in JsonFormatterTest var parser = new JsonParser(new JsonParser.Settings(10, registry)); Assert.AreEqual(original, parser.Parse <Any>(json)); string valueFirstJson = "{ \"value\": \"1673-06-19T12:34:56Z\", \"@type\": \"type.googleapis.com/google.protobuf.Timestamp\" }"; Assert.AreEqual(original, parser.Parse <Any>(valueFirstJson)); }
private static void Main(string[] args) { // This way we get the binary streams instead of readers/writers. var input = new BinaryReader(Console.OpenStandardInput()); var output = new BinaryWriter(Console.OpenStandardOutput()); var typeRegistry = TypeRegistry.FromMessages(ProtobufTestMessages.Proto3.TestAllTypesProto3.Descriptor); int count = 0; while (RunTest(input, output, typeRegistry)) { count++; } Console.Error.WriteLine("Received EOF after {0} tests", count); }
public void Any_Nested() { var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor); var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry)); var parser = new JsonParser(new JsonParser.Settings(10, registry)); var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 }; var nestedMessage = Any.Pack(doubleNestedMessage); var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMessage) }; var json = formatter.Format(message); // Use the descriptor-based parser just for a change. Assert.AreEqual(message, parser.Parse(json, TestWellKnownTypes.Descriptor)); }
public void AnyNested() { var registry = TypeRegistry.FromMessages(TestWellKnownTypes.Descriptor, TestAllTypes.Descriptor); var formatter = new JsonFormatter(new JsonFormatter.Settings(false, registry)); // Nest an Any as the value of an Any. var doubleNestedMessage = new TestAllTypes { SingleInt32 = 20 }; var nestedMessage = Any.Pack(doubleNestedMessage); var message = new TestWellKnownTypes { AnyField = Any.Pack(nestedMessage) }; AssertJson("{ 'anyField': { '@type': 'type.googleapis.com/google.protobuf.Any', 'value': { '@type': 'type.googleapis.com/protobuf_unittest.TestAllTypes', 'singleInt32': 20 } } }", formatter.Format(message)); }
public void Any_RegularMessage() { var registry = TypeRegistry.FromMessages(TestAllTypes.Descriptor); var formatter = new JsonFormatter(new JsonFormatter.Settings(false, TypeRegistry.FromMessages(TestAllTypes.Descriptor))); var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } }; var original = Any.Pack(message); var json = formatter.Format(original); // This is tested in JsonFormatterTest var parser = new JsonParser(new JsonParser.Settings(10, registry)); Assert.AreEqual(original, parser.Parse <Any>(json)); string valueFirstJson = "{ \"singleInt32\": 10, \"singleNestedMessage\": { \"bb\": 20 }, \"@type\": \"type.googleapis.com/protobuf_unittest.TestAllTypes\" }"; Assert.AreEqual(original, parser.Parse <Any>(valueFirstJson)); }