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

Fix using gRPC reflection with services that have a base type #1352

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Linq;
using System.Reflection;
using Grpc.AspNetCore.Server;
using Grpc.Core;
using Grpc.Reflection;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down Expand Up @@ -63,10 +64,7 @@ public static IServiceCollection AddGrpcReflection(this IServiceCollection servi

foreach (var serviceType in serviceTypes)
{
var baseType = GetServiceBaseType(serviceType);
var definitionType = baseType?.DeclaringType;

var descriptorPropertyInfo = definitionType?.GetProperty("Descriptor", BindingFlags.Public | BindingFlags.Static);
var descriptorPropertyInfo = GetDescriptorProperty(serviceType);
if (descriptorPropertyInfo != null)
{
if (descriptorPropertyInfo.GetValue(null) is Google.Protobuf.Reflection.ServiceDescriptor serviceDescriptor)
Expand All @@ -85,6 +83,59 @@ public static IServiceCollection AddGrpcReflection(this IServiceCollection servi
return services;
}

private static PropertyInfo? GetDescriptorProperty(Type serviceType)
{
// Prefer finding the descriptor property using attribute on the generated service
var descriptorPropertyInfo = GetDescriptorPropertyUsingAttribute(serviceType);

if (descriptorPropertyInfo == null)
{
// Fallback to searching for descriptor property using known type hierarchy that Grpc.Tools generates
descriptorPropertyInfo = GetDescriptorPropertyFallback(serviceType);
}

return descriptorPropertyInfo;
}

private static PropertyInfo? GetDescriptorPropertyUsingAttribute(Type serviceType)
{
Type? currentServiceType = serviceType;
BindServiceMethodAttribute? bindServiceMethod;
do
{
// Search through base types for bind service attribute.
bindServiceMethod = currentServiceType.GetCustomAttribute<BindServiceMethodAttribute>();
if (bindServiceMethod != null)
{
// Descriptor property will be public and static and return ServiceDescriptor.
return bindServiceMethod.BindType.GetProperty(
"Descriptor",
BindingFlags.Public | BindingFlags.Static,
binder: null,
typeof(Google.Protobuf.Reflection.ServiceDescriptor),
Type.EmptyTypes,
Array.Empty<ParameterModifier>());
}
} while ((currentServiceType = currentServiceType.BaseType) != null);

return null;
}

private static PropertyInfo? GetDescriptorPropertyFallback(Type serviceType)
{
// Search for the generated service base class
var baseType = GetServiceBaseType(serviceType);
var definitionType = baseType?.DeclaringType;

return definitionType?.GetProperty(
"Descriptor",
BindingFlags.Public | BindingFlags.Static,
binder: null,
typeof(Google.Protobuf.Reflection.ServiceDescriptor),
Type.EmptyTypes,
Array.Empty<ParameterModifier>());
}

private static Type? GetServiceBaseType(Type serviceImplementation)
{
// TService is an implementation of the gRPC service. It ultimately derives from Foo.TServiceBase base class.
Expand Down
5 changes: 5 additions & 0 deletions test/Grpc.AspNetCore.Server.Tests/Proto/greet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ service SecondGreeter {
rpc SayHellos (HelloRequest) returns (stream HelloReply);
}

service ThirdGreeterWithBaseType {
rpc SayHello (HelloRequest) returns (HelloReply);
rpc SayHellos (HelloRequest) returns (stream HelloReply);
}

message HelloRequest {
string name = 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,56 @@ public class ReflectionGrpcServiceActivatorTests
{
[Test]
public async Task Create_ConfiguredGrpcEndpoint_EndpointReturnedFromReflectionService()
{
// Arrange and act
TestServerStreamWriter<ServerReflectionResponse> writer = await ConfigureReflectionServerAndCallAsync(builder =>
{
builder.MapGrpcService<GreeterService>();
});

// Assert
Assert.AreEqual(1, writer.Responses.Count);
Assert.AreEqual(1, writer.Responses[0].ListServicesResponse.Service.Count);

var serviceResponse = writer.Responses[0].ListServicesResponse.Service[0];
Assert.AreEqual("greet.Greeter", serviceResponse.Name);
}

[Test]
public async Task Create_ConfiguredGrpcEndpointWithMultipleInheritenceLevel_EndpointReturnedFromReflectionService()
{
// Arrange and act
TestServerStreamWriter<ServerReflectionResponse> writer = await ConfigureReflectionServerAndCallAsync(builder =>
{
builder.MapGrpcService<InheritGreeterService>();
});

// Assert
Assert.AreEqual(1, writer.Responses.Count);
Assert.AreEqual(1, writer.Responses[0].ListServicesResponse.Service.Count);

var serviceResponse = writer.Responses[0].ListServicesResponse.Service[0];
Assert.AreEqual("greet.Greeter", serviceResponse.Name);
}

[Test]
public async Task Create_ConfiguredGrpcEndpointWithBaseType_EndpointReturnedFromReflectionService()
{
// Arrange and act
TestServerStreamWriter<ServerReflectionResponse> writer = await ConfigureReflectionServerAndCallAsync(builder =>
{
builder.MapGrpcService<GreeterServiceWithBaseType>();
});

// Assert
Assert.AreEqual(1, writer.Responses.Count);
Assert.AreEqual(1, writer.Responses[0].ListServicesResponse.Service.Count);

var serviceResponse = writer.Responses[0].ListServicesResponse.Service[0];
Assert.AreEqual("greet.ThirdGreeterWithBaseType", serviceResponse.Name);
}

private static async Task<TestServerStreamWriter<ServerReflectionResponse>> ConfigureReflectionServerAndCallAsync(Action<IEndpointRouteBuilder> action)
{
// Arrange
var endpointRouteBuilder = new TestEndpointRouteBuilder();
Expand All @@ -56,7 +106,8 @@ public async Task Create_ConfiguredGrpcEndpoint_EndpointReturnedFromReflectionSe
var serviceProvider = services.BuildServiceProvider(validateScopes: true);

endpointRouteBuilder.ServiceProvider = serviceProvider;
endpointRouteBuilder.MapGrpcService<GreeterService>();

action(endpointRouteBuilder);

// Act
var service = serviceProvider.GetRequiredService<ReflectionServiceImpl>();
Expand All @@ -73,12 +124,16 @@ public async Task Create_ConfiguredGrpcEndpoint_EndpointReturnedFromReflectionSe

await service.ServerReflectionInfo(reader, writer, context);

// Assert
Assert.AreEqual(1, writer.Responses.Count);
Assert.AreEqual(1, writer.Responses[0].ListServicesResponse.Service.Count);
return writer;
}

private class InheritGreeterService : GreeterService
{
}

private class GreeterServiceWithBaseType : ThirdGreeterWithBaseType.ThirdGreeterWithBaseTypeBase
{

var serviceResponse = writer.Responses[0].ListServicesResponse.Service[0];
Assert.AreEqual("greet.Greeter", serviceResponse.Name);
}

private class GreeterService : Greeter.GreeterBase
Expand Down Expand Up @@ -124,3 +179,18 @@ public IApplicationBuilder CreateApplicationBuilder()
}
}
}

namespace Greet
{
public class ThirdGreeterBaseType
{

}

public static partial class ThirdGreeterWithBaseType
{
public partial class ThirdGreeterWithBaseTypeBase : ThirdGreeterBaseType
{
}
}
}