Skip to content

Commit

Permalink
fix: Keep the gRPC full method name in RestServiceCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
jskeet committed Nov 29, 2022
1 parent 071c692 commit f24381c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
37 changes: 17 additions & 20 deletions Google.Api.Gax.Grpc.Tests/Rest/RestServiceCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,47 @@
* https://developers.google.com/open-source/licenses/bsd
*/

using Google.Api.Gax.Grpc.Rest;
using Grpc.Core;
using System;
using Xunit;

namespace Google.Api.Gax.Grpc.Tests.Rest;
namespace Google.Api.Gax.Grpc.Rest.Tests;

// Partial class for the Sample service to allow access to gRPC Method objects.
public partial class Sample
{
public static IMethod MethodWithNoHttpOptions => __Method_MethodWithNoHttpOptions;
public static IMethod SimpleMethod => __Method_SimpleMethod;
public static IMethod NonExistentMethod => new Method<SimpleRequest, SimpleResponse>(
MethodType.Unary,
__ServiceName,
"WrongName",
__Marshaller_google_api_gax_grpc_rest_tests_SimpleRequest,
__Marshaller_google_api_gax_grpc_rest_tests_SimpleResponse);
}

public class RestServiceCollectionTest
{
[Fact]
public void SupportedMethod()
{
var method = new FakeGrpcMethod("google.api.gax.grpc.rest.Sample.SimpleMethod");
var collection = CreateRestServiceCollectionForTestService();
Assert.NotNull(collection.GetRestMethod(method));
Assert.NotNull(collection.GetRestMethod(Sample.SimpleMethod));
}

[Fact]
public void UnsupportedMethod()
{
var method = new FakeGrpcMethod("google.api.gax.grpc.rest.Sample.MethodWithNoHttpOptions");
var collection = CreateRestServiceCollectionForTestService();
var exception = Assert.Throws<RpcException>(() => collection.GetRestMethod(method));
var exception = Assert.Throws<RpcException>(() => collection.GetRestMethod(Sample.MethodWithNoHttpOptions));
Assert.Equal(StatusCode.Unimplemented, exception.StatusCode);
}

[Fact]
public void UnknownMethod()
{
var method = new FakeGrpcMethod("google.api.gax.grpc.rest.Sample.ThisDoesntExist");
var collection = CreateRestServiceCollectionForTestService();
var exception = Assert.Throws<RpcException>(() => collection.GetRestMethod(method));
var exception = Assert.Throws<RpcException>(() => collection.GetRestMethod(Sample.NonExistentMethod));
Assert.Equal(StatusCode.InvalidArgument, exception.StatusCode);
}

Expand All @@ -49,16 +58,4 @@ public void BadService()
var metadata = new ApiMetadata("test", new[] { BadServiceReflection.Descriptor });
Assert.Throws<ArgumentException>(() => RestServiceCollection.Create(metadata));
}

// Just enough of IMethod to meet RestServiceCollection's needs.
private sealed class FakeGrpcMethod : IMethod
{
public string FullName { get; }

public FakeGrpcMethod(string fullName) => FullName = fullName;

public MethodType Type => throw new NotImplementedException();
public string ServiceName => throw new NotImplementedException();
public string Name => throw new NotImplementedException();
}
}
8 changes: 7 additions & 1 deletion Google.Api.Gax.Grpc/Rest/RestMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ internal class RestMethod

private RestMethod(ApiMetadata apiMetadata, MethodDescriptor protoMethod, JsonParser parser, HttpRuleTranscoder transcoder) =>
(_apiMetadata, _protoMethod, _parser, FullName, _transcoder) =
(apiMetadata, protoMethod, parser, $"/{protoMethod.Service.FullName}/{protoMethod.Name}", transcoder);
(apiMetadata, protoMethod, parser, GetGrpcFullName(protoMethod), transcoder);

/// <summary>
/// Returns the name by which gRPC will refer to the given proto method,
/// e.g. "/google.somepackage.SomeService/SomeMethod".
/// </summary>
internal static string GetGrpcFullName(MethodDescriptor method) => $"/{method.Service.FullName}/{method.Name}";

/// <summary>
/// Creates a <see cref="RestMethod"/> representation from the given protobuf method representation.
Expand Down
2 changes: 1 addition & 1 deletion Google.Api.Gax.Grpc/Rest/RestServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal static RestServiceCollection Create(ApiMetadata metadata)
var parser = new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true).WithTypeRegistry(typeRegistry));
var methodsByName = services.SelectMany(service => service.Methods)
.ToDictionary(
method => method.FullName,
RestMethod.GetGrpcFullName,
method => RestMethod.Create(metadata, method, parser),
StringComparer.Ordinal);
return new RestServiceCollection(new ReadOnlyDictionary<string, RestMethod>(methodsByName));
Expand Down

0 comments on commit f24381c

Please sign in to comment.